HDOJ 2037 今年暑假不AC 【暴力解决】⊙﹏⊙b汗

来源:互联网 发布:人工智能 编程语言 编辑:程序博客网 时间:2024/05/17 09:25
#include <stdio.h>#include <algorithm>using namespace std;#define PLINE printf("\n------------------\n")struct time{int s,e;}t[101];int n, ans;bool cmp(struct time a, struct time b){return a.s < b.s;}void search(int d, int num, int e){if(d == n)//刚开始竟然把层数写错了,╮(╯▽╰)╭,以后还是搞统一吧{if(num > ans)ans = num;return;}if(t[d].s >= e)search(d+1, num+1, t[d].e);search(d+1, num, e);}int main(){int i;while(scanf("%d", &n) && n ){ans = 0;for(i=0; i<n; i++)scanf("%d%d", &t[i].s, &t[i].e);sort(t, t+n, cmp);search(0, 0, 0);printf("%d\n", ans);}return 0;}
//贪心解法include <stdio.h>#include <algorithm>using namespace std;#define PLINE printf("\n------------------\n")struct time{int s,e;}t[101];bool cmp(struct time a, struct time b){return a.e < b.e;}int main(){int i, n, ans, e;while(scanf("%d", &n) && n ){ans = 0;for(i=0; i<n; i++)scanf("%d%d", &t[i].s, &t[i].e);sort(t, t+n, cmp);e = 0;for(i=0; i<n; i++)if(t[i].s >= e){e = t[i].e;ans++;}printf("%d\n", ans);}return 0;}



原创粉丝点击