codeforces 479C Exams 排序贪心

来源:互联网 发布:淘宝新店铺怎么刷信誉 编辑:程序博客网 时间:2024/05/18 01:02

题目链接:http://codeforces.com/problemset/problem/479/C
题意:
给出n个考试,对于每一个考试有一个规定的考试时间ai,有一个可以提前进行考试的时间bi,(bi < ai),但是对于每一个考试,老师都只会在本子上记录下时间ai,想让本子上的时间是一个不降的序列。求最早多久能完成考试。
思路:
既然本子上的时间必须是不降的,那么考虑对ai进行排序,从小到大,如果ai相同的话,将bi从小到大排。
又因为需要尽早的完成考试,所以考虑每一个都尽量选取bi进行考试,如果这次的bi比上次的考试时间来的晚,就可以选取bi,否则只能选取ai。
第一场考试就肯定是选取bi的了,用一个temp记录本次考试的时间。

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;#define M 5009typedef struct{    int x,y;}node;node a[M];bool cmp(node a,node b){    if(a.x != b.x)    return a.x < b.x;    return a.y < b.y;}int n;int main(){    while(scanf("%d",&n)==1)    {        for(int i = 0;i < n;i++)        {            scanf("%d %d",&a[i].x,&a[i].y);        }        sort(a,a+n,cmp);        int ans = 0;        int temp = a[0].y;        for(int i = 1;i < n;i++)        {            if(a[i].y >= temp) //尽可能选取bi进行考试            {                temp = a[i].y;            }            else            {                temp = a[i].x;            }        }        printf("%d\n",temp);    }    return 0;}
0 0
原创粉丝点击