您的位置:首页 > 其它

POJ 1201 差分约束系统

2016-04-30 00:01 267 查看

题目

Intervals

Time 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj