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
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.
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
从最开始学习差分约束之后就懵逼了 我还以为是什么很高级的东西 没想到还是 转化
转化转化 这是作图论最重要的步骤 因为转化同时也包括了 建图
简而言之 差分约束就是将 a-b<=c 的约束条件转化成最短路问题 这是我看的差分约束详解 简直一看就懂 再次感谢博主 http://www.cnblogs.com/void/archive/2011/08/26/2153928.html
关于这道题的话 题意是 给你很多线段 让你在这些线段中必须挑出几个点 加入一个集合 问你最后在这个中 元素最少有几个
这里的话 我们这样转化 输入 a b c 那么定义一个 函数 f(x) 表示 1— x 之间挑了多少个数
于是乎 得下列关系式 f(b)-f(a-1)>=c 对于所有区间内的点 a 有 0<=f(a)-f(a-1)<=1
习惯性写法 把0给避免掉的化 每次输入都 +1就好了
ac code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn=50005;
const int inf=0x3f3f3f3f;
int n,idx;
int head[maxn],d[maxn];
bool vis[maxn];
struct node
{
int v,w;
int nxt;
}edge[maxn];
void add(int u,int v,int w)
{
edge[idx].v=v;
edge[idx].w=w;
edge[idx].nxt=head[u];
head[u]=idx++;
}
void spfa(int mi)
{
queue<int> que;
que.push(mi);
vis[mi]=true;
d[mi]=0;
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].v;
if(d[u]+edge[i].w>d[v])
{
d[v]=d[u]+edge[i].w;
if(!vis[v])
{
vis[v]=true;
que.push(v);
}
}
}
}
}
int main()
{
int u,v,w;
while(scanf("%d",&n)!=EOF)
{
memset(head,-1,sizeof head);
idx=0;
int mi=inf,mx=-inf;
for(int i=0;i<n;i++)
{
scanf("%d %d %d",&u,&v,&w);
add(u,v+1,w);
mx=max(mx,v+1);
mi=min(mi,u);
}
for(int i=mi;i<=mx;i++)
{
add(i+1,i,-1);
add(i,i+1,0);
d[i]=-inf;
vis[i]=false;
}
spfa(mi);
printf("%d\n",d[mx]);
}
return 0;
}