天天看点

hdu 2037 今年暑假不AC 贪心)分析代码

今年暑假不AC

分析

若有a、b两种方案,都观看了n个节目,但a方案的结束时间比b方案早,则a方案优于b方案。理由:在n个节目后,a可能比b都看节目,至少不会比b方案少。

当n=1时,……

当n=2时,……

……

代码

#include <bits/stdc++.h>
using namespace std;
#define MXN 110
int n, ans; 
struct sGame{int s, e;} g[MXN];
int main(){
    int tmp;
    while(scanf("%d", &n), n){
        for(int i = 1; i <= n; i++) scanf("%d %d", &g[i].s, &g[i].e);
        sort(g+1, g+n+1, [](sGame x, sGame y){return x.e < y.e;});
        ans = 1, tmp = g[1].e;
        for(int i = 2; i <= n; i++){
            if(g[i].s >= tmp) ans++, tmp = g[i].e;
        }
        printf("%d\n", ans);
    }
    return 0;
}
           

继续阅读