hdu 1384 Intervals (差分约束)
Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4181 Accepted Submission(s): 1577
Problem Description
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
> reads the number of intervals, their endpoints and integers c1, ..., cn from the standard input,
> computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i = 1, 2, ..., n,
> writes the answer to the standard output
Write a program that:
> reads the number of intervals, their endpoints and integers c1, ..., cn from the standard input,
> computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i = 1, 2, ..., n,
> writes the answer to the standard output
Input
The first line of the input contains an integer n (1 <= n <= 50 000) - the number of intervals. The following n lines describe the intervals. The i+1-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50 000 and 1 <= ci <= bi - ai + 1.
Process to the end of file.
Process to the end of file.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i = 1, 2, ..., n.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Author
1384
一道很明显的差分约束题。
对于每个[l,r]对应的k可以列出方程 f[r]-f[l-1]>=k;
同时区间之间 f[i]-f[i-1]>=0; f[i-1]-f[i]>=-1;
然后就是拿所有区间的最左端点作源点跑一遍spfa();
结束。
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<vector> 5 #include<set> 6 #define INF 1000000000 7 #define clr(x) memset(x,0,sizeof(x)) 8 #define clrmin(x) memset(x,-1,sizeof(x)) 9 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x)) 10 using namespace std; 11 struct node 12 { 13 int to,val,next; 14 }edge[50010*3]; 15 int head[50010]; 16 int dis[50010]; 17 int n,maxx,minx,maxans,cnt,l,r,k; 18 set<int> Q; 19 void addedge(int l,int r,int k); 20 int min(int a,int b); 21 int max(int a,int b); 22 void spfa(int s); 23 int main() 24 { 25 while(scanf("%d",&n)!=EOF) 26 { 27 clrmin(head); 28 clrmin(dis); 29 Q.clear(); 30 maxx=0; 31 cnt=0; 32 for(int i=1;i<=n;i++) 33 { 34 scanf("%d%d%d",&l,&r,&k); 35 addedge(l,r+1,k); 36 maxx=max(r+1,maxx); 37 minx=min(l,minx); 38 } 39 for(int i=minx;i<maxx;i++) 40 { 41 addedge(i,i+1,0); 42 addedge(i+1,i,-1); 43 }; 44 spfa(minx); 45 printf("%d\n",dis[maxx]); 46 } 47 return 0; 48 } 49 void addedge(int l,int r,int k) 50 { 51 edge[++cnt].to=r; 52 edge[cnt].val=k; 53 edge[cnt].next=head[l]; 54 head[l]=cnt; 55 return; 56 } 57 int min(int a,int b) 58 { 59 return a<b?a:b; 60 } 61 int max(int a,int b) 62 { 63 return a>b?a:b; 64 } 65 void spfa(int s) 66 { 67 dis[s]=0; 68 Q.insert(s); 69 int v,k; 70 while(!Q.empty()) 71 { 72 v=*Q.begin(); 73 Q.erase(v); 74 k=head[v]; 75 while(k!=-1) 76 { 77 if(dis[v]+edge[k].val>dis[edge[k].to]) 78 { 79 dis[edge[k].to]=dis[v]+edge[k].val; 80 Q.insert(edge[k].to); 81 } 82 k=edge[k].next; 83 } 84 } 85 return ; 86 }