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;
}
结构体法:
题意:中文题不解释。首先是尽人皆知的贪心,用烂了。刚开始入门时候不懂,就跟着人模仿。今天(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 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- 2015-11-11 hdu新生赛 A题(AC)
- 2015-11-11 hdu新生赛 C题(结束后一发AC)
- 2015-11-11 hdu新生赛 E题(结束后一发AC)
- 2015-11-11 hdu新生赛 F题(结束后一发AC)
- hdu-5385