12108 - Extraordinarily Tired Students(特别困的学生)

2023-05-16

题目:

When a student is too tired, he can’t help sleeping in class, even if his favorite teacher is right here in front of him. Imagine you have a class of extraordinarily tired students, how long do you have to wait, before all the students are listening to you and won’t sleep any more? In order to complete this task, you need to understand how students behave. When a student is awaken, he struggles for a minutes listening to the teacher (after all, it’s too bad to sleep all the time). After that, he counts the number of awaken and sleeping students (including himself). If there are strictly more sleeping students than awaken students, he sleeps for b minutes. Otherwise, he struggles for another a minutes, because he knew that when there is only very few sleeping students, there is a big chance for them to be punished! Note that a student counts the number of sleeping students only when he wants to sleep again. Now that you understand each student could be described by two integers a and b, the length of awaken and sleeping period. If there are always more sleeping students, these two periods continue again and again. We combine an awaken period with a sleeping period after it, and call the combined period an awaken-sleeping period. For example, a student with a = 1 and b = 4 has an awaken-sleeping period of awaken-sleeping-sleeping-sleeping-sleeping. In this problem, we need another parameter c (1 ≤ c ≤ a + b) to describe a student’s initial condition: the initial position in his awaken-sleeping period. The 1st and 2nd position of the period discussed above are awaken and sleeping, respectively. Now we use a triple (a, b, c) to describe a student. Suppose there are three students (2, 4, 1), (1, 5, 2) and (1, 4, 3), all the students will be awaken at time 18. The details are shown in the table below.

题目大意:

每个学生有一个“睡眠-清醒” 周期, 第i个学生醒Ai分钟睡 Bi分钟, 初始处在他的周期的第Ci分钟, 临睡时会查看全班睡眠人数是否>清醒人数, 是则进入睡眠周期, 不然保持清醒Ai分钟, 问多久后全部学生都清醒。

分析:

题目求的是清醒人数, 那么我们可以把清醒人数设为一个变量, 设一个足够长的时间轴作为循环, 当清醒人数为N时跳出循环, 否则到了时间轴尾就输出-1。 题目给定的Ci其实很好的帮我们处理了“当前时间的问题”, 可以这样想, 时间轴每一点都进行ci的判断, <ai 则清醒人数++(清醒人数每一点初始化为0), 然后如果ci == ai 就判断人数, 如果不符合将ci回归到1, 同时当ci = ai + bi时也要回归到1

代码:

#include <stdio.h>
#include <string.h>
#define maxn 15
struct STUDENT {
	int A;//清醒A分钟
	int B;//睡B分钟
	int C;//当前处于的周期时间C分钟
	int D;//周期总时间
	int is_sleep;//当前时间是不是睡着
}st[maxn];
int cases = 0, n;
int main()
{
	while (scanf("%d", &n) && n)
	{
		int ALL = 1;//所有学生的醒睡总周期
		int awake = 0, sleep = 0;//全班清醒人数和睡觉人数
		for (int i = 0;i < n;i++)
		{
			scanf("%d%d%d", &st[i].A, &st[i].B, &st[i].C);
			st[i].D = st[i].A + st[i].B;
			if (st[i].C <= st[i].A)
			{
				st[i].is_sleep = 0;
				awake++;
			}
			else
			{
				st[i].is_sleep = 1;
				sleep++;
			}
			ALL *= st[i].D;
		}
		int time;//当前时间
		int flag=0;//标记是否存在全班都清醒的时刻
		for (time = 1;time <= ALL;time++)//只需要判断从初始回到初始这个时间段的情况
		{
			if (awake == n)
			{
				flag = 1;
				break;
			}
			int pre_sleep = sleep;//pre_sleep是上一时刻睡觉人数
			int pre_awake = awake;//pre_awake是上一时刻清醒人数
			for (int i = 0;i < n;i++)
			{
				st[i].C++;//下面就在i位学生周期的C时刻该学生的状态进行讨论
				//只有两种变化(醒->睡和睡->醒)时才需更新状态,其余情况只需C叠加
				if (st[i].C == (st[i].D + 1))//睡->醒
				{
					st[i].C = 1;
					st[i].is_sleep = 0;
					sleep--;
					awake++;
				}
				else if (st[i].C == (st[i].A + 1))//醒->睡
				{
					if (pre_sleep > pre_awake)
					{ //这点很重要!sleep和awake是变化当前状态,而需要判断的实际上是上一时刻
						st[i].is_sleep = 1;
						sleep++;
						awake--;
					}
					else st[i].C = 1;
				}
			}
		}
		if (flag) printf("Case %d: %d\n", ++cases, time);
		else printf("Case %d: %d\n", ++cases, -1);
	}
	return 0;
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

12108 - Extraordinarily Tired Students(特别困的学生) 的相关文章

随机推荐

  • STM32F103学习笔记(2.3)——读GPIO 按键

    为了读取引脚的高低电平 xff0c 就需要将引脚配置成输入模式 xff0c 并读取IDR寄存器 目录 寄存器配置 端口配置低寄存器 GPIOx CRL x 61 A E 端口输入数据寄存器 GPIOx IDR x 61 A E 按键点灯 寄
  • Windows系统下,Ubuntu安装至移动硬盘(简单分析与详细安装教程)

    前期说明 博主因学业要求 xff0c 需要同时使用Windows系统与Linux系统 xff0c 故而考虑安装双系统 但个人电脑硬盘仅剩100G左右大小 xff0c 安装双系统可能导致硬盘容量不足 xff0c 恰好博主手中有个空闲的移动硬盘
  • QT开发学习4(远程调试 Qt 程序)

    2 5 1 rsync 方式 Qt 远程调试 在 Qt Creator 中默认情况下 xff0c 会使用 sftp 或 rsync 发送程序到板卡 由于正点原子 I MX6 U 出厂 Qt 文件系统 xff08 文件系统 V1 9 及之后的
  • 使用 python requests 模块发送 http 请求及接收响应

    内容概要 如何构建GET 与 POST request 请求消息对 request 的header query string message body 定制化http header参数 content type 的设置分析request r
  • 汇编.section和.text以及入口地址解释

    section data 汇编程序中以 开头的名称并不是指令的助记符 xff0c 不会被翻译成机器指令 xff0c 而是给汇编器一些特殊指示 xff0c 称为汇编指示 xff08 Assembler Directive xff09 或伪操作
  • Linux - 配置Linux用户的环境变量- Anaconda3的环境变量配置

    目录 临时生效变量环境变量的分类 xff08 永久生效 xff09 如何让某个命令永久生效环境变量配置文件的运行顺序参考链接 Linux 操作系统的环境变量 xff0c 看似很复杂 xff0c 其实不然 我们通常用到的Windows 操作系
  • 兔子繁殖问题

    首先读懂题目 xff0c 知道运算规律后在使用斐波那契数列九很好解决啦 7 26 兔子繁殖问题 10 分 已知有一对兔子 xff0c 每个月可以生一对兔子 xff0c 而小兔子一个月后又可以生一对小兔子 比如 2月份出生的小兔子4月份可以生
  • 华为Atlas200DK环境配置指南(版本20.0.0)

    官方参考文档 https support huaweicloud com usermanual A200dk 3000 atlas200dk 02 0024 html 务必保证配置时版本 20 0 0 一致 1 配置开发环境 自己电脑 若不
  • 软件工程(速成)——第三章 需求分析

    一 需求分析 1 需求分析的概念与任务 xff1a 需求分析是软件定义时期的最后一个阶段 xff0c 它的基本任务是准确地回答 系统必须做什么 这个问题 二 分析建模与规格说明 需求分析应该建立三种模型 xff1a 数据模型 功能模型 行为
  • Pytorch 深度学习实战:视频自动打码

    点击上方 小白学视觉 xff0c 选择加 34 星标 34 或 置顶 重磅干货 xff0c 第一时间送达 人脸识别 人脸识别是一门比较成熟的技术 它的身影随处可见 xff0c 刷脸支付 xff0c 信息审核 xff0c 监控搜索 xff0c
  • 基于深度学习的视觉目标跟踪方法

    点击上方 小白学视觉 xff0c 选择加 34 星标 34 或 置顶 重磅干货 xff0c 第一时间送达 以前写过一个 自动驾驶中的目标跟踪 介绍 xff0c 这次重点放在深度学习和摄像头数据方面吧 先提一下以前说的那篇综述 xff1a 3
  • 递归解决赶鸭子问题,角骨定理

    一 题目分析 用递归方法设计下列各题 xff0c 并给出每道题目的递归出口 xff08 递归结束的条件 xff09 和递归表达式 同时考虑题目可否设计为非递归方法 xff0c 如果可以 xff0c 设计出非递归的算法 1 一个人赶着鸭子去每
  • 最详细ubuntu16.04安装nvidia显卡驱动(完全无经验小白教程)

    ubuntu16 04安装nvidia显卡驱动 1 禁用nouveau ubuntu 16 04默认安装了第三方开源的驱动程序nouveau xff0c 安装nvidia显卡驱动首先需要禁用nouveau xff0c 不然会碰到冲突的问题
  • 修改ssh端口重启服务报错error: Bind to port 8822 on :: failed: Permission denied

    root 64 BabyishRecent VM vi etc ssh sshd config root 64 BabyishRecent VM systemctl restart sshdJob for sshd service fail
  • Linux之iptables(一、防火墙的概念)

    Linux之iptables 一 防火墙的概念 防火墙的概念 一 安全技术 入侵检测与管理系统 xff08 Intrusion Detection Systems xff09 xff1a 特点是不阻断任何网络访问 xff0c 量化 定位来自
  • Python调用海康SDK对接摄像机

    以前做过的项目都是通过 ffmpeg c 43 43 来捕获摄像机的 RSTP 视频流来处理视频帧 xff0c 抽空看了一下海康的SDK说明 xff0c 使用 python ctypes方式a实现了对海康SDK DLL的调用 可以进行视频预
  • 数码管点亮顺序——有错请纠正

    找了半天没有找到 xff0c 自己试了几个数试出来了 xff0c 记这个顺序图比记编码表要快些
  • css-input的美化

    原装input很丑陋 xff0c 我们需要人工对其进行装饰才能好看哦 xff01 首先取消选中时的蓝色外边框 xff1a outline style none 若你想取消外边框 xff1a border xff1a 0 或 border x
  • echarts-横向柱状图的左侧文字左对齐设置

    在需要左对齐的Y轴中这样设置 xff0c 设置完后会发现 xff0c 文字跟圆柱重合覆盖 xff08 跟你需要的位置有区别 xff09 yAxis axisLabel margin 80 textStyle align 39 left 39
  • 12108 - Extraordinarily Tired Students(特别困的学生)

    题目 xff1a When a student is too tired he can t help sleeping in class even if his favorite teacher is right here in front