您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU2037 ACM