UVALive 4244 Party Party Party(HDU 2779 && Sicily 1663)
2015-02-23 20:41
721 查看
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
Emma has just graduated high school and it is the custom for the new graduates to throw parties for themselves and invite everyone in school to attend. Naturally, Emma wishes to attend as many parties as possible. This is notsuch a problem on a weekday since usually there are only two or three parties in the evening. But, Saturdays are packed! Typically some parties start at 8 AM (breakfast is served) while others might end at midnight (much to the annoyance of the neighbors).
Emma naturally wants to know how many parties she can attend.
Each party has a starting and stopping time, which are on the hour. These are listed via a 24-hour clock. For example, a party might start at 10 AM (10) and end at 2 PM (14). The earliest a party can start is 8 AM (8) and the
latest it can end is midnight (24). In order not to be rude, Emma stays at each party at least one half hour and will consider traveling time between parties to be instantaneous. If there are times during the day when there are no parties to attend, she'll
simply go home and rest.
Input
There will be multiple test cases. Each test case starts with a line containing an integerp (≤100) indicating the number of parties on the given day. (A value of
p = 0 indicates end of input.) The following
p lines are each of the form s e , both integers where
8≤s < e≤24 , indicating a party that starts at time
s and ends at time e . Note there may be multiple parties with the same starting and ending time.
Output
For each input set output a line of the formOn day
d Emma can attend as many as n parties.
where you determine the value of
n and d is the number of the test case starting at 1.
Sample Input
8 12 13 13 14 12 13 9 10 9 10 12 13 12 14 9 11 3 14 15 14 15 14 15 0
Sample Output
On day 1 Emma can attend as many as 7 parties. On day 2 Emma can attend as many as 2 parties.
Solution
题意是给出n个party的开始和结束时间,至少在每个party呆半个小时,求最多可以参加多少个party。
可以比较容易的想到是简单的贪心,按party时间从短到长排序,依次去参加每一个。
注意在时间上做标记。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s[105], e[105], r[105];
bool times[105];
bool cmp(const int a, const int b)
{
return e[a] - s[a] < e[b] - s[b];
}
int main()
{
int p, ca = 0;
while (scanf("%d", &p) == 1 && p)
{
int i, j, c = 0;
memset(times, 0, sizeof(times));
for (i = 0; i < p; ++i)
{
scanf("%d%d", &s[i], &e[i]);
//方便给时间做标记,第n个半小时
s[i] *= 2;
e[i] *= 2;
r[i] = i;
}
sort(r, r + p, cmp);
for (i = 0; i < p; ++i)
{
int t = r[i];
for (j = s[t]+1; j <= e[t]; ++j) if (!times[j])//第j个半小时是空闲的
{
times[j] = true;
++c;
break;
}
}
printf("On day %d Emma can attend as many as %d parties.\n", ++ca, c);
}
return 0;
}
相关文章推荐
- HDU 5150 && UVALive 5061 (LCA标记)
- HDU 3666&& UVALive - 5094 THE MATRIX PROBLEM(stack判负环,差分约束)
- UVALive 5004 Balanced Number && hdu-3967 Zero's Number(数位dp)
- UVALive 5717 & HDU 4085 Peach Blossom Spring(DP+斯坦纳树)
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
- POJ 3342 Party at Hali-Bula / HDU 2412 / UVAlive 3794 / UVA 1220
- UVa 301 & HDU 1456 & POJ 1040 - Transportation
- ACM->uvalive->Sentry Robots(二分图匹配的最小点覆盖)
- UVa 11054/HDU 1489/POJ 2940 Wine trading in Gergovia(贪心&双向队列)
- UVA 12103/poj 3128/hdu 1975/UVALive 3641 Leonardo's Notebook 置换
- UVa 10557 & HDU 1317 - XYZZY
- 字符->UVALive 4773 YY and YY Again
- DP->UVALive 4764
- UVALive 3938 "Ray, Pass me the dishes!"
- Hdu 3234 & Uva 12232 Exclusive-OR
- uva 1421 & uvalive 4253 Archery(二分)
- UVALive - 3938 "Ray, Pass me the dishes!"
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)
- Splay Tree (POJ 3580 & UVA 12356 & HDU 1890)
- UVALive 4327 Parade(hdu 2490 Parade)