标签:des style blog http color os java io strong
Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2931 Accepted Submission(s): 1067Problem DescriptionYou 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
InputThe 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.
OutputThe 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 Input5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
Sample Output6
1 /*************************************************************************
2 > File Name: 1384.cpp
3 > Author: Stomach_ache
4 > Mail: sudaweitong@gmail.com
5 > Created Time: 2014年08月26日 星期二 08时59分19秒
6 > Propose:
7 ************************************************************************/
8 #include <queue>
9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <vector>
13 #include <fstream>
14 #include <cstring>
15 #include <iostream>
16 #include <algorithm>
17 using namespace std;
18 /*Let‘s fight!!!*/
19
20 const int INF = 0x3f3f3f3f;
21 const int MAX_N = 50050;
22 typedef pair<int, int> pii;
23 vector<pii> G[MAX_N];
24 int n, d[MAX_N];
25 bool inq[MAX_N];
26
27 void AddEdge(int u, int v, int w) {
28 G[u].push_back(pii(v, w));
29 }
30
31 void spfa(int s) {
32 queue<int> Q;
33 memset(d, 0x3f, sizeof(d));
34 memset(inq, false, sizeof(inq));
35 d[s] = 0;
36 inq[s] = true;
37 Q.push(s);
38 while (!Q.empty()) {
39 int u = Q.front(); Q.pop(); inq[u] = false;
40 for (int i = 0; i < G[u].size(); i++) {
41 int v = G[u][i].first, w = G[u][i].second;
42 if (d[u] + w < d[v]) {
43 d[v] = d[u] + w;
44 if (!inq[v]) Q.push(v), inq[v] = true;
45 }
46 }
47 }
48 }
49
50 int main(void) {
51 while (~scanf("%d", &n)) {
52 for (int i = 0; i <= 50005; i++) G[i].clear();
53 int s = INF, t = -1;
54 for (int i = 0; i < n; i++) {
55 int a, b, c;
56 scanf("%d %d %d", &a, &b, &c);
57 b++;
58 s = min(s, a); t = max(t, b);
59 AddEdge(b, a, -c);
60 }
61 for (int i = s; i < t; i++) AddEdge(i, i + 1, 1), AddEdge(i + 1, i, 0);
62
63 spfa(t);
64
65 printf("%d\n", -d[s]);
66 }
67
68 return 0;
69 }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/Stomach-ache/p/3936812.html