poj 1201

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个闭区间

【a,b】  及c

要求区间内的点数必须要大于等于c

点只存在于区间内。

然后问负无穷到正无穷最多有多少点。

做法:

我们从最左边到最右边。

区间内最少有c个点,说明点(a-1)到b点 最少会增加c个点。

也就是说  b-(a-1)>=c  转换下   (a-1)-b<=-c 就是差分约束的公式了。

然后就是建边 建一条权值为-c   的b到(a-1)的边就行了。

然后取最小的坐标x,取最大的坐标d。

因为求最大值  所以得公式 d-(x-1)>=?

加个负号, (x-1)-d<=-?   ,所以最后最短路 计算 d点到(x-1)的最短距离,然后取负就是答案了。


但是这样条件还不够。

还有关键的是相邻点之间的建边,设相邻两点  (i) 和(i+1)   

走到(i+1)数到的点数  肯定是比左边的(i)要大的 , 所以 (i+1)-(i)>=0  加个负号, (i)-(i+1)<=0

而从(i)走到(i+1)最多只增加了一个点,所以(i+1)-(i)<=1 ,   

所以还要建这些边,才能跑出最后的答案。

//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,-1,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e5+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while

int n,x,y,k;
struct node
{
    int to,w,next;
}E[maxx*40];
int head[maxx];
int d[maxx],inq[maxx],num[maxx];
inline int Scan()
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')flag=1;
    else if(ch>='0' && ch<='9')res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
    return flag ? -res : res;
}
void init()
{
    memset(head,-1,sizeof(head));
    k=0;
    for(int i=0;i<maxx;i++)
    {
        inq[i]=0;
        d[i]=INF;
    }
}

void add(int from,int to,int w)
{
    E[k].to=to;
    E[k].w=w;
    E[k].next=head[from];
    //cout<<E[k].to<<"  "<<E[k].w<<endl;
    head[from]=k++;

}
int SPFA()
{
    int s=y;
    queue<int >Q;
    Q.push(s),d[s]=0,inq[s]=1;
    me(num);
    while(!Q.empty())
    {
        int now=Q.front();
        //cout<<now<<endl;
        Q.pop();
        inq[now]=0;
        ++num[now];
        if(num[now]>n) return 0;
        for(int i=head[now];i!=-1;i=E[i].next)
        {
            int v=E[i].to,w=E[i].w;
            if(d[v]>d[now]+w)
            {
                d[v]=d[now]+w;
                if(inq[v]==1) continue;
                inq[v]=1;
                Q.push(v);
            }
        }
    }
    if(d[x-1]!=INF)
        cout<<-d[x-1]<<endl;
    else  puts("-2");
    return 1;
}
int main()
{
    int T;
    cin>>T;
    init();
    x=INF;y=0;
    W(T--)
    {
        int a,b,c;
        a=Scan();b=Scan();c=Scan();
        add(b,a-1,-c); //b到a-1
        //add(b,a-1,-c);
        x=min(x,a,b);
        y=max(y,a,b);
    }
    n=y-(x-2);
    FOr(x-1,y,i)
    {
        add(i,i+1,1);
        add(i+1,i,0);
        // E[i].push_back(make_pair(i+1,1));
        // E[i+1].push_back(make_pair(i,0));
    }
    //cout<<k<<endl;
    int t=SPFA();
    if(t==0)
        puts("-1");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值