【POJ 1716 Integer Intervals】+ 差分约束
2017-03-16 18:21
393 查看
Integer Intervals
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14475 Accepted: 6149
Description
An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b.
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.
Output
Output the minimal number of elements in a set containing at least two different integers from each interval.
Sample Input
4
3 6
2 4
0 2
4 7
Sample Output
4
限制条件有三个:
1、对于区间,必定有num[start] <= num[end]-2;
2、对于整数点,必有num[i] <= num[i+1];
3、对于整数点,必有num[i+1] <= num[i]+1;
AC代码:
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14475 Accepted: 6149
Description
An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b.
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.
Output
Output the minimal number of elements in a set containing at least two different integers from each interval.
Sample Input
4
3 6
2 4
0 2
4 7
Sample Output
4
限制条件有三个:
1、对于区间,必定有num[start] <= num[end]-2;
2、对于整数点,必有num[i] <= num[i+1];
3、对于整数点,必有num[i+1] <= num[i]+1;
AC代码:
#include<cstdio> #include<algorithm> using namespace std; const int K = 1e4 + 10; int a[K],b[K],num[K]; int main() { int N; while(scanf("%d",&N) != EOF){ int l = K,r = 0; for(int i = 1; i <= N; i++) scanf("%d %d",&a[i],&b[i]), b[i]++,l = min(l,a[i]),r = max(r,b[i]); int ok = 1; while(ok){ ok = 0; for(int i = 1; i <= N; i++) if(num[a[i]] > num[b[i]] - 2) num[a[i]] = num[b[i]] - 2,ok = 1; for(int i = l; i < r; i++) if(num[i + 1] > num[i] + 1) num[i + 1] = num[i] + 1,ok = 1; for(int i = r - 1; i >= l; i--) if(num[i] > num[i + 1]) num[i] = num[i + 1],ok = 1; } printf("%d\n",num[r] - num[l]); } return 0; }
相关文章推荐
- POJ 1716 Integer Intervals 差分约束
- 关于差分约束的一些总结和题解。(poj 1364 ,3159, 2983, 3169 ,1201 ,1716 ,1275)
- POJ 1716 Integer Intervals (差分约束,SPFA)
- poj 1716 Integer Intervals(差分约束)
- poj 1716 Integer Intervals(差分约束||贪心)(中等)
- POJ-1716 同上..SPFA差分约束..
- POJ 1716 Integer Intervals【差分约束】
- POJ 1716 Integer Intervals(差分约束)
- poj 1716 1201 差分约束 SPFA
- POJ 1716 Integer Intervals (差分约束)
- POJ 1716 (差分约束)
- poj1716 Integer Intervals(差分约束)
- Poj 1716 Integer Intervals(差分约束)
- poj 1716 -- Integer Intervals ( 差分约束 )
- poj 1716 Integer Intervals (差分约束 或 贪心)
- POJ 1716 Integer Intervals(差分约束)
- POJ 1716 Integer Intervals(差分约束)
- poj 1716 Integer Intervals (差分约束)
- poj1716 Integer Intervals (差分约束)
- poj1716(差分约束+Bellman_Ford)