Week14 - 程序设计思维与实践 - HDU3700 - Cat(限时模拟)

2023-05-16

Problem Description

There is a cat, cat likes to sleep.
If he sleeps, he sleeps continuously no less than A hours.
For some strange reason, the cat can not stay awake continuously more than B hours.
The cat is lazy, it could sleep all the time,
but sometimes interesting events occur(such as cat food, TV show, etc) .
The cat loves these events very much.
So, Please help the cat plan their day so as not to miss any interesting events.
Every day the cat wants to live on the same schedule.

Input

The first line of the input file contains two integers A and B (1 <= A, B <= 24).
The second line of the input file contains the number n, the number of interesting events (1 <= n <= 20).
Following n rows describe the interesting events.
Each event is described line of the form hh:mm-hh:mm, which specifies
the time period during which it occurs. Time varies from 00:00 to 23:59.
No two interesting events will overlap.
If the event is completed earlier than the beginning, This means that it captures midnight.
The event is considered to be occupying the whole minute,
when it begins and the moment when it ends (event 12:00-13:59 lasted exactly 120 minutes). Start time and time end of the event are different.

Output

If the cat can organize their day so that during all the interesting events not sleep, print to output file Yes.
On the second line Bring k - how many times a day cat should go to bed.
In the following k rows Bring out the intervals in which the cat sleeps in the same format, in which interesting events are set in the input file. If making a few, display any.
If the cat can not organize their day desired way, print to the output file No.

Sample Input

12 12
1
23:00-01:00
3 4
3
07:00-08:00
11:00-11:09
19:00-19:59

Sample Output

Yes
1
01:07-22:13
No

思路

这道题的思路比较简单,由于输入的时间表是保证不重叠的且数据量比较少,逐个枚举判断即可

  • 设置一个变量来存储循环过程中连续工作的时间
  • 每次遍历到一个节目就将该节目的时长加入到连续工作的时间当中,若大于 B 则凉凉了,直接退出
  • 当前节目的结束与下一个节目的开始这段时间若大于等于 A 则可以睡觉,否则加入连续工作的时间
  • 若小于 A 则清空连续工作的时间
  • 其余的只需要注意跨夜的判断以及时间为闭区间

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
 
const int day=24*60;
struct node{
    int s,e;
    node(){}
    node(int ss,int ee):s(ss),e(ee){} 
    bool operator<(const node &t)const{return s<t.s;}
}a[30];
 
vector<node> res;
int A,B,N,sum_1,sum_2;

void Output(node t){
    printf("%d%d:%d%d-%d%d:%d%d\n", 
			t.s/600, t.s/60%10, t.s%60/10, t.s%10, 
			t.e/600, t.e/60%10, t.e%60/10, t.e%10);
}
 
int main()
{	
    while(~scanf("%d%d%d", &A, &B,&N)){
        A*=60;
        B*=60;
        bool flag=true;
        for (int i=1; i<=N; i++){
        	int h1,m1,h2,m2;
			scanf("%02d:%02d-%02d:%02d",&h1,&m1,&h2,&m2);
            a[i].s=h1*60+m1;a[i].e=h2*60+m2;
            if(a[i].e<a[i].s) a[i].e+=day;
   			if(a[i].e-a[i].s+1>B) flag=false;        
        }
        if(!flag){
        	printf("No\n");
        	continue;
		}
        sort(a+1,a+N+1);
        a[N+1].s=a[N].e+1;
        a[N+1].e=a[1].s+day-1;
		res.clear();
		sum_1=0,sum_2=0;
		for(int i=1;i<=N;i++){
			sum_1+=a[i].e-a[i].s+1;
			if(sum_1>B){
				flag=false;
				break;
			}
			if(i<N)
				sum_2=a[i+1].s-a[i].e-1;
			else
				sum_2=a[i+1].e-a[i].e;
			if(sum_2>=A){
				sum_1=0;
				if(i<N)
					res.push_back(node((a[i].e+1)%day,(a[i+1].s-1)%day));
				else
					res.push_back(node((a[i].e+1)%day,(a[i+1].e)%day));
			} 
			else{
				sum_1+=sum_2;
				if(sum_1>B){
					flag=false;
					break;
				}
			}
		}
		if(flag&&res.size()>0){
			printf("Yes\n%d\n", res.size());
			for(int i=0;i<res.size();i++)
				Output(res[i]);
		}
		else
			printf("No\n");
    }
    return 0;
}

限时测试的时候写的是下面这份代码,没看出来哪里有问题,只好放弃debug重写了上面那一份,我感觉可能是 day 那里没处理好…

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

struct node{
	int day,sh,sm,eh,em;
	node(){}
	node(int aa,int bb,int cc,int dd):sh(aa),sm(bb),eh(cc),em(dd){day=0;}
	node(int aa,int bb,int cc,int dd,int ee):sh(aa),sm(bb),eh(cc),em(dd){day=ee;}
	bool operator<(const node &s) const{
		if(day!=s.day)
			return day<s.day;
		return (sh*60+sm)<(s.sh*60+s.sm);
	}
}a[30];
vector<node> res;
int A,B,N;

void addminute(int &h_1,int &m_1){
	if(m_1+1>=60){
		m_1=m_1+1-60;
		h_1++;
		if(h_1>23){
			h_1=0;
		} 
	}
	else
		m_1++;
}

void subminute(int &h_2,int &m_2){
	if(m_2-1<0){
		h_2--;
		m_2=m_2-1+60;
		if(h_2<0){
			h_2=23;
		}
	}
	else
		m_2--;	
}

int sumtime_1(int sh,int sm,int eh,int em){
	int s,e,ans;
	if(eh*60+em<sh*60+sm) eh+=24;
	s=sh*60+sm;
	e=eh*60+em;
	ans=e-s+1;
	return ans;
}

int sumtime_2(int sh,int sm,int eh,int em){
	addminute(sh,sm);
	subminute(eh,em);
	return sumtime_1(sh,sm,eh,em);
}

int main()
{
	while(~scanf("%d%d%d",&A,&B,&N)){
		res.clear();
		A=A*60;B=B*60;
		bool flag=true;
		for(int i=1;i<=N;i++){
			scanf("%d:%d-%d:%d",&a[i].sh,&a[i].sm,&a[i].eh,&a[i].em);	
			if(sumtime_1(a[i].sh,a[i].sm,a[i].eh,a[i].em)>B) flag=false;	
			if(a[i].sh*60+a[i].sm>a[i].eh*60+a[i].em)
				a[i].day=2;	
			else
				a[i].day=1;	
		}
		if(!flag){
			printf("No\n");
			continue;
		}
		sort(a+1,a+1+N);
		int h_1=a[N].eh,m_1=a[N].em,h_2=a[1].sh,m_2=a[1].sm;
		addminute(h_1,m_1);
		subminute(h_2,m_2);
		a[N+1]=node(h_1,m_1,h_2,m_2);
		int sum_1=0,sum_2=0;
		for(int i=1;i<=N;i++){
			sum_1+=sumtime_1(a[i].sh,a[i].sm,a[i].eh,a[i].em);
			if(sum_1>B){
				flag=false;
				break;
			}
			if(i<N)
				sum_2=sumtime_2(a[i].eh,a[i].em,a[i+1].sh,a[i+1].sm);
			else
				sum_2=sumtime_2(a[i].eh,a[i].em,a[i+1].eh,a[i+1].em)+1;
			if(sum_2>=A){
				int hh_1=a[i].eh,mm_1=a[i].em;
				addminute(hh_1,mm_1);
				if(i<N){
					int hh_2=a[i+1].sh,mm_2=a[i+1].sm;
					subminute(hh_2,mm_2);
					res.push_back(node(hh_1,mm_1,hh_2,mm_2));
				}						
				else
					res.push_back(node(hh_1,mm_1,a[i+1].eh,a[i+1].em));
				sum_1=0;
			} 
			else{
				sum_1+=sum_2;
				if(sum_1>B){
					flag=false;
					break;
				}
			}
		}
		if(flag&&res.size()>0){
			printf("Yes\n");
			printf("%d\n",res.size());
			for(int i=0;i<res.size();i++)
				printf("%02d:%02d-%02d:%02d\n",res[i].sh,res[i].sm,res[i].eh,res[i].em);
		}
		else{
			printf("No\n");
		}
	}
	return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Week14 - 程序设计思维与实践 - HDU3700 - Cat(限时模拟) 的相关文章

  • [week9]签到题(长凳)——贪心算法

    文章目录 题意InputOutput输入样例输出样例提示 分析总结代码 题意 SDUQD 旁边的滨海公园有 x 条长凳 第 i 个长凳上坐着 a i 个人 这时候又有 y 个人将来到公园 xff0c 他们将选择坐在某些公园中的长凳上 xff
  • [week14] Q老师与十字叉

    文章目录 题意InputOutput输入样例输出样例提示 分析总结代码 题意 Q老师 得到一张 n 行 m 列的网格图 xff0c 上面每一个格子要么是白色的要么是黑色的 Q老师认为失去了 十字叉 的网格图莫得灵魂 一个十字叉可以用一个数对
  • [week15] ZJM 与霍格沃兹 —— 字符串哈希

    文章目录 题意InputOutput输入样例输出样例提示 分析总结代码 题意 ZJM 为了准备霍格沃兹的期末考试 xff0c 决心背魔咒词典 xff0c 一举拿下咒语翻译题 题库格式 xff1a 魔咒 对应功能 背完题库后 xff0c ZJ
  • [week14] D - Q老师染砖(选做) —— 矩阵快速幂优化DP

    文章目录 题意InputOutput输入样例输出样例提示 分析总结代码 题意 衣食无忧的 Q老师 有一天突发奇想 xff0c 想要去感受一下劳动人民的艰苦生活 具体工作是这样的 xff0c 有 N 块砖排成一排染色 xff0c 每一块砖需要
  • [week14] E - Q老师度假(选做)—— 矩阵快速幂优化DP(拓展)

    文章目录 题意InputOutput输入样例输出样例提示 分析总结代码 题意 忙碌了一个学期的 Q老师 决定奖励自己 N 天假期 假期中不同的穿衣方式会有不同的快乐值 已知 Q老师 一共有 M 件衬衫 xff0c 且如果昨天穿的是衬衫 A
  • [week15] B - ZJM与生日礼物(选做)—— 字典树

    文章目录 题意InputOutput输入样例输出样例提示 分析总结代码 题意 ZJM 收到了 Q老师 送来的生日礼物 xff0c 但是被 Q老师 加密了 只有 ZJM 能够回答对 Q老师 的问题 xff0c Q老师 才会把密码告诉 ZJM
  • [week15] C - ZJM与纸条(选做)—— KMP算法

    文章目录 题意InputOutput输入样例输出样例提示 分析总结代码 题意 ZJM 的女朋友是一个书法家 xff0c 喜欢写一些好看的英文书法 有一天 ZJM 拿到了她写的纸条 xff0c 纸条上的字暗示了 ZJM 的女朋友 想给 ZJM
  • 51按键外部中断控制流水灯

    实验二 外部按键输入 一 实验目的 1 了解单片机检测口方法 2 了解单片机外部中断原理 3 了解按键输入原理 二 实验内容 1 完成按键扫描控制流水灯 2 完成按键外部中断控制流水灯 三 实验原理 四 实验电路与程序 1 软件实验一 xf
  • 树莓派4B构建debian镜像UEFI启动

    树莓派4B构建debian镜像UEFI启动 前言 今天按照大佬的博客树莓派俱乐部官方 Debian 系统镜像 支持UEFI跑了遍 完整的UEFI镜像构建过程 包括镜像分区 挂载 xff0c 根文件系统的制作 xff0c 内核的移植 xff0
  • Linux修改主机名问题

    记一次修改主机名不成功原因 场景 虽然使用hostname命令可以修改主机名 xff0c 但如果重启主机之后主机名还会变为之前的 xff0c 所以需要把修改的主机名称写到配置文件中 假设修改后的主机名为 new hostname 1 修改配
  • mybatisPlus分页插件报错,sql后面拼接多了一个limit。

    原本 用的mybatisPlus版本为3 1 0 xff0c 后来升级到3 4 2了 xff0c 使用分页的时候报错 解决 xff1a mybatisPlus 3 1 0 所用到的分页插件为 而mybatisPlus 3 4 2版本pagi
  • Deep Knowledge Tracing (深度知识追踪)

    boss又让我看这块的内容了 xff0c 刚开学 xff0c 还不太适应实验室的学习生活 xff0c 假期闲散惯了操 目录 1 概述2 表示3 1 DKT的优势3 2 DKT的不足4 模型5 序列的输入和输出输入输出 6 优化及应用7 三个
  • C程序代码

    一 C语言概述有算法 1 输出一行信息 span class token macro property span class token directive hash span span class token directive keyw
  • 【C语言-10】.求10 个整数中最大值。 (数组定义法和函数调用法)

    数组定义法 首先定义一个一维数组存放输入的数字 xff0c 然后将键盘输入的数字依次存入一维数组 xff1b 假定数组中某一元素为最大值 xff0c 将其与其他元素逐一比较 xff0c 得到最大的数为max值 xff1b 最后得到的max为
  • 【工程实践】解决 nvcc: command not found

    1 nvcc nvcc 是The main wrapper for the NVIDIA CUDA Compiler suite Used to compile and link both host and gpu code NVIDIA
  • hdu 5119(dp题)

    题目链接 xff1a http acm hdu edu cn showproblem php pid 61 5119 题目 xff1a Matt has N friends They are playing a game together
  • word(doc/docx)转markdown:使用Typora的插件

    打开你的Typora xff0c 选择文件 gt 导入 第一次导入会让你下载 pandoc 插件 下载链接如下 xff1a https github com jgm pandoc releases download 2 14 1 pando
  • 案例描述:update中,MySQL inner join 和 left join的区别,小结果集驱动大结果集

    场景描述 以一个场景为例 xff1a 单据A xff1a 下游子表 xff08 数据量级小 xff09 单据B xff1a 下游主表 xff08 数据量级小 xff09 单据C xff1a 中游子表 xff08 数据量级小 xff09 单据
  • Hadoop生态圈(一)- Hadoop详解

    目录 前言1 Hadoop概述1 1 Hadoop是什么1 2 Hadoop发展简史1 2 Hadoop三大发行版本1 3 Hadoop优势1 4 Hadoop的组成1 4 1 Hadoop1 x 2 x 3 x区别1 4 2 HDFS架构
  • arduino硬件总结

    文章目录 arduino硬件总结串口通讯I2CSPI中断函数基本了解实现测速 ADC读取光敏传感器的值 pwm舵机控制 arduino硬件总结 arduino 支持中断 xff0c ADC PWM xff0c I2C xff0c spi x

随机推荐

  • 文件上传 - Apache SSI远程命令执行

    文章目录 一 漏洞原理二 漏洞场景 挖掘思路三 触发条件四 漏洞复现4 1 启动环境4 2 访问环境4 3 复现过程 五 防御措施 一 漏洞原理 在测试任意文件上传漏洞的时候 xff0c 目标服务端可能不允许上传php jsp asp后缀的
  • Linux:chmod -R 777 *含义

    Linux xff1a chmod R 777 首先 xff0c chmod命令是linux上用于改变权限的命令 xff0c R 是递归遍历子目录 xff0c 因为你要操作的文件使用的 通配符 777 xff0c 第一个7代表文件所属者的权
  • STM32HAL库学习笔记七——I2C通信

    HAL库快速部署I2C 本文主要介绍如何使用STM32CubeMX快速部署I2C通信 xff0c 并与EEPROM进行数据收发 文章目录 HAL库快速部署I2CI2C简介EEPROM简介HAL库部署IIC通信实现多字节写入一 CubeMX配
  • python报错Statements must be separated by newlines or semicolons解决方法

    今天做练习时遇到这样的报错 xff1a Statements must be separated by newlines or semicolons 翻译一下就是 xff1a 语句必须用换行符或分号分隔 首先按报错提示 xff0c 我把cl
  • python自然语言处理之spacy详解

    spaCy简介 spaCy号称工业级Python自然语言处理 xff08 NLP xff09 软件包 xff0c 可以对自然语言文本做词性分析 命名实体识别 依赖关系刻画 xff0c 以及词嵌入向量的计算和可视化等 spaCy模块有4个非常
  • anaconda创建env报错 ResolvePackageNotFound

    具体错误 如图 xff1a 按照其他博主 xff08 方法详情 xff09 提供的方法操作了还是有部分报错 xff1a 解决策略 继续上面解决剩下的部分报错 xff0c 打开 yaml文件 xff0c 记事本打开就行 将报错列出的几个包移到
  • LDA主题建模过程及参数详解

    平台及工具 语言 xff1a python 平台 xff1a anaconda 43 jupyter notebook 语料库 xff1a 近三百篇英文文献的摘要 主要代码 首先 xff0c pandas处理csv数据 span class
  • 已经成功安装了但是jupyter notebook仍然找不到模块

    问题描述 工具 语言 jupyter notebook 43 anaconda python 有时会遇到这样的情况 xff0c 命名已经install了模块 xff0c notebook还是报找不到模块错误 再装已经提示satisfied
  • pyecharts 地图绘制

    环境描述 win11 jupyter notebook 目标效果 世界地图 43 按数据进行分级着色 xff1b 最终效果图如下 xff1a pyecharts 绘制地图时注意点 可以实现目标地图绘制效果的python库很多 xff0c 这
  • 指定wb用户在指定日期范围内的wb内容抓取

    一 操作步骤 只记录过程 xff0c 不讲述原理 1 获取用户ID和cookie 用户ID在进入个人主页时导航栏中就会有显示 xff0c 例如下面这样 xff1a cookie获取 xff08 有的代码无需cookie也能运行 xff09
  • 中介变量、调节变量与协变量

    在平时看论文过程中偶会接触到这几个概念 xff0c 然而都没想过弄明白 xff0c 每次总觉得只要看明白个大概反正自己又不用这种方法 作为科研人 xff0c 还是应该保持谦逊 xff0c 保持学习 一 中介变量 1 概念 中介变量 xff0
  • linux环境搭建

    文章目录 linux环境搭建基础工具环境docker镜像docker 的基本操作git amp amp sshbash脚本 python 环境 linux环境搭建 基础工具环境 docker镜像 Docker CE 镜像源站 docker如
  • 【Linux】状态机

    Linux 状态机 首先感谢阅读 xff0c 作者是在工作中学习与积累 xff0c 每一个笔记都是心得和积累 xff0c 希望可以和大家一起交流学习 学习咱们先定好计划 xff0c 需要了解的都一样 xff0c 有 xff1a xff08
  • MyBatisPlus源码

    MyBatisPlus源码 文章目录 MyBatisPlus源码1 通用Mapper 96 BaseMapper 96 2 顶级Mapper 96 Mapper 96 3 通用Service 96 IService 96 4 通用Servi
  • bomb二进制炸弹拆除实验(MIPS)

    上学期计算机系统基础课程的一个实验 xff0c 在这里再简略整理一下 xff1a 实验要求 xff1a 仅给定一个MIPS二进制可执行文件bomb xff0c 要求运用GDB调试工具 xff0c 通过分析反汇编代码来输入正确的参数以拆除炸弹
  • 图与网络可视化实战(matlab实现)

    本篇以2020年数学建模美赛D题的足球传球网络可视化为例 xff0c 分三大步骤来简单讲解一下matlab在图与网络可视化方面的应用 xff1a Step1 构图 xff1a 首先根据输入数据构造邻接矩阵 xff0c 然后调用matlab中
  • Week8 - 程序设计思维与实践 - HRZ的序列(第二次CSP模拟T1)

    T1 HRZ的序列 该比赛已结束 xff0c 您无法在比赛模式下递交该题目 您可以点击 在题库中打开 以普通模式查看和递交本题 题目描述 相较于咕咕东 xff0c 瑞神是个起早贪黑的好孩子 xff0c 今天早上瑞神起得很早 xff0c 刷B
  • Week8 - 程序设计思维与实践 - HRZ学英语(第二次CSP模拟T2)

    T2 HRZ学英语 该比赛已结束 xff0c 您无法在比赛模式下递交该题目 您可以点击 在题库中打开 以普通模式查看和递交本题 题目描述 瑞神今年大三了 xff0c 他在寒假学会了英文的26个字母 xff0c 所以他很兴奋 xff01 于是
  • Week14 - 程序设计思维与实践 - 矩阵快速幂(+优化DP)

    题目连接 A Q老师与石头剪刀布 xff08 必做 xff09 题意 每一个大人曾经都是一个小孩 xff0c Q老师 也一样 为了回忆童年 xff0c Q老师 和 Monika 玩起了石头剪刀布的游戏 xff0c 游戏一共 n 轮 无所不知
  • Week14 - 程序设计思维与实践 - HDU3700 - Cat(限时模拟)

    Problem Description There is a cat cat likes to sleep If he sleeps he sleeps continuously no less than A hours For some