POJ 1201 差分约束系统

来源:互联网 发布:索隆 知乎 编辑:程序博客网 时间:2024/04/28 19:43

题目

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 0x3f3f3f3ftypedef 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;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 人被冤枉敲诈勒索进看守所了怎么办 打架被拘留孩子怎么办三周了 技师被拘留她的家人该怎么办 我是担保人贷款人跑了我该怎么办 公司依旷工理由辞退我该怎么办 试工两天被辞退了不发工资怎么办 员工在单位被打监控调不出来怎么办 老师罚做60张试卷家长该怎么办 孩子在幼儿园被老师打该怎么办 孩子被孩子打了老师该怎么办? 法院判决书下来后看守所不收怎么办 因病看守所不收押发院判了怎么办 现在显示还在看守所羁押需要怎么办 嫖了N多年丈夫让我怎么办 预约考试的登录密码忘了怎么办 人进了看守所信用卡没还怎么办 上海租房人放2个麻将台怎么办 南宁公租房住满5年后怎么办 合伙买房时如果出售意见不合怎么办 老旧小区改造下水一楼不同意怎么办 老旧小区下水改造没改怎么办 替公司租房子中介不退押金怎么办 想在昆山找合租房的该怎么办 链家二手房价钱买贵了怎么办 拿私人房产证抵押借钱不还怎么办 在借贷宝里借钱不还怎么办 出租屋的大门感应钥匙弄丢了怎么办 法院拍卖的房子房主不配合怎么办 租的房子如果房主卖了怎么办 房东把门锁换了里面的东西怎么办 房租没到期房东把门锁换了怎么办 租了三年店面房东违反了合同怎么办 学生登录教务系统的密码忘记怎么办 铜陵无牌助力车被交警查到怎么办 福州超标电动车被交警抓到怎么办 单位自管公租房承租人去世怎么办 取得房产证后贷款批不下来怎么办 租店面遇到难搞的房东怎么办 在拆违通知书上签字了该怎么办 单位没交公职金的退休后怎么办 公租房合同到期后没有续签怎么办