Line coverage, range choice of site, to explain the interval coverage greedy

First, the greedy introduction:

Minimum coins

There 1,2,5,10,20,50,100 seven kinds of face value of coins to pay the specified amount, and asked how to pay the minimum number of coins used.

This is a very routine of the problem, we immediately think of, as far as possible to use large denomination of coin, the coin will be able to pay as little as possible. This is the "greedy choice."

Second, the greedy - line coverage

topic

description

 

School chapel every day there are many activities planned time there is time for these activities will be a conflict need to choose some activities organized. Liu's job is to arrange the school chapel activities, arrange a maximum of one event every time. Liu now has a schedule of activities planned, he would like to arrange more activities as much as possible, I ask him how the arrangements.

Entry

 

The first row is an integer number m (m <100) indicates there are m sets of test data.

The first line of each test is an integer n (1 <n <10000) The test data indicates that there are n events.

Followed by n lines, each line has two positive integers Bi, Ei (0 <= Bi, Ei <10000), respectively, the start and end time of the i-th event (Bi <= Ei)

 

Export

 

For each group the number of active input, output up to be able to arrangements.

Sample input 1

2
2
1 10
10 11
3
1 10
10 11
11 20

 

Sample Output 1

1
2

 answer:

Is to give you some of the activities of the time, most of the activities you need to be held within the specified time range (the number of events to be held in maximum)

This time there are a number of activities to be undertaken bit longer, there are some activities that short time, this time we can follow each activity must first terminate the time from small to large.

After sorting finished only one time within this activity can be carried out before the end of the next activity, and end time can limit activity began after time we have chosen to be greater than it. We want to ensure that every activity undertaken time not too long, so when it was over the same time we must follow its start time descending order.

After sorting finished, activity No. 0 position we have held. Subsequent events and we have chosen before the event (this time is the number 0 events) compared to after the event if the start time is greater than 0 activity, it is confirmed that the next event to be held it. After the events then be compared with this event

Example Analysis:

3

1 10

10 11

11 20

After sorting:

1 10

10 11

11 20

No. 0 Events mandatory, so that the first number after the events and activities are relatively No. 0. Event start time because the first number is not greater than No. 0 end time. Therefore, this event is not held

And a second number comparing activities, the second number greater than the activity start time end time No. 0. So hold II activities. And the second number after the event to go and compare activities

Loop end! Output Answer 2

 

Code:

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 struct node
 6 {
 7     int s;
 8     int e;
 9 } v[10005];
10 bool cmp(node a,node b)
11 {
12     if(a.e==b.e)
13     {
14         return a.s>b.s;
15     }
16     return a.e<b.e;
17 }
18 int main()
19 {
20     int n,t,m,i,x,sum;
21     scanf("%d",&t);
22     while(t--)
23     {
24         scanf("%d",&n);
25         sum=1;
26         for(i=0; i<n; i++)
27         {
28             scanf("%d %d",&v[i].s,&v[i].e);
29         }
30         sort(v,v+n,cmp);
31         x=v[0].e;
32         for(i=1; i<n; i++)
33         {
34             if(v[i].s>x)
35             {
36                 sum++;
37                 x=v[i].e;
38             }
39         }
40         printf("%d\n",sum);
41     }
42     return 0;
43 }
View Code

 

 

三、贪心——区间选点

题目

描述

 

上数学课时,老师给了LYH一些闭区间,让他取尽量少的点,使得每个闭区间内至少有一个点。但是这几天LYH太忙了,你们帮帮他吗?

 

输入

 

多组测试数据。

每组数据先输入一个N,表示有N个闭区间(N≤100)。

接下来N行,每行输入两个数a,b(0≤a≤b≤100),表示区间的两个端点。

 

输出

 

输出一个整数,表示最少需要找几个点。

 

输入样例 1

4
1 5
2 4
1 4
2 3
3
1 2
3 4
5 6
1
2 2

输出样例 1

1
3
1

区间选点的话,我们可以以区间头来先排序。也可以先对区间尾进行排序。这里说对区间尾进行排序:

对区间尾进行从小到大排序,如果区间尾相同,我们可以对区间头任意排序,当然也可以不排序。

排序完之后0号区间是肯定要在里面建一个点,之后的区间和它(目前是0号)进行比较。如果之后的区间与这个区间有交集那就不需要添加新的点。否则就必须添加一个点且之后的比较要和这个新的区间进行比较

 样例解析:

4
1 5
2 4
1 4
2 3

排序后:

2 3

1 4

2 4

1 5

第0号区间必选,之后第一号区间1 4和第0号区间有重叠部分,所以不用加点。第二号区间也和第0号区间有重叠部分,所以也不用加点。第三号区间也是如此

所以最后答案是1

 

 代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 using namespace std;
 5 struct node
 6 {
 7     int s,e;
 8 }a[10005];
 9 bool cmp(node a,node b)
10 {
11     if(a.e==b.e)
12     {
13         return a.s>b.s;
14     }
15     return a.e<b.e;
16 }
17 int main()
18 {
19     int n;
20     while(~scanf("%d",&n))
21     {
22         for(int i=0;i<n;i++)
23         {
24             scanf("%d",&a[i].s);
25             scanf("%d",&a[i].e);
26         }
27         sort(a,a+n,cmp);
28         int ans=1,t=0;
29         for(int i=1;i<n;i++)
30         {
31             if(a[i].s>a[t].e)
32             {
33                 ans++;
34                 t=i;
35             }
36         }
37         printf("%d\n",ans);
38     }
39     return 0;
40 }
View Code

 

 

四、区间覆盖

题目

农夫约翰正在指派他的N头牛(1 <= N <= 25,000)中的一些来做谷仓周围的清洁工作。他总是希望有一头奶牛来做清洁工作,他把一天分成了T班(1 <= T <= 1,000,000),第一个是1班,最后一个是T班。
每头奶牛只能在一天中的某个时段进行清洁工作。任何被选来做清洁工作的奶牛将在她的整个休息期间工作。
你的工作是帮助农民John安排一些奶牛轮班,这样(i)每个轮班至少安排一头奶牛,(ii)清洁的奶牛越少越好。如果无法为每个班次分配一头奶牛,请打印-1。

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

可以先对区间头进行从小到大排序,在区间头一样的情况下我们要对区间尾从大到小排序。因为我们要保证我们选择的每一头牛工作时间尽可能地长

0号牛可以说我们一定会使用它,之后的牛如果和0号牛的工作时间有交集那我们就要从这些有交集的牛上面找出来工作终止时间最大的那个

 

 

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 struct node
 7 {
 8     int s;
 9     int e;
10 } a[25010];
11 bool cmp(node a,node b)
12 {
13     if(a.s==b.s)
14         return a.e>b.e;
15     return a.s<b.s;
16 }
17 int main()
18 {
19     int n,m;
20     scanf("%d%d",&n,&m);
21     for(int i=0;i<n;++i)
22     {
23         scanf("%d%d",&a[i].s,&a[i].e);
24     }
25     sort(a,a+n,cmp);
26     if(a[0].s!=1)
27     {
28         printf("-1\n");
29         return 0;
30     }
31     int ans=1,k=a[0].e,ke=0;
32     for(int i=1;i<n;++i)
33     {
34         if(a[i].s>k+1)
35         {
36             ans++;
37             k=ke;
38         }
39         if(a[i].s<=k+1)
40         {
41             ke=max(ke,a[i].e);
42             if(ke==m)
43             {
44                 k=ke;
45                 ans++;
46                 break;
47             }
48         }
49     }
50     if(k==m) printf("%d\n",ans);
51     else printf("-1\n");
52     return 0;
53 }
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11778750.html
Recommended