Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3211 Accepted Submission(s): 1181
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
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
刚学差分约束,不敢玩高大尚的,一步步来。
题意:有n个区间,给出每个区间的左端点和右端点以及在该区间至少有多少个数在集合Z里面,现在让你求出集合Z里面最少元素个数。
思路:设S[i] 表示集合Z里面的元素在区间[0, i ]的个数,Maxl,Maxr分别表示所有区间里面的最左端和最右端,dist[]数组存储源点到某点的最短路。则由题意得限制条件
一 S[right] - S[left-1] >= least 即[left, right]区间个数不小于least,转换得S[left-1] - S[right] <= least;
二 0 <= S[i] - S[i-1] <= 1转换得 S[i-1] - S[i] <= 0 && S[i] - S[i-1] <= 1。
然后根据限制条件建图
转化问题:题目需要求的是S[Maxr] - S[Maxl-1] >= ans 即S[Maxl-1] - S[Maxr] <= -ans。 若以Maxr为源点 ,而-ans就为Maxr到Maxl-1的最短路径的相反数,即-dist[Maxl-1]。
代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 1000000000
#define MAX 50000+10
using namespace std;
struct Edge
{
int from, to, val, next;
}edge[150000+10];
int n;//n个区间
int dist[MAX];//最短路 以区间最右端为源点
int Maxl, Maxr;//区间最左段与最右端
bool vis[MAX];//标记是否在队列里面
int head[MAX], top;
void init()
{
top = 0;
memset(head, -1, sizeof(head));
Maxl = 50000+10, Maxr = 0;
}
void addedge(int u, int v, int w)
{
Edge E = {u, v, w, head[u]};
edge[top] = E;
head[u] = top++;
}
void getMap()
{
int a, b, c;
for(int i = 0; i < n; i++)
{
scanf("%d%d%d", &a, &b, &c);
Maxl = min(Maxl, a);
Maxr = max(Maxr, b);//更新
addedge(b, a-1, -c);
}
for(int i = Maxl; i <= Maxr; i++)
{
addedge(i, i-1, 0);
addedge(i-1, i, 1);
}
}
void SPFA()
{
queue<int> Q;
for(int i = Maxl-1; i <= Maxr; i++)//区间最右端为源点
dist[i] = i==Maxr ? 0 : INF;
memset(vis, false, sizeof(vis));
vis[Maxr] = true;
Q.push(Maxr);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(dist[E.to] > dist[u] + E.val)
{
dist[E.to] = dist[u] + E.val;
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
printf("%d\n", -dist[Maxl-1]);
}
int main()
{
while(scanf("%d", &n) != EOF)
{
init();
getMap();
SPFA();
}
return 0;
}