对差分约束理解还是不深入,约束条件总是漏掉。
题目中的约束条件
s[a]-s[b]>=n
s[i]-s[i-1]>=0
s[i]-s[i-1]<=1
s[i]表示整数集合Z从0到i这一范围内的整数个数
然后把第一个和第二个的约束乘以-1就可以编程最短了。
Intervals
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17050 | Accepted: 6383 |
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 end points 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 end points 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 <= 50000) -- 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 <= 50000 and 1 <= ci <= bi - ai+1.
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
#include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<vector> using namespace std; #define MAXN 50500 #define INF 0xFFFFFF struct node { int to; int dis; }; vector<node> g[MAXN]; int dis[MAXN]; int l[MAXN],r[MAXN],num[MAXN]; bool in[MAXN]; int n,m,mm; int mins,maxs; void init() { node temp; maxs=0; mins=INF; for(int i=1;i<=n;i++) { scanf("%d%d%d",&l[i],&r[i],&num[i]); mins=min(l[i]-1,mins); maxs=max(maxs,r[i]); } for(int i=mins;i<=maxs;i++) g[i].clear(); for(int i=1;i<=n;i++) { temp.to=l[i]-1; temp.dis=-num[i]; g[r[i]].push_back(temp); } for(int i=mins+1;i<=maxs;i++) { temp.to=i; temp.dis=1; g[i-1].push_back(temp); temp.to=i-1; temp.dis=0; g[i].push_back(temp); } } void spfa(int s) { queue<int> q; for(int i=mins;i<=maxs;i++) { dis[i]=INF; in[i]=false; } dis[s]=0; in[s]=true; q.push(s); while(!q.empty()) { int tag=q.front(); q.pop(); in[tag]=false; for(int i=0;i<g[tag].size();i++) { int j=g[tag][i].to; if(dis[j]>g[tag][i].dis+dis[tag]) { dis[j]=g[tag][i].dis+dis[tag]; if(!in[j]) { in[j]=true; q.push(j); } } } } } int main() { while(cin>>n) { init(); spfa(maxs); cout<<-dis[mins]<<endl; } return 0; }