POJ1201 Intervals (difference constraint), poj1201intervals

Source: Internet
Author: User

POJ1201 Intervals (difference constraint), poj1201intervals

Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:28416   Accepted:10966
DescriptionYou 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. inputThe 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. outputThe 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

53 7 38 10 36 8 11 3 110 11 1
Sample Output
6
SourceSouthwestern Europe 2002 Each time you give an interval $ [a_ I, B _ I] $ and a number $ c_ I $, so that there is at least $ c_ I $ in the middle, and you can find a minimum set $ Z $, so that the set $ Z $ meets all the above requirements, ask the size of the set $ Z $ Ideas: Set $ S [I] $ to indicate the prefix and Then the relation of the question becomes $ S [B _ I]-S [a_ I]> = c_ I $ This is a typical differential constraint problem. The minimum set is required in the question, so convert to the longest path and write all the statements in the form of $ B-A> = C $ There is also a condition $0 <= S [I]-S [I-1] <= 1 $ Because the data is an integer So we get two more equations. $ S \ left [I \ right]-S \ left [i-1 \ right] \ geq 0 $
$ S \ left [i-1 \ right]-S \ left [I \ right] \ geq-1 $
But there is a detail: $ S [I-1] $ cannot be represented, so we need to place all subscript $ + 1 $, then $ S [I] $ represents $0 to (I-1) $ prefix and At the same time, this figure is China Unicom, so you do not need to create a super Source 
#include<cstdio>#include<queue>#include<cstring>#define INF 1e8+10using namespace std;const int MAXN=1e6+10;#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)char buf[MAXN],*p1=buf,*p2=buf;inline int read(){    char c=getchar();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}    return x*f;}struct node{    int u,v,w,nxt;}edge[MAXN];int head[MAXN],num=1;int maxx=-INF,minn=INF;int dis[MAXN],vis[MAXN];inline void AddEdge(int x,int y,int z){    edge[num].u=x;    edge[num].v=y;    edge[num].w=z;    edge[num].nxt=head[x];    head[x]=num++;}int SPFA(){    queue<int>q;    memset(dis,-0xf,sizeof(dis));    dis[minn]=0;q.push(minn);     while(q.size()!=0)    {        int p=q.front();q.pop();        vis[p]=0;        for(int i=head[p];i!=-1;i=edge[i].nxt)        {            if(dis[edge[i].v]<dis[p]+edge[i].w)            {                dis[edge[i].v]=dis[p]+edge[i].w;                if(vis[edge[i].v]==0)                    vis[edge[i].v]=1,q.push(edge[i].v);            }        }    }    printf("%d",dis[maxx]);}int main(){    #ifdef WIN32    freopen("a.in","r",stdin);    #else    #endif    memset(head,-1,sizeof(head));    int N=read();    for(int i=1;i<=N;i++)    {        int x=read(),y=read(),z=read();        AddEdge(x,y+1,z);         maxx=max(y+1,maxx);        minn=min(x,minn);    }    for(int i=minn;i<=maxx-1;i++)    {        AddEdge(i+1,i,-1);        AddEdge(i,i+1,0);    }    SPFA();    return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.