UVALive 7483 贪心策略

来源:互联网 发布:百度统计 js请求 编辑:程序博客网 时间:2024/06/04 18:43

题目如下(在线转换PDF到word这差距实在是太大了)

 

You are the boss of ACM (Air Conditioned Minions), an upstanding company with a single goal of

worlddomination.

The company has N minions. Each minion works evilly from early morning until evening inside

a super secret bunker in Helsinki. After much deliberation, you decided to move your headquarters

to Singapore. However, unlike Helsinki, Singapore is very hot, so the entire complex must be air

conditioned. Withstrenuousworkinghours(underminimumwagetoo),    itisimperativethatallyour

minions work under optimal work condition. In particular, all minions must like the temperatures of

theroomstheyarein.

You are planning to construct several rooms in your new hideout, and assign your minions there.

You fix the temperature of each room to any value you want (different rooms may have different

temperatures). Afteryoufixthetemperatures,youwillassigneachofyourminionstotheserooms(a

roomcanholdanynumberofminions).   Youwantallminionstolikethetemperaturesoftheirassigned

rooms. Eachminionlikesanintervaloftemperature,andthesepreferenceswillbegiventoyou.

Air conditioners are very expensive to maintain. Thus, you want to construct as few rooms as

possible. Whatistheminimumnumberofroomsyouneedtosetupsuchthatitwouldbepossibleto

assignminionstoroomsasdiscussedearlier?

Input

Theinputfilecontainsseveraltestcases,eachofthemasdescribedbelow.

The first line contains a non-negative integer 2≤N ≤100, giving the number of minions in your

company. The next N lines each describe the temperature preferences of all your minions. The i-th

lineconsistsoftwosinglespaceseparatedintegersLandU      (1≤L≤U  ≤2N),whichdenotesthatthe

i-thminionlikesanytemperaturebetweenLandU ,inclusively.

Output

Foreachcase,printanintegerdenotingtheminimumnumberofroomsyouneedtoconstructonaline

byitself.

Explanation:

In the first example, one of the possible solutions is to setup two rooms — one with temperature

2,andanotherwithtemperature5.   Thefirsttwominionscanbeassignedtothefirstroom,whilethe

thirdminioncanbeassignedtothesecondroom.

Sample Input

3

1 2

2 4

5 6

5

1 2

3 5

4 6

7 9

8 10

Sample Output

2

3

 

 


我认为这道题的套路是“贪心”。输入数据时需要处理一下,把每个区间的起点挂在一起,然后从第一个节点开始遍历,寻找挂载的最低节点,并且更新。同时如果这个区域和临近的区域相重叠则自增计数器,并且更新标准值为INF。

AC代码如下 View Source On GitHub

/**LINK=http://acm.hust.edu.cn/vjudge/contest/view.action?cid=115784#problem/F*/#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <vector>#include <algorithm>using namespace std;#define MAXN 105int main(){    int n;    while(scanf("%d",&n)==1)    {        vector<int> pool[MAXN*2+8];        for(int i=0;i<n;i++)        {            int a,b;            scanf("%d %d",&a,&b);            pool[a].push_back(b);        }        int k=INT_MAX;        int cnt=0;        for(int i=0;i<MAXN*2+8;i++)        {            if(k<i)            {                ++cnt;                k=INT_MAX;            }            for(int j=0;j<pool[i].size();j++)            {                k=min(k,pool[i].at(j));            }        }        if(k<INT_MAX)        {            ++cnt;        }        printf("%d\n",cnt);    }    /// End of main loop    return 0;}
另外我还想用一种朴素的做法解这道题,但是一直WA就不贴代码了。


我在GitHub上建立了一个仓库,用于存放已经AC的题目的源代码。如果各位有未收录的题目或者有更好的解法,欢迎fork仓库+PR~ 让我们共同创建一个AC代码集中仓库,造福ACM Beginner ~

仓库地址: OJ-Problems-Source On GitHub



0 0