HDU 2037 - 今年暑假不AC(贪心 + 动态)
2013-11-28 13:28
489 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2037
贪心算法:
动态规划:
贪心算法:
#include<iostream> #include<algorithm> using namespace std; struct TV{ int begin; int end; }Tv[105]; bool cmp(TV a,TV b) { return a.end < b.end; } int main() { int n, i; while(cin >> n && n != 0) { int count = 1; for(i = 0; i < n; i++) { cin >> Tv[i].begin >> Tv[i].end; } sort(Tv,Tv+n,cmp); int End=Tv[0].end; for(i=1; i<n; i++) { if(Tv[i].begin >= End) { count++; End = Tv[i].end; } } cout << count << endl; } return 0; }
动态规划:
#include <stdio.h> #include <stdlib.h> struct c { int x; int y; int ord; }d[100]; int cmp(const struct c *a, const struct c *b) { if ((*a).x == (*b).x) return (*a).y - (*b).y; else return (*a).x - (*b).x; } int main(void) { int i, j, n, max; while (scanf("%d", &n), n) { for (max = i = 0; i < n; i++) { scanf("%d%d", &d[i].x, &d[i].y); d[i].ord = 1; } qsort(d, n, sizeof(struct c), cmp); d[n-1].ord = 1; for (i = n - 2; i >= 0; i--) { for (j = i + 1; j < n; j++) { if (d[i].y <= d[j].x && d[i].ord < d[j].ord + 1) d[i].ord = d[j].ord + 1; } if (max < d[i].ord) max = d[i].ord; } printf("%d\n", max); } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- zoj3549 快速幂
- NWERC2010 NKOJ2178 Stock Prices
- 2011ACM福州网络预选赛B题 HDU4062 Abalone
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- POJ挑战赛3(POJ Challenge Round 3)题解
- ACM常用算法
- 2013 Multi-University Training Contest 1
- ACM/ICPC需要掌握的知识[转载]
- 北大—1006——Biorhythms
- ZOJ Problem Set - 1711 解题报告
- ZOJ Problem Set - 2412解题报告