poj-1201 intervals (differential constraint)

Source: Internet
Author: User

Topic Links:

Intervals

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24379 Accepted: 9274

Description

You are given n closed, integer intervals [AI, bi] and n integers c1, ..., CN.
Write a program:
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 have at least CI common elements with interval [AI, bi], for each i= ,..., 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 & lt;= 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

53 7 38 10 36 8 11 3 110 11 1

Sample Output

6



Test instructions

Give an interval [Ai,bi], which has the same CI elements, asking how many elements the set contains at least;


Ideas:

Can get such a few inequalities;
c[i]<=dis[b+1]-dis[a];
0<=dis[i+1]-dis[i];
-1<=dis[i]-dis[i+1];
Test instructions is must meet these conditions, then is to find the longest road, of course, can also become >= to build a map, the shortest way;



AC Code:


code for//longest path
//#include <bits/stdc++.h>#include <iostream>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong LongLL;ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=0x3f3f3f3f;Const intn=5e4+5;intn,head[3*n],vis[n],ans[n],cnt,mmin;structedge{intTo,next,dis;} edge[3*N];queue<int>Qu;voidSPFA () {MST (Vis,0); MST (ans,-inf); Ans[mmin]=0; Qu.push (mmin); Vis[mmin]=1; while(!Qu.empty ()) { intFr=Qu.front (); Qu.pop (); for(inti=head[fr];i!=-1; i=Edge[i].next) { intx=edge[i].to; if(ans[x]<ans[fr]+Edge[i].dis) {Ans[x]=ans[fr]+Edge[i].dis; if(!Vis[x]) {Qu.push (x); VIS[X]=1; }}} Vis[fr]=0; }}voidAdd_edge (intSintEintval) {edge[cnt].to=e; Edge[cnt].next=Head[s]; Edge[cnt].dis=Val; Head[s]=cnt++;}intMain () { while(SCANF ("%d", &n)! =EOF) { inta,b,c,mmax=0; Mmin=55000; CNT=0; MST (Head,-1); Riep (n) {scanf ("%d%d%d",&a,&b,&c); Mmax=Max (MMAX,B); Mmin=min (mmin,a); Add_edge (A, B+1, c); } riep (Mmax) Add_edge (i,i+1,0), Add_edge (i+1, i,-1); SPFA (); printf ("%d\n", ans[mmax+1]); } return 0;}
//code for Shortest path//#include <bits/stdc++.h>#include <iostream>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong LongLL;ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=0x3f3f3f3f;Const intn=5e4+5;intn,head[3*N],vis[n],ans[n],cnt,mmin,mmax;structedge{intTo,next,dis;} edge[3*N];queue<int>Qu;voidSPFA () {MST (Vis,0);    MST (Ans,inf); Ans[mmax]=0;    Qu.push (Mmax); Vis[mmax]=1;  while(!Qu.empty ()) {        intFr=Qu.front ();        Qu.pop ();  for(inti=head[fr];i!=-1; i=Edge[i].next) {            intx=edge[i].to; if(ans[x]>ans[fr]+Edge[i].dis) {Ans[x]=ans[fr]+Edge[i].dis; if(!Vis[x])                    {Qu.push (x); VIS[X]=1; }}} Vis[fr]=0; }}voidAdd_edge (intSintEintval) {edge[cnt].to=e; Edge[cnt].next=Head[s]; Edge[cnt].dis=Val; Head[s]=cnt++;}intMain () { while(SCANF ("%d", &n)! =EOF) {        intA,b,c; Mmax=0; Mmin=55000; CNT=0; MST (Head,-1); Riep (n) {scanf ("%d%d%d",&a,&b,&c); Mmax=max (mmax,b+1); Mmin=min (mmin,a); Add_edge (b+1, a,-c); } riep (Mmax) Add_edge (i,i+1,1), Add_edge (i+1I0);        SPFA (); printf ("%d\n",-Ans[mmin]); }    return 0;}

poj-1201 intervals (differential constraint)

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.