uvalive 4328(贪心)
2015-03-25 19:00
405 查看
题意:有n对夫妻要举行婚礼,然后一个牧师需要在所有婚礼上举行一个仪式,每个仪式必须占用婚礼一半以上的时间且不能被打断,给出了n个婚礼的开始和结束时间,问是否可以让牧师在所有婚礼都举行仪式。
题解:先按婚礼起始时间排序,然后遍历所有婚礼看是否有足够时间举行仪式。
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 100005;
struct Couple {
int s, t, d;
}cou
;
int n;
bool cmp(Couple a, Couple b) {
return a.s + a.d < b.s + b.d;
}
bool judge() {
int ss = cou[0].s;
for (int i = 0; i < n - 1; i++) {
int temp = ss + cou[i].d;
if (temp >= cou[i + 1].s && temp + cou[i + 1].d <= cou[i + 1].t)
ss = temp;
else if (temp < cou[i + 1].s)
ss = cou[i + 1].s;
else
return false;
}
return true;
}
int main() {
while (scanf("%d", &n) && n) {
for (int i = 0; i < n; i++) {
scanf("%d%d", &cou[i].s, &cou[i].t);
cou[i].d = (cou[i].t - cou[i].s) / 2 + 1;
}
sort(cou, cou + n, cmp);
if (judge())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
题解:先按婚礼起始时间排序,然后遍历所有婚礼看是否有足够时间举行仪式。
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 100005;
struct Couple {
int s, t, d;
}cou
;
int n;
bool cmp(Couple a, Couple b) {
return a.s + a.d < b.s + b.d;
}
bool judge() {
int ss = cou[0].s;
for (int i = 0; i < n - 1; i++) {
int temp = ss + cou[i].d;
if (temp >= cou[i + 1].s && temp + cou[i + 1].d <= cou[i + 1].t)
ss = temp;
else if (temp < cou[i + 1].s)
ss = cou[i + 1].s;
else
return false;
}
return true;
}
int main() {
while (scanf("%d", &n) && n) {
for (int i = 0; i < n; i++) {
scanf("%d%d", &cou[i].s, &cou[i].t);
cou[i].d = (cou[i].t - cou[i].s) / 2 + 1;
}
sort(cou, cou + n, cmp);
if (judge())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
相关文章推荐
- UVALive - 4328 Priest John's Busiest Day 贪心
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- uvalive4254(贪心+优先队列)
- UVALive 7147 World Cup 【贪心】
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- UVALive 3959 Rectangular Polygons (排序贪心)
- UVALive - 7464 Robots 贪心+递归
- UVAlive 2326 Moving Tables(贪心 + 区间问题)
- UVALive 3177 && uva 1335 Beijing Guards (贪心+二分)
- uvalive 4254 Processor处理器 (二分模拟+贪心)
- uvalive 2322 Wooden Sticks(贪心)
- UVALive 3530 Martian Mining(贪心,dp)
- UVALive 6588 Crane (思维+贪心) -- 解题报告
- UVALive - 3507 Keep the Customer Satisfied 贪心
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- UVaLive 6609 Meeting Room Arrangement (贪心,区间不相交)
- UVALive 2757 Supermarket(贪心)
- UVALive - 7752 Free Figurines (Gym - 101173F ) 贪心(套娃嵌套)
- UVALive 6424 Russian Dolls 贪心