hdu 1384 Intervals

Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4673    Accepted Submission(s): 1773


Problem 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 endpoints 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 <= 50 000) - 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 <= 50 000 and 1 <= ci <= bi - ai + 1.
Process to the end of file.
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
 
Author
1384
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:   1529  1548  1596  3592  3666 
 

思路:
  

题意:要求找到一个集合,限制条件是 给定n个区间,并要求位于该区间内的元素至少由k个 a,b,k
求该集合最少有多少个数
设s[i]为1-i 中有多少个数
则对于每一组限制均有: s[b]-sum[a-1]>=k;
考虑是集合:所以每个元素最多出现一次:即 s[i]-s[i-1]<=1;s[i]-s[i-1]>=0;
答案即为: s[max_]-s[min_-1]>=ans
典型的差分约束模型: 求最小值,找出min_到max的最长路
因为该图是联通的,所以不必建立超级源点>>>>>>

 
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
struct edge{int to,v;};
vector<edge>g[N];
int dis[N],vis[N];
int spfa(int s,int t){
    queue<int>q;
    memset(dis,-1,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;vis[s]=1;q.push(s);
    while(!q.empty()){
        int u=q.front();q.pop();vis[u]=0;
        for(int i=0;i<g[u].size();i++){
            edge v=g[u][i];
            if(dis[v.to]<dis[u]+v.v){
                dis[v.to]=dis[u]+v.v;
                if(!vis[v.to]){
                    q.push(v.to);
                    vis[v.to]=1;
                }
            }
        }
    }
    return dis[t];
}
int main(){
    int n,a,b,c;
    while(~scanf("%d",&n)){
        for(int i=0;i<N;i++) g[i].clear();
        int s=N,t=-1;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d",&a,&b,&c);
            g[a].push_back({b+1,c});
            s=min(s,a);t=max(t,b+1);
        }
        for(int i=s;i<=t;i++){
            g[i].push_back({i+1,0});
            g[i+1].push_back({i,-1});
        }
        int ans=spfa(s,t); 
        printf("%d\n",ans);
    }
    return 0;
} 

  

猜你喜欢

转载自www.cnblogs.com/vainglory/p/9150084.html
今日推荐