UVa 10020 Minimal coverage(欲張り&区間カバー)
10020-Minimal coverage
Time limit:3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_オンラインjudge&Itemid=8&category=113&page=show_problem&problem=961
The Problem
Given several segments of line(int the X axis)with coordinans[Li,Ri].You are to chose the minimal amount of them,such the y would copletely cover the segment[0,M].
The Input
The first line is the number of test cases,followed by a blank line.
Each test case in the input shout contains an integer M(1==M<=5000)、フォロワーby pairs“Li Ri”(124 Li|、124; Ri(=50000、i==100000)、each on a separate line.Eacterminates”
Each test case will be separated by a single line.
The Output
For each test case,in the first line of output your progrm shuld prininininininiml number of line segments ininininininininttttttttcan cover segment[0,M].In the follwing inininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininttttttttttttttdedededededededededededededests、thes、thes、thes、the the.If[0,M]can not be covered by given line segments、your program shoud print“0”。
Print a blank line between the outputts for two consecutive test cases.
Sample Input
問題のデータに欠陥があります。 if(nowL<M)cnt=0;この言葉はACにもなります。
完全コード:
Time limit:3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_オンラインjudge&Itemid=8&category=113&page=show_problem&problem=961
The Problem
Given several segments of line(int the X axis)with coordinans[Li,Ri].You are to chose the minimal amount of them,such the y would copletely cover the segment[0,M].
The Input
The first line is the number of test cases,followed by a blank line.
Each test case in the input shout contains an integer M(1==M<=5000)、フォロワーby pairs“Li Ri”(124 Li|、124; Ri(=50000、i==100000)、each on a separate line.Eacterminates”
Each test case will be separated by a single line.
The Output
For each test case,in the first line of output your progrm shuld prininininininiml number of line segments ininininininininttttttttcan cover segment[0,M].In the follwing inininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininttttttttttttttdedededededededededededededests、thes、thes、thes、the the.If[0,M]can not be covered by given line segments、your program shoud print“0”。
Print a blank line between the outputts for two consecutive test cases.
Sample Input
2
1
-1 0
-5 -3
2 5
0 0
1
-1 0
0 1
0 0
Sample Output0
1
0 1
『入門経典』P 154の経典欲張り。問題のデータに欠陥があります。 if(nowL<M)cnt=0;この言葉はACにもなります。
完全コード:
/*0.065s*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100005;
struct seg
{
int l, r;
bool operator < (const seg a) const
{
return l < a.l;
}
} p[N], ans[N];
int main()
{
int T, n, M, i, nowL, maxR, cnt, templ, tempr;
bool found;
scanf("%d", &T);
while (T--)
{
scanf("%d", &M);
n = 0;
while (scanf("%d%d", &templ, &tempr), templ || tempr)
if (templ < M && tempr) p[n++] = (seg){templ, tempr};///
sort(p, p + n);
for (i = nowL = cnt = 0; nowL < M; ++cnt)
{
found = false;
maxR = 0;
for (; i < n && p[i].l <= nowL; ++i)
if (p[i].r > maxR)
{
found = true;
maxR = p[i].r;///
ans[cnt] = p[i];
}
if (!found) break;
nowL = maxR;///
}
if (nowL < M) cnt = 0;/// ~
printf("%d
", cnt);
for (i = 0; i < cnt; i++)
printf("%d %d
", ans[i].l, ans[i].r);
if (T) putchar(10);
}
return 0;
}