UVaLive 5845 Laptop (贪心)

题目

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3856

题意

笔记本一旦没有任务就会进入休眠,休眠到激活状态会消耗能量。
为了省电,要求休眠次数最少

解法

贪心,让更多的任务连续。
初次结束时间r为第一个任务的右端点,对于以后的每个任务,若左端点大于r,ans+1, 更新r,否则只更新r

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node {
    int x, y;
} p[100010];
int cmp(Node a, Node b) {
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}
int main() {
    int T;
    int n;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d %d", &p[i].x, &p[i].y);
        sort(p, p + n, cmp);
        int ans = 0;
        int l = p[0].x, r = p[0].y;
        for(int i = 1; i < n; i++) {
            if(p[i].x > r) {
                ans ++;
                r = p[i].y;
            }
            else
                r = min(r + 1, p[i].y);
        }
        printf("%d\n", ans);
    }
}

Source

2011 Asia Daejeon Regional Contest

posted @ 2015-08-16 23:40  ACM_Record  阅读(125)  评论(0编辑  收藏  举报