UVA - 1420 Priest John's Busiest Day

题目大意:有一个司仪,要主持 n 场婚礼,给出婚礼的起始时间和终止时间,每个婚礼需要超过一半的时间做为仪式,并且仪式不能终止。问说司仪能否主持 n 场婚礼。

解题思路:贪心,为了尽量主持多的婚礼,每场的仪式时间就一定要尽量短 d = (t - s) / 2 + 1,(因为必须大于一半,所以加 1)。然后按照每场婚礼可以最晚结束的时间排序 t - d,(因为要满足所有的婚礼,所以尽量解决早点的仪式,腾出时间来给后面的婚礼),维护一个占用时间值即可。

#include <cstdio>
#include <algorithm>
using namespace std;

struct Wedding {
    int S, T, D;
    bool operator < (const Wedding& a) const {
        return T - D < a.T - a.D;
    }
} W[100010];

bool judge(int N) {
    int cur = 0;
    for (int i = 0; i < N; i++) {
        cur = max(cur, W[i].S) + W[i].D;
        if (cur > W[i].T)
            return false;
    }
    return true;
}

int main() {
    int N;
    while (scanf("%d", &N) && N) {
        for (int i = 0; i < N; i++) {
            scanf("%d%d", &W[i].S, &W[i].T);
            W[i].D = (W[i].T - W[i].S) / 2 + 1;
        }
        sort(W, W + N);

        printf("%s\n", judge(N) ? "YES" : "NO");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UVA - 1420 Priest John's Busiest Day

时间: 2024-08-01 10:42:46

UVA - 1420 Priest John's Busiest Day的相关文章

HDU2491 Priest John&#39;s Busiest Day

题目链接 题意: 有n个人要进行乒乓球比赛,每个人都一个能力值,每个人出现的次序就是他们住的位置 现在要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间 问这种比赛一共可以进行多少次 思路: 用树状数组做,否则TLE,先从左到右扫一遍,计算每点左边大的个数和小的个数, 再从右到左扫一遍,计算每点右边大和小的个数,然后交叉相乘取和就可以了 代码如下: #include<cstdio> #include<cstring> #include<string

POJ 3683 Priest John&#39;s Busiest Day (2-SAT+输出可行解)

题目地址:POJ 3683 第一次做需要输出可行解的题目...大体思路是先用强连通来判断是否有可行解,然后用逆序建图,用拓扑排序来进行染色,然后输出可行解.具体思路见传送门 因为判断的时候少写了一个等号..检查了好长时间..sad... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #incl

POJ 3683(Priest John&#39;s Busiest Day-强连通分量解决2-SAT)[Template:2-SAT]

Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8144   Accepted: 2769   Special Judge Description John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old le

POJ 3683.Priest John&#39;s Busiest Day 2-SAT

Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10144   Accepted: 3472   Special Judge Description John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old l

POJ 3683 Priest John&#39;s Busiest Day(2-SAT)

[题目链接] http://poj.org/problem?id=3683 [题目大意] 每个婚礼有两个时段可以进行特别仪式,特别仪式必须要有神父在场, 神父只有一个,问是否能满足所有婚礼的需求, [题解] 因为两个时段必须要满足一个时段,所以如果一个时段被占用那么另一个时段必须被空出来, 我们根据各个婚礼两个时段之间的冲突关系建边,之后跑2-SAT,判断是否冲突, 若无冲突则输出方案. [代码] #include <cstdio> #include <algorithm> #in

POJ-3683 Priest John&#39;s Busiest Day

图论中的2-SAT.模板题. #include <cstdio> #include <cstdlib> #include <algorithm> #include <cctype> #include <cstring> #include <iostream> using namespace std; #define rep(i, l, r) for(int i=l; i<=r; i++) #define travel(x) fo

HDU 2491 Priest John&#39;s Busiest Day(贪心)

题目链接 题意: n场婚礼 给定每场婚礼的开始和结束时间,要求每场婚礼至少举行一半以上(不包括一半) 如果可行输出YES,否则输出NO 思路: 算出每场婚礼的至少要举行的时间t, 最早的结束时间mid 以最早结束时间mid为第一变量,开始时间s为第二变量从小到大排序 用cnt记录举办完每场婚礼的结束时间 分三种情况: ①加参前当婚礼的时间够不一半 ②果加参婚礼时婚礼已开始,结束时间就加上婚礼一半的时光 ③婚礼没开始,结束时间就是婚礼的最早结束时间mid 代码如下: #include<cstdio

图论(2-sat):Priest John&#39;s Busiest Day

Priest John's Busiest Day Description John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of

UVA1420 - Priest John&#39;s Busiest Day

题目链接 题意:牧师John能否安排到所有婚礼,使其为所有婚礼送上祝福(祝福时间大于婚礼从时间的一半). 思路:贪心,按照婚礼中间时间从小到大排序,尽量早结束婚礼祝福时间最晚开始(每个婚礼都有一个祝福时间最晚开始的时间)比较早的婚礼. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int