Try Again

POJ 1201 Intervals

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
给定n个闭区间[ai,bi](1≤i≤n,0≤ai≤bi≤50000) 和n个整数ci(1≤i≤n)
你需要在数轴上选出尽可能少的点,满足每个区间[ai,bi]中,至少有ci个数被选中了。
//差分约束系统
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define INF 1044266558
struct edge{
    int v,w,next;
}e[150006];
int dis[50006],head[50006],in[50006],ans=0;
int l=INF,r=0,n;
void add_edge(int u,int v,int w){
    e[ans].v=v;
    e[ans].w=w;
    e[ans].next=head[u];
    head[u]=ans++;
}
void bfs(){
    queue<int>q;
    q.push(l);
    memset(dis,-1,sizeof(dis));
    dis[l]=0;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        in[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(dis[v]<dis[u]+e[i].w){
                dis[v]=dis[u]+e[i].w;
                if(!in[v]){
                    in[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main(){
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    for(int i=0;i<n;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        add_edge(u,v+1,w);
        l=min(l,u);
        r=max(r,v+1);
    }
    for(int i=l;i<r;i++){
        add_edge(i,i+1,0);
        add_edge(i+1,i,-1);
    }
    bfs();
    printf("%d\n",dis[r]);
    return 0;
}

 

posted @ 2018-11-02 18:40  十年换你一句好久不见  阅读(180)  评论(0编辑  收藏  举报