HDU 4509 湫湫系列故事——减肥记II
【摘要】 题目链接~~>
做题感悟:这题不错,如果你思维灵活 5 分钟左右就可以搞定,否则。
解题思路:这题可以用线段树,也可以用结构体,也可以用跟简单的方法,把24小时全分成分钟,然后统计。
代码(结构体):
#include<stdio.h>#include<iostream>#include<map>#include<str...
做题感悟:这题不错,如果你思维灵活 5 分钟左右就可以搞定,否则。
解题思路:这题可以用线段树,也可以用结构体,也可以用跟简单的方法,把24小时全分成分钟,然后统计。
代码(结构体):
-
#include<stdio.h>
-
#include<iostream>
-
#include<map>
-
#include<string>
-
#include<string.h>
-
#include<stdlib.h>
-
#include<math.h>
-
#include<vector>
-
#include<queue>
-
#include<algorithm>
-
using namespace std ;
-
int n ;
-
struct node
-
{
-
int x,y ;
-
}T[500005] ;
-
bool cmp(node a,node b)
-
{
-
if(a.x!=b.x)
-
return a.x < b.x ;
-
else return a.y < b.y ;
-
}
-
int main()
-
{
-
while(~scanf("%d",&n))
-
{
-
int r=0 ;
-
int h1,h2,m1,m2 ;
-
for(int i=0 ;i<n ;i++)
-
{ // 注意如果存在 23:00~01:11 这样的数据!!分开处理(本题没有)
-
scanf("%d:%d%d:%d",&h1,&m1,&h2,&m2) ;
-
T[r].x=h1*60+m1 ;
-
T[r++].y=h2*60+m2 ;
-
}
-
sort(T,T+r,cmp) ;
-
int sum=0,rt ;
-
sum+=T[0].y-T[0].x ;
-
rt=T[0].y ;
-
for(int i=1 ;i<r ;i++) // 合并
-
{
-
if(T[i].x>=rt)
-
{
-
sum+=T[i].y-T[i].x ;
-
rt=T[i].y ;
-
}
-
else if(T[i].x<rt&&T[i].y>rt)
-
{
-
sum+=T[i].y-rt ;
-
rt=T[i].y ;
-
}
-
}
-
printf("%d\n",24*60-sum) ;
-
}
-
return 0 ;
-
}
优代码:
-
#include<stdio.h>
-
#include<iostream>
-
#include<map>
-
#include<string>
-
#include<string.h>
-
#include<stdlib.h>
-
#include<math.h>
-
#include<vector>
-
#include<queue>
-
#include<algorithm>
-
using namespace std ;
-
bool vis[1450] ;
-
int main()
-
{
-
int n ;
-
while(~scanf("%d",&n))
-
{
-
memset(vis,false,sizeof(vis)) ;
-
int ans=24*60 ;
-
for(int i=0 ;i<n ;i++)
-
{
-
int h1,h2,m1,m2 ;
-
scanf("%d:%d%d:%d",&h1,&m1,&h2,&m2) ;
-
for(int i=h1*60+m1 ;i<h2*60+m2 ;i++)
-
if(!vis[i])
-
{
-
ans-- ;
-
vis[i]=true ;
-
}
-
}
-
printf("%d\n",ans) ;
-
}
-
return 0 ;
-
}
文章来源: blog.csdn.net,作者:Linux猿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/nyist_zxp/article/details/20230759
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)