POJ 1201 差分约束系统
2016-04-30 00:01
267 查看
题目
IntervalsTime Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24326 Accepted: 9247
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.
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
题意
给出一些区间,从这些区间里挑一些数。要求每个区间里的数不能小于一定的值。问最少要挑多少个数题解
定义d[i]为在[0,i]区间里挑了d[i]个数则有约束条件:
d[i+1]>=d[i],后面的数肯定不会比前面的数少。
d[i]+1>=d[i+1],d[i+1]比d[i]最多多一个1.
d[a[i].R] - d[a[i].L]>=a[i].v,满足题意包含合适的数。
然后就可以用Bllman-ford算法计算结果了。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <string> #include <set> #include <cmath> #include <map> #include <queue> #include <sstream> #include <vector> #include <iomanip> #define m0(a) memset(a,0,sizeof(a)) #define mm(a) memset(a,0x3f,sizeof(a)) #define m_1(a) memset(a,-1,sizeof(a)) #define f(i,a,b) for(i = a;i<=b;i++) #define fi(i,a,b) for(i = a;i>=b;i--) #define lowbit(a) ((a)&(-a)) #define FFR freopen("data.in","r",stdin) #define FFW freopen("data.out","w",stdout) #define INF 0x3f3f3f3f typedef long long ll; typedef long double ld; const ld PI = acos(-1.0); using namespace std; #define SIZE ( 50000+10) struct Edge { int L, R; int v; }; Edge a[SIZE]; int d[SIZE]; int main() { //ios_base::sync_with_stdio(false); cin.tie(0); int n; while (~scanf("%d", &n)) { int i; int Min = INF; int Max = -INF; f(i, 1, n) { int L, R, K; scanf("%d%d%d", &L, &R, &K); L++; R++; Min = min(L - 1, Min); Max = max(R, Max); a[i].L = L - 1; a[i].R = R; a[i].v = K; } m0(d); int ok = 1; while (ok) { ok = 0; f(i, 1, n) { if (d[a[i].L] > d[a[i].R] - a[i].v) { d[a[i].L] = d[a[i].R] - a[i].v; ok = 1; } } f(i, Min, Max - 1) if (d[i] > d[i + 1]) { d[i] = d[i + 1]; ok = 1; } f(i, Min, Max - 1) if (d[i + 1] > d[i] + 1) { d[i + 1] = d[i] + 1; ok = 1; } } printf("%d\n", d[Max] - d[Min]); } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)