Intervals

Descriptionios

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.  

Inputweb

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 Inputspa

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

Sample Outputorm

6題意:n個區間,[a[i],b[i]]每一個區間至少選c[i]個數。求知足條件的最小數的個數題解:設f[i]爲[1,i]中選的數的個數,那麼對於區間[a,b]來講,f[b]-f[a-1]>=c;f[b]>=f[a-1]+c;另外,0<=f[i]-f[i-1]<=1;spfa算法+差分約束#include<stdio.h>#include<iostream>#include<queue>#include<string.h>#define MAX 50005using namespace std;int head[50005];int dest[50005];int visit[50005];struct lmx{ int v; int next; int val;};lmx lm[MAX*4];int k,nmin,nmax;void add(int u,int v,int w){  lm[k].v=v;  lm[k].val=w;  lm[k].next=head[u];  head[u]=k++;}void spfa(){ queue<int> s; int i; memset(visit,0,sizeof(visit)); for(i=0;i<=50000;i++) dest[i]=-50000; dest[nmin]=0; visit[nmin]=1; s.push(nmin); while(!s.empty()) {  int top=s.front();  s.pop();  for(i=head[top];i!=-1;i=lm[i].next)  {   if(dest[lm[i].v]<dest[top]+lm[i].val)   {    dest[lm[i].v]=dest[top]+lm[i].val;    if(visit[lm[i].v]==0)    {     visit[lm[i].v]=1;     s.push(lm[i].v);    }   }  }  visit[top]=0; }}int main(){ int n,a,b,c,i;    while(scanf("%d",&n)!=EOF) {  nmin=50005;  nmax=0;  k=0;  memset(head,-1,sizeof(head));  while(n--)  {   scanf("%d %d %d",&a,&b,&c);   if(nmin>a) nmin=a;   if(nmax<b+1) nmax=b+1;   add(a,b+1,c);  }  for(i=nmin;i<=nmax;i++)  {   add(i,i-1,-1);   add(i-1,i,0);  }  spfa();  printf("%d\n",dest[nmax]); } return 0;}
相關文章
相關標籤/搜索