POJ 1201 差分约束系统

题目

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值