您的位置:首页 > 其它

hdu2037 今年暑假不AC(贪心||暴力)

2016-02-05 18:48 357 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2037

题意:中文题不解释。首先是尽人皆知的贪心,用烂了。刚开始入门时候不懂,就跟着人模仿。今天(2016/10/22)学妹用暴搜写出来但没过,我看了看感觉思维没问题啊。这思路核心就是枚举时间,看代码都能看懂。想了一会这题的时间表示不是我们熟知的24小时啊!那就尽量往大开呗,好在貌似机器的测试数据不是很多,惊险A过= =

暴力法:

#include <iostream>
#include <cstdio>
#include <algorithm>

const int N = 1000000;

using namespace std;

struct node
{
int s,e;
}a
;

int main()
{
// freopen("in.txt", "r", stdin);
int n;
while(~scanf("%d", &n))
{
if(n == 0) break;
int num = 0;
int x, y;
for(int i = 0; i < n; i++)
{
scanf("%d%d", &x, &y);
// x = x%24;
// y = y%24;
a[i].s = x;
a[i].e = y;
}
int s = 0;
//开始时间为零,然后截止时间从1逐渐增加,
//若有等于en的数,则停止循环,以这一点的结束当下一个节目的起始时间
for(int i = 0; i <= N; i++)
{
for(int j = 0; j < n; j++)
{
if(a[j].e == i && a[j].s >= s)
{
num++;
s = i;
break;
}
}
}
printf("%d\n", num);
}
return 0;
}


结构体法:

#include <stdio.h>
#include <algorithm>

using namespace std;

const int N = 1005;
struct PRO
{
int pre;
int suc;
}pro
;

int cmp(PRO x, PRO y)
{
return (x.suc < y.suc) ? 1 : 0;
}

int main()
{
// freopen("in.txt", "r", stdin);
int n;
while(~scanf("%d", &n) && n)
{
for(int i = 0; i < n; i ++)
scanf("%d%d", &pro[i].pre, &pro[i].suc);
sort(pro, pro + n, cmp);
int start = 0;
int num = 0;
for(int i = 0; i < n; i ++)
{
if(pro[i].pre >= start)
{
num ++;
start = pro[i].suc;
}
}
printf("%d\n", num);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu