Intervals
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
AC代码
//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lson k<<1
#define rson k<<1|1
#define mid (l+r)/2
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
//typedef unsigned long long ull;
#define MOD 142857
const int maxn = 100100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int n, m, s, t;
int len;
struct road
{
int u, v, len;
int next;
void getvalue(int uu, int vv, int ll, int nn)
{
u = uu; v = vv; len = ll; next = nn;
}
road(){}
};
struct node
{
int v;
int len;
int cost;
node(int v, int l, int c):v(v),len(l),cost(c){}
friend bool operator <(node a, node b)
{
return a.cost>b.cost;
}
};
road G[maxn*5];
int h[maxn];
int d[maxn];
bool vis[maxn];
void Frod(int Min)
{
ms(vis,0);
queue<int> q;
q.push(Min);
while(q.size())
{
int x = q.front(); q.pop();
vis[x] = 0;
for(int i = h[x]; i!=-1; i=G[i].next)
{
road e = G[i];
if(d[e.v] < d[e.u] + e.len)
{
d[e.v] = d[e.u] + e.len;
if(!vis[e.v])
{
q.push(e.v);
vis[e.v] = 1;
}
}
}
}
}
int main()
{
//freopen("out.txt", "w", stdout);
while(~sd(n))
{
int ta, tb, tc;
int Min = INF, Max = -INF;
len = 0;
fill(h, h+n+1, -1);
for(int i = 0; i < n; i++)
{
sddd(ta, tb, tc);
G[len].getvalue(ta,tb+1,tc,h[ta]);
h[ta] = len++;
Min = min(Min, ta);
Max = max(Max, tb+1);
}
fill(d+Min, d+Max+1, -INF);
d[Min] = 0;
for(int i = Min; i < Max; i++)
{
G[len].getvalue(i,i+1,0,h[i]);
h[i] = len++;
G[len].getvalue(i+1,i,-1,h[i+1]);
h[i+1] = len++;
}
Frod(Min);
printf("%d\n", d[Max]);
}
return 0;
}