非洲小孩
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
家住非洲的小孩,都很黑。为什么呢?
第一,他们地处热带,太阳辐射严重。
第二,他们不经常洗澡。(常年缺水,怎么洗澡。)
现在,在一个非洲部落里,他们只有一个地方洗澡,并且,洗澡时间很短,瞬间有木有!!(这也是没有的办法,缺水啊!!)
每个小孩有一个时间段能够洗澡。并且,他们是可以一起洗的(不管你是男孩是女孩)。
那么,什么时间洗澡,谁应该来洗,由谁决定的呢?那必然是他们伟大的“澡”神啊。“澡”神有一个时间表,记录着该部落的小孩,什么时候段可以洗澡。现在,“澡”神要问你,一天内,他需要最少开启和关闭多少次洗澡的水龙头呢?因为,开启和关闭一次水龙头是非常的费力气的,即便,这也是瞬间完成的。
-
输入
-
多组数据
第一行一个0<n<=100。
接下来n行,每行一个时间段。H1H1:M1M1-H2H2:M2M2,24小时制。
保证该时间段是在一天之内的。但是,不保证,H1H1:M1M1先于H2H2:M2M2。
输出
- 题目描述,“澡”神最少需要开启和关闭多少次水龙头呢? 样例输入
-
1 00:12-12:12 2 00:12-12:12 14:00-12:00
样例输出
-
1 1
提示
Ps:开启和关闭为一次
没什么难的,就是题目坑!
思路:
我觉得是题意没有完全表达清楚,应该是在最省水的情况下开水龙头次数最少,即能一起洗的都一起洗。
比如你给的数据,事实上只要在0:13到10:00中开一次水龙头,两个小孩就能同时洗完,然后11点再开一次,另一个洗完(都是瞬间的...)。而不是一直开着等人来洗,不然每天早晨一开晚上一关不就行了...╮(╯▽╰)╭3
00:12-10:00
00:13-12:12
11:00-14:00
答案:2次
我的代码:
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; struct S { int L; int R; }a[110]; bool cmp(const S& z,const S& x) { return z.L<x.L||z.L==x.L&&z.R<x.R; } int main() { int n,i,sum,p,q; char str[15]; while (~scanf("%d",&n)) { sum=0; for (i=0;i<n;++i) { scanf("%s",str); q=(str[0]-'0')*600+(str[1]-'0')*60 +(str[3]-'0')*10+(str[4]-'0'); p=(str[6]-'0')*600+(str[7]-'0')*60 +(str[9]-'0')*10+(str[10]-'0'); if (q>p) { swap(q,p); } a[i].L=q; a[i].R=p; } sort(a,a+n,cmp); /* for (i=0;i<n;++i) { printf("%d %d\n",a[i].L,a[i].R); }*/ sum=1; i=0; while (a[i].L==0&&a[i].R==0) { i++; } p=a[i].R; for (i=1+i;i<n;++i) { if (p<a[i].L) { sum++; p=a[i].R; } else if (p>a[i].R) { p=a[i].R; } } printf("%d\n",sum); } return 0; }
标程:
#include<cstdio> #include<algorithm> struct time{ int x, y; bool operator < (const time& rhs) const{ return y < rhs.y; } }tim[110]; using namespace std; int main(){ int n, h1, h2, m1, m2; while(~scanf("%d", &n)){ for(int i = 0; i < n; i++){ scanf("%d:%d-%d:%d", &h1, &m1, &h2, &m2); tim[i].x = min(h1*60+m1, h2*60+m2); tim[i].y = max(h1*60+m1, h2*60+m2); } sort(tim, tim+n); int t = 0; int ma = -1; for(int i = 0; i < n; i++){ if(tim[i].x>ma){ ma = tim[i].y; t++; } } printf("%d\n", t); } return 0; }
-
多组数据