UVA1723 Intervals 题解

· · 题解

题目传送门

一、简化题意

给定 n 个闭区间,对于每个给出的区间 [a_i,b_i],要求是:集合中在此区间出现的数量必须大于等于 c_i,求集合的最小大小。

二、什么意思

更简单说明就是,在数轴上,一个集合的数(未知),分布在不同的整数点上,对于题目所给的区间 [a_i,b_i],要求数轴上这段区间上本集合出现的数量大于等于 c_i。

三、确定思路,算法

其实我们不妨将集合在数轴上的数设为 vis[i] = 1(这是因为 a_i,b_i 范围很小),然后我们就可以做一个前缀和 D[i],根据题意,我们可以得:

D[b_i] - D[a_i] >= C_i

哎,这不就是一个差分约束吗(建边 (a_i-1) \rightarrow b_i,权值为 c_i)?(看到这种式子,就得想到差分约束+SPFA)

不过,这只是每个差分约束初学者都要做到的这一点,我们在看待这个关键的前缀和,对于每个正整数 i,是不是一定有:

$D[i-1] + 1 >= D[i]$ ② 你可能会想:这不是正整数前缀和都满足的条件吗?有什么用? 没错!这确实是我们人类都知道的,但你想想,你的编译器,你眼前的这台电脑,难道会自己看出 ① 式和 ② 式吗? 所以,我们在做**差分约束**时一定要仔细观察,将我们寻常都可以得到的信息全都告诉电脑。 本蒟蒻一开始毫无头绪,后来又在建边时出错导致卡了我很久,一开始是这样的: ``` #include<bits/stdc++.h> using namespace std; int n; const int N = 4005; struct node{ long long to; long long w; }; basic_string <node>E[N]; long long dist[N]; long long rd[N]; bool vis[N]; const long long inf = 1e18; void bfs() { queue<int>q; fill(dist,dist+n+1,-inf); q.push(0); vis[0]=true; dist[0]=0; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(auto ttmp : E[u]) { int v=ttmp.to; long long w=ttmp.w; if(dist[u]+w>dist[v]) { dist[v]=dist[u]+w; rd[v]=rd[u]+1; // if(rd[v]>n+1) { // cout<<"-1"; // exit(0); // } if(!vis[v]) { q.push(v); vis[v]=true; } } } } return ; } int T; int main() { ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>T; while(T--) { cin>>n; for(int i=1;i<=n+1;i++) { E[i].clear(); } memset(vis,false,sizeof(vis)); memset(rd,0,sizeof(rd)); for(int i=1,a,b,c;i<=n;i++) { cin>>a>>b>>c; E[a]+={b+1,c}; } for(int i=1;i<=n+1;i++) { E[i]+={i-1,-1}; E[i-1]+={i,1}; } bfs(); cout<<dist[n+1]<<endl; } return 0; } ``` 好了,喜提 `8` 分。 改了一下,再来! ``` #include<bits/stdc++.h> using namespace std; int n; const int N = 50005; struct node{ long long to; long long w; }; basic_string <node>E[N]; long long dist[N]; long long rd[N]; bool vis[N]; const long long inf = 1e18; void bfs() { queue<int>q; fill(dist,dist+n+1,-inf); q.push(0); vis[0]=true; dist[0]=0; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(auto ttmp : E[u]) { int v=ttmp.to; long long w=ttmp.w; if(dist[u]+w>dist[v]) { dist[v]=dist[u]+w; rd[v]=rd[u]+1; // if(rd[v]>n+1) { // cout<<"-1"; // exit(0); // } if(!vis[v]) { q.push(v); vis[v]=true; } } } } return ; } int T; int main() { ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>T; while(T--) { cin>>n; for(int i=0;i<=n+1;i++) { E[i].clear(); } //这里也改了 memset(vis,false,sizeof(vis)); memset(rd,0,sizeof(rd)); for(int i=1,a,b,c;i<=n;i++) { cin>>a>>b>>c; E[a]+={b+1,c}; } for(int i=1;i<=n+1;i++) { E[i]+={i-1,-1}; E[i-1]+={i,1}; } bfs(); long long ans=0; for(int i=0;i<=n+1;i++) { ans=max(ans,dist[i]); } cout<<ans<<endl; // 这块地方改了 } return 0; } ``` 好,喜提 `33` 分。 最终 AC 代码: :::success[AC code] ``` #include<bits/stdc++.h> using namespace std; int n; const int N = 50005; struct node{ long long to; long long w; }; basic_string <node>E[N]; long long dist[N]; long long rd[N],mx; bool vis[N]; const long long inf = 1e18; void bfs() { queue<int>q; memset(dist,128,sizeof(dist)); q.push(0); vis[0]=true; dist[0]=0; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(auto ttmp : E[u]) { int v=ttmp.to; long long w=ttmp.w; if(dist[u]+w>dist[v]) { dist[v]=dist[u]+w; rd[v]=rd[u]+1; if(!vis[v]) { q.push(v); vis[v]=true; } } } } return ; } int T; int main() { ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>T; while(T--) { cin>>n; for(int i=0;i<=N-5;i++) { E[i].clear(); } mx=0; memset(vis,false,sizeof(vis)); memset(rd,0,sizeof(rd)); for(int i=1,a,b,c;i<=n;i++) { cin>>a>>b>>c; E[a]+={b+1,c},mx=max(mx,(long long)b+1); //原来得求maxx } for(int i=1;i<=mx;i++) { E[i]+={i-1,-1}; E[i-1]+={i,0}; //this ,本蒟蒻经验不足 } bfs(); cout<<dist[mx]<<endl; //this } return 0; } ``` :::