UVa-10020 minimal coverage **

Source: Internet
Author: User
1 /*
2 * minimum range coverage of Uva-10020-greedy
3 *
4 * very greedy .. For details, see Liu rujia's osai getting started classic.
5 *
6 * test data: http://www.algorithmist.com/index.php/UVa_10020#Input
7 *
8 */
9
10 # include <cstdio>
11 # include <algorithm>
12 using namespace STD;
13
14 const int maxn = 100000 + 5;
15 int casenum, M, intervalnum, anslist [maxn];
16
17 struct sdata {
18 int X, Y;
19 };
20 sdata interval [maxn];
21
22 bool CMP (const sdata LHS, const sdata RHs ){
23 return LHS. x <RHS. X;
24}
25
26 int main (){
27 scanf ("% d", & casenum );
28 while (casenum --){
29 scanf ("% d", & M );
30
31 intervalnum = 0;
32 int tmpx, tmpy;
33 while (scanf ("% d", & tmpx, & tmpy )){
34 if (tmpx = 0 & tmpy = 0)
35 break;
36
37 interval [intervalnum]. x = tmpx;
38 interval [intervalnum ++]. Y = tmpy;
39}
40
41 sort (interval, interval + intervalnum, CMP );
42
43 int cover = 0, cur = 0, nextcover, nextcovernum, I, ANS = 0;
44 bool covered = 0; // pay attention to initialization !! Wa once here
45 while (cur <intervalnum ){
46 nextcover = cover;
47 I = cur;
48 While (I <intervalnum & interval [I]. x <= cover ){
49 If (interval [I]. Y> nextcover ){
50 nextcover = interval [I]. Y;
51 nextcovernum = I;
52}
53 I ++;
54}
55 if (I = cur) break;
56
57 cover = nextcover;
58 anslist [ans ++] = nextcovernum;
59 cur = I;
60 if (cover> = m ){
61 covered = 1;
62 break;
63}
64}
65 if (covered ){
66 printf ("% d \ n", ANS );
67 for (Int J = 0; j <ans; j ++ ){
68 printf ("% d \ n", interval [anslist [J]. X, interval [anslist [J]. y );
69}
70}
71 else printf ("0 \ n ");
72 If (casenum> 0) printf ("\ n ");
73
74}
75
76
77 return 0;
78}

    

1 /*
2. If the range endpoint is an integer, you can store the data in arrays without sorting. The O (n) time can be used.
3: interval [I] = J, indicating that the maximum value of the right endpoint of the range with the left endpoint as I is J ..
4 */
5 # include <cstdio>
6 # include <cstring>
7 using namespace STD;
8
9 const int maxn = 50000 + 10;
10 int M, interval [maxn];
11
12 INT main (){
13 memset (interval,-1, sizeof (interval ));
14
15 scanf ("% d", & M );
16 int tmpa, tmpb;
17 while (scanf ("% d", & tmpa, & tmpb )){
18 if (tmpa = 0 & tmpb = 0) break;
19 if (tmpb <0 | tmpa> = m) continue;
20 if (tmpa <0) tmpa = 0;
21 if (interval [tmpa] <tmpb)
22 interval [tmpa] = tmpb;
23}
24
25 int cover = 0, cur = 0, I = 0, IMAX = 0, num = 0;
26 bool flag = 0;
27 while (I <m ){
28 flag = 0;
29 for (; I <= cover; I ++ ){
30 if (interval [I]> IMAX ){
31 IMAX = interval [I];
32 cur = I;
33}
34}
35 if (IMAX = cover) break;
36
37 cover = IMAX;
38 num ++;
39 if (cover> = m ){
40 flag = 1;
41 break;
42}
43}
44
45 if (FLAG) printf ("% d \ n", num );
46 else printf ("no solution \ n ");
47
48 return 0;
49}

 

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.