【HDU - 4509】湫湫系列故事——减肥记II(合并区间模板 or 离散化标记 or 线段树)

题干:

虽然制定了减肥食谱,但是湫湫显然克制不住吃货的本能,根本没有按照食谱行动! 
于是,结果显而易见… 
  但是没有什么能难倒高智商美女湫湫的,她决定另寻对策——吃没关系,咱吃进去再运动运动消耗掉不就好了? 
  湫湫在内心咆哮:“我真是天才啊~\(≧▽≦)/~” 

  可是,大家要知道,过年回家多忙啊——帮忙家里做大扫除,看电影,看小说,高中同学聚餐,初中同学聚餐,小学同学聚餐,吃东西,睡觉,吃东西,睡觉,吃东西,睡觉……所以锻炼得抽着时间来。 

  但是,湫湫实在太忙了,所以没时间去算一天有多少时间可以用于锻炼,现在她把每日行程告诉你,拜托你帮忙算算吧~ 

  皮埃斯:一天是24小时,每小时60分钟
Input输入数据包括多组测试用例。 
每组测试数据首先是一个整数n,表示当天有n件事要做。 
接下来n行,第i行是第i件事的开始时间和结束时间,时间格式为HH:MM。 

[Technical Specification] 
1. 1 <= n <= 500000 
2. 00 <= HH <= 23 
3. 00 <= MM <= 59 
Output请输出一个整数,即湫湫当天可以用于锻炼的时间(单位分钟)Sample Input
1
15:36 18:40
4
01:35 10:36
04:54 22:36
10:18 18:40
11:47 17:53
Sample Output
1256
179


解题报告:

    水题,包含一个合并区间的小模板,或者用开一个数组标记每个分钟是否有安排。后者比较好想但是用时稍长。


ac代码:(合并区间)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;struct Node {int s,e;} node[500000 + 5],nn[500000 + 5]; bool cmp(Node a,Node b) {return a.s<b.s;}
int main()
{int n;int cnt,maxx,minn;//int hour,minutes;int sum;while(~scanf("%d",&n) ) {sum=0;cnt=0;maxx=0;for(int i = 0 ; i<n; i++) {scanf("%d:%d",&hour,&minutes);node[i].s=hour*60+minutes;scanf("%d:%d",&hour,&minutes);node[i].e=hour*60+minutes;}sort(node,node+n,cmp);maxx=node[0].e;nn[cnt].s=node[0].s;nn[cnt].e=maxx;for(int i = 0; i<n; i++) {if(node[i].s > maxx) {nn[cnt++].e=maxx;//把maxx放入,并cnt++ nn[cnt].s=node[i].s;//把当前的start放入 maxx=node[i].e;nn[cnt].e=maxx;//为了防止i=n-1的情况(即读到最后一个node了),因为此时可能maxx可能还未更新到nn.e中 }if(node[i].e>maxx) {maxx=node[i].e;nn[cnt].e=maxx;}}for(int i = 0; i<=cnt; i++) {sum+=nn[i].e-nn[i].s;}printf("%d\n",24*60-sum);}return 0 ;} 


ac代码2:(标记法)

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main() 
{int t;while(~scanf("%d",&t)) {int ans[2000];memset(ans, 0, sizeof(ans));for(int i = 0; i < t; i++)  {int a, b, a1, b1;scanf("%d:%d", &a, &b);scanf("%d:%d", &a1, &b1);for(int j = a * 60 + b; j < a1 * 60 + b1; j++)ans[j] = 1;}int cnt= 0;for(int i = 0; i < 1440; i++)  {if(!ans[i])cnt++;}printf("%d\n",cnt);}return 0;}

更:

ac代码3:(好像还可以用线段树?贴的代码)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 1000000
#define eps 1e-8
#define MAXN (2000+10)
#define MAXM (100000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
struct Tree{int l, r, sum, lazy;
};
Tree tree[MAXN<<2];
void PushUp(int o){tree[o].sum = tree[ll].sum + tree[rr].sum;
}
void PushDown(int o)
{if(tree[o].lazy != -1){tree[ll].lazy = tree[rr].lazy = tree[o].lazy;tree[ll].sum = 0;tree[rr].sum = 0;tree[o].lazy = -1;}
}
void Build(int o, int l, int r)
{tree[o].l = l; tree[o].r = r;tree[o].sum = r - l + 1; tree[o].lazy = -1;if(l == r) return ;int mid = (l + r) >> 1;Build(lson); Build(rson);
}
void Update(int o, int L, int R)
{if(tree[o].l == L && tree[o].r == R){tree[o].lazy = 0;tree[o].sum = 0;return ;}PushDown(o);int mid = (tree[o].l + tree[o].r) >> 1;if(R <= mid) Update(ll, L, R);else if(L > mid) Update(rr, L, R);else {Update(ll, L, mid); Update(rr, mid+1, R);}PushUp(o);
}
int main()
{int n;while(Ri(n) != EOF){Build(1, 0, 1440-1);for(int i = 0; i < n; i++){int sh, sm, th, tm;scanf("%d:%d%d:%d", &sh, &sm, &th, &tm);int s = sh * 60 + sm;int t = th * 60 + tm - 1;if(s <= t) Update(1, s, t);}Pi(tree[1].sum);}return 0;
}

再附一个合并区间的错误代码:(感谢cxsys大佬发现的这个问题点,帮助更好的理解区间合并与贪心问题。)

他写的:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stdlib.h> 
using namespace std;struct node{int start_hour,start_mint,end_hour,end_mint;
};
bool cmp(node a,node b)
{if(a.start_hour==b.start_hour)return a.start_mint<b.start_mint;return a.start_hour<b.start_hour;
}
struct node t[500005];
int main()
{int n;while(scanf("%d",&n)!=EOF){memset(t,0,sizeof(t));int sum=0;for(int i=0;i<n;i++){scanf("%d:%d%d:%d",&t[i].start_hour,&t[i].start_mint,&t[i].end_hour,&t[i].end_mint);}sort(t,t+n,cmp);for(int i=0;i<n;i++){if(sum==0)sum=(t[i].end_hour-t[i].start_hour)*60+(t[i].end_mint-t[i].start_mint);if(t[i+1].start_hour*60+t[i+1].start_mint>t[i].end_hour*60+t[i].end_mint)sum=sum+(t[i+1].end_hour-t[i+1].start_hour)*60+(t[i+1].end_mint-t[i+1].start_mint);if(t[i+1].end_hour*60+t[i+1].end_mint>t[i].end_hour*60+t[i].end_mint)sum=sum+(t[i+1].end_hour-t[i].end_hour)*60+(t[i+1].end_mint-t[i].end_mint);if(t[i+1].end_hour*60+t[i+1].end_mint<t[i].end_hour*60+t[i].end_mint)continue;}	printf("%d\n",1440-sum);}return 0;
}

有点丑陋,不易阅读,我给他修改了下以后的代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stdlib.h> 
using namespace std;struct node{int start_hour,start_mint,end_hour,end_mint;int s,e; 
};
bool cmp(node a,node b)
{if(a.start_hour==b.start_hour)return a.start_mint<b.start_mint;return a.start_hour<b.start_hour;
}
struct node t[500005];
int main()
{int n;while(scanf("%d",&n)!=EOF){memset(t,0,sizeof(t));int sum=0;for(int i=0;i<n;i++){scanf("%d:%d%d:%d",&t[i].start_hour,&t[i].start_mint,&t[i].end_hour,&t[i].end_mint);t[i].s=t[i].start_hour*60+t[i].start_mint;t[i].e=t[i].end_hour*60+t[i].end_mint;}sort(t,t+n,cmp);for(int i=0;i<n;i++){if(sum==0)sum=t[i].e-t[i].s;if(t[i+1].s>=t[i].e)sum=sum+t[i+1].e-t[i+1].s;if(t[i+1].e>=t[i].e)sum=sum+(t[i+1].e-t[i].e;if(t[i+1].e<t[i].e)continue;}	printf("%d\n",1440-sum);}return 0;
}

比如一个例子:

这么算应该是4+0+0.5小时,但其实是4小时


总结:合并区间的模板可否在优化一下?


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442137.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 2093】 考试排名(排序+格式输出)

题干&#xff1a;C编程考试使用的实时提交系统&#xff0c;具有即时获得成绩排名的特点。它的功能是怎么实现的呢&#xff1f; 我们做好了题目的解答&#xff0c;提交之后&#xff0c;要么“AC”&#xff0c;要么错误&#xff0c;不管怎样错法&#xff0c;总是给你记上一笔&…

.net core 5 IIS Api网站部署需要注意(同.net 6)

应用程序池&#xff1a;.net clr 版本&#xff1a;无托管代码 2.安装.NET Core SDK和AspNetCoreModule托管模块 此工具要在官网直接下载即可

【HDU - 2087】 剪花布条(直接模拟 or KMP)

题干&#xff1a; 一块花布条&#xff0c;里面有些图案&#xff0c;另有一块直接可用的小饰条&#xff0c;里面也有一些图案。对于给定的花布条和小饰条&#xff0c;计算一下能从花布条中尽可能剪出几块小饰条来呢&#xff1f; Input输入中含有一些数据&#xff0c;分别是成…

webapi自宿主设置本地端口使用https协议

首先&#xff0c;你要申请证书&#xff0c;然后导入到证书里面&#xff1a; 具体步骤&#xff1a;运行–MMC命令&#xff0c;进入如下界面进行设置&#xff1a; 一直点下一步直到完成&#xff0c;然后将证书导入到个人里面 这个时候进入cmd程序运行如下命令&#xff1a; /…

第九届(2018)蓝桥杯 山东省赛解题报告(题目+分析+代码)

1标题&#xff1a;第几天 2000年的1月1日&#xff0c;是那一年的第1天。那么&#xff0c;2000年的5月4日&#xff0c;是那一年的第几天&#xff1f; 注意&#xff1a;需要提交的是一个整数&#xff0c;不要填写任何多余内容。 【答案】&#xff1a;125 2标题&#xff1a;明码 …

Windows下 Python3.7.0 运行环境的搭建 一套操作后就可以使用Python写代码啦~

1.下载Python for windows 废话不说&#xff0c;直接上网址&#xff1a;https://www.python.org/ftp/python/3.5.1/python-3.5.1.exe 2.安装Python for windows 运行安装文件之后&#xff0c;你会看到这个页面&#xff1a;不得不说Python 在 Windows平台下的安装比傻瓜式还傻瓜…

【HDU - 1326】Box of Bricks(模拟水题)

题干&#xff1a;Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. Look, Ive built a wall!, he tells his older sister Alice. Nah, you should make all stacks the same height. Then you …

微信公众平台网站开发JS_SDK遇到的bug——wx.config注册提示成功,但部分接口注册失败问题

1 2022-02-23 使用微信公众平台调用扫一扫接口&#xff0c;总是注册不成功 这是进行注册后成功注册的接口提示 尝试注册了以下接口 拥有相关权限 解决办法&#xff1a;猜测失败原因为&#xff1a;子界面进行注册&#xff0c;在父界面进行注册后&#xff0c;成功

c++ 低位在前 高位在后_A股市场:如果股票涨停后第二天“高开低走”,你知道怎么操作才能利益最大化吗?...

(本文由公众号越声策略(yslc188)整理&#xff0c;仅供参考&#xff0c;不构成操作建议。如自行操作&#xff0c;注意仓位控制和风险自负。)如果你的股票涨停后第二天高开低走&#xff0c; 后市怎么操作&#xff1f;简单来讲&#xff0c;高开低走就是开盘价高于上个交易日的收盘…

文件内容查找方式

第一种&#xff0c;使用windows自带的查找工具 搜索工具里面有”高级选项“&#xff0c;选择”文件内容“然后进行搜索即可 第二种&#xff0c;使用命令行 在需要进行搜索的文件夹下使用命令行&#xff1a; Get-ChildItem -Path F:\ -Recurse | Select-String -Pattern &qu…

【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法)

题干&#xff1a; Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5505 Accepted Submission(s): 3976 Problem Description The Head Elder of the tropical island of Lagrishan has a proble…

vscode中怎样格式化js代码_[VSCode插件推荐] Bracket Pair Colorizer: 为代码中的括号添上一抹亮色...

在代码编写过程中&#xff0c;各种括号 {[()]} 必不可少。然而&#xff0c;随着代码量的增加&#xff0c;你有没有因为括号的嵌套太多&#xff0c;而导致代码难以阅读&#xff1f;我们来看看下面的代码&#xff0c;在第三行代码的最后部分&#xff0c;连续出现了5个右括号&…

*【HDU - 1506】【POJ - 2559】Largest Rectangle in a Histogram(单调栈或动态规划)

题干&#xff1a; Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consist…

【基础知识】大数据组件HBase简述

HBase是一个开源的、面向列&#xff08;Column-Oriented&#xff09;、适合存储海量非结构化数据或半结构化数据的、具备高可靠性、高性能、可灵活扩展伸缩的、支持实时数据读写的分布式存储系统。 只是面向列&#xff0c;不是列式存储 mysql vs hbase vs clickhouse HMaster …

改变定时器获取传感器频度_广东梅州梅县压力传感器*校对

广东梅州梅县压力传感器*校对看门狗寄存器不会改变或改变不大&#xff0c;如果看门狗寄存器发生了改变或改变很大&#xff0c;则说明系统陷入“死循环”.需要进行出错处理。在工业应用中&#xff0c;严重的干扰有时会破坏中断方式控制字&#xff0c;关闭中断&#xff0c;造成看…

springboot 不响应字段为空_面试官扎心一问:Tomcat 在 SpringBoot 中是如何启动的?...

作者&#xff1a;木木匠 http://my.oschina.net/luozhou/blog/3088908前言我们知道 SpringBoot 给我们带来了一个全新的开发体验&#xff0c;我们可以直接把 web 程序达成 jar 包&#xff0c;直接启动&#xff0c;这就得益于 SpringBoot 内置了容器&#xff0c;可以直接启动&am…

a1708硬盘转接口_资讯:希捷上架新款银河Exos系列机械硬盘,15000转+SAS协议

今日最新消息&#xff0c;希捷上架一款新品希捷银河Exos系列机械硬盘。据悉这款硬盘采用了SAS协议&#xff0c;转速高达15000RPM&#xff0c;目前公布的售价600GB为1899元RMB。据官方介绍这款希捷银河Exos系列机械硬盘为2.5英寸&#xff0c;15mm的厚度&#xff0c;最高的转速可…

腐蚀单机怎么进_暖气片堵塞是什么原因?要怎么解决呢?

你知道散热器到底为什么堵塞吗&#xff1f;散热器堵塞了怎么办&#xff1f;下面和金旗舰散热器小编一起来看看吧~一、散热器堵塞怎么办首先&#xff0c;把进回水阀先全部关闭&#xff0c;用扳手将散热器的堵头轻轻拧开。这里需要注意的是&#xff0c;堵头对应的散热器下面要放一…

vspy如何在图形面板显示报文_设备实时状态监控:如何进行工业生产设备数据采集?...

设备实时状态监控&#xff1a;如何进行工业生产设备数据采集&#xff1f;数据采集(DAQ)&#xff0c;是指从传感器和其它待测设备等模拟和数字被测单元中自动采集非电量或者电量信号,送到上位机中进行分析&#xff0c;处理。慧都设备数据采集系统解决方案工业生产设备数据采集是…

Sql Server数据库设置一个账户只能看到一个数据库

1 新建登录名&#xff0c;注意不要设置用户映射&#xff0c;服务器角色只选择public&#xff08;默认必选&#xff0c;无法去掉&#xff0c;可以添加其他服务器角色&#xff0c;但是不要添加查看所有数据库的权限&#xff0c;接下来会去掉public的查看所有数据库权限&#xff0…