70分运行超时

#include <iostream>
using namespace std;//结构体,记录每个路口状态和所剩时间
struct status {int color;int time;
};
int r, y, g;
status st[100001];//函数,得到下一个灯色,所剩时间为最大
status nextOf(status now)
{status next = { 0,0 };switch (now.color){case 1: {//rednext.color = 3;next.time = g;break;}case 2: {//yellownext.color = 1;next.time = r;break;}case 3: {//greennext.color = 2;next.time = y;break;}default:break;}return next;}//函数:需要再此灯处耗费多少时间
int cost(status now)
{switch (now.color){case 1: {//redreturn now.time;}case 2: {//yellowreturn now.time + r;}case 3: {//greenreturn 0;}default:return now.time;}}//设计函数,参数为当前状态,所剩时间和经过时间,返回末状态和所剩时间
status fun(status now, long long ltime)
{status t = { 0,0 };if (now.color == 0){t.color = now.color;t.time = now.time;return t;}while (now.time < ltime){ltime -= now.time;now = nextOf(now);}t.color = now.color;t.time = now.time - ltime;/*if (now.time >= ltime){t.color = now.color;t.time = now.time - ltime;}else{t = fun(nextOf(now), ltime - now.time);}*/return t;}int main()
{cin >> r >> y >> g;int n;cin >> n;for (int i = 0; i < n; i++){cin >> st[i].color >> st[i].time;}long long sum = 0;//经过时间for (int i = 0; i < n; i++){if (st[i].color == 0){sum += st[i].time;for (int j = i + 1; j < n; j++){st[j] = fun(st[j], st[i].time);}}else{sum += cost(st[i]);for (int j = i + 1; j < n; j++){st[j] = fun(st[j], cost(st[i]));}}}cout << sum << endl;;return 0;
}

满分瞅他人的,

#include <iostream>
using namespace std;
//结构体,记录每个路口状态和所剩时间
struct status {int color;int time;
};
int r, y, g;
status st[100001];
//函数,得到下一个灯色,所剩时间为最大
status nextOf(status now)
{status next = { 0,0 };switch (now.color){case 1: {//rednext.color = 3;next.time = g;break;}case 2: {//yellownext.color = 1;next.time = r;break;}case 3: {//greennext.color = 2;next.time = y;break;}default:break;}return next;}
//函数:需要再此灯处耗费多少时间
int cost(status now)
{switch (now.color){case 1: {//redreturn now.time;}case 2: {//yellowreturn now.time + r;}case 3: {//greenreturn 0;}default:return now.time;}}
//设计函数,参数为当前状态,所剩时间和经过时间,返回末状态和所剩时间
status fun(status now, long long ltime)
{status t = { 0,0 };if (now.color == 0){t.color = now.color;t.time = now.time;return t;}while (now.time < ltime){ltime -= now.time;now = nextOf(now);}t.color = now.color;t.time = now.time - ltime;/*if (now.time >= ltime){t.color = now.color;t.time = now.time - ltime;}else{t = fun(nextOf(now), ltime - now.time);}*/return t;}int main()
{cin >> r >> y >> g;int n;cin >> n;long long sum = 0;//经过时间for (int i = 0; i < n; i++){cin >> st[i].color >> st[i].time;if (st[i].color == 0){sum += st[i].time;}else{sum+= cost(fun(st[i], sum%(r+g+y)));}}cout << sum << endl;;return 0;
}

就一个关键,将sum%(r+g+y)。迭代或者递归次数限制到了常数级

还有一个就是fun函数对于0即经过一段道路的处理

上面是迭代
下面是递归

#include <iostream>
using namespace std;//结构体,记录每个路口状态和所剩时间
struct status {int color;int time;
};int r, y, g;
status st[100001];//函数,得到下一个灯色,所剩时间为最大
status nextOf(status now)
{status next = { 0,0 };switch (now.color){case 1: {//rednext.color = 3;next.time = g;break;}case 2: {//yellownext.color = 1;next.time = r;break;}case 3: {//greennext.color = 2;next.time = y;break;}default:break;}return next;}//函数:需要再此灯处耗费多少时间
int cost(status now)
{switch (now.color){case 1: {//redreturn now.time;}case 2: {//yellowreturn now.time + r;}case 3: {//greenreturn 0;}default:return now.time;}}//设计函数,参数为当前状态,所剩时间和经过时间,返回末状态和所剩时间
status fun(status now, long long ltime)
{status t = { 0,0 };/*if (now.color == 0){t.color = now.color;t.time = now.time;return t;}while (now.time < ltime){ltime -= now.time;now = nextOf(now);}t.color = now.color;t.time = now.time - ltime;
*/if (now.color == 0){t.color = now.color;t.time = now.time;return t;}if (now.time >= ltime){t.color = now.color;t.time = now.time - ltime;}else{t = fun(nextOf(now), ltime - now.time);}return t;}int main()
{cin >> r >> y >> g;int n;cin >> n;long long sum = 0;//经过时间for (int i = 0; i < n; i++){cin >> st[i].color >> st[i].time;if (st[i].color == 0){sum += st[i].time;}else{sum+= cost(fun(st[i], sum%(r+g+y)));}}cout << sum << endl;;return 0;
}

递归调用耗时少点,偶然还是必然?

转载于:https://www.cnblogs.com/WuDie/p/11332165.html

201712-2放学相关推荐

  1. 热议!“建议放学时间与父母下班时间一致”,网友:陪我一起996吗?

    随着疫情的稳定,新学期开始,各地中小学学生开始重返校园,开启新一学期的学习生活. 这可让习惯了过去一年在家远程上课的家长们又开始头痛了. 为什么?因为孩子放学和家长下班时间,二者之间存在一段尴尬的&q ...

  2. 延迟放学,让“996父母”松了口气

    最近,深圳市中小学拟提供课后延时服务的消息,在家长中引起热议. 据深圳教育局发布的<深圳市义务教育阶段学校课后延时服务实施意见(征求意见稿)>信息,课后延时服务有如下特点: 正常上课日的下 ...

  3. 放学默示录Ⅰ:来!抠奖 移动游戏策划案

    游戏名称:放学默示录 游戏类型:休闲类 单机版带网络服务 运营方式:道具收费,捐赠. 游戏背景:以学生放学后的小摊游艺为参考. 游戏玩法: 游戏分为默示录模式,日常模式,大赛模式,小游戏. 游戏元素: ...

  4. 终于等到放学了的飞鸽传书

    飞鸽传书2012绿色版 作者:       i_like_cpp        -       日期:2012-04-26     浏览 61 次 飞鸽传书2012绿色版事情,但在平时一遍遍练习中我终 ...

  5. 【CCF】201812-2小明放学

    问题描述 试题编号: 201812-2 试题名称: 小明放学 时间限制: 1.0s 内存限制: 512.0MB 题目背景 汉东省政法大学附属中学所在的光明区最近实施了名为"智慧光明" ...

  6. CCF201812-2 小明放学

    试题编号: 201812-2 试题名称: 小明放学 时间限制: 1.0s 内存限制: 512.0MB 问题描述: 题目背景 汉东省政法大学附属中学所在的光明区最近实施了名为"智慧光明&quo ...

  7. ##CSP 201812-2 小明放学(C语言)(100分)

    题目背景 汉东省政法大学附属中学所在的光明区最近实施了名为"智慧光明"的智慧城市项目.具体到交通领域,通过"智慧光明"终端,可以看到光明区所有红绿灯此时此刻的状 ...

  8. CCF201812-2 小明放学(100分)【序列处理】

    题目背景 汉东省政法大学附属中学所在的光明区最近实施了名为"智慧光明"的智慧城市项目.具体到交通领域,通过"智慧光明"终端,可以看到光明区所有红绿灯此时此刻的状 ...

  9. CCF认证-201812-2-小明放学(C语言实现)

    问题描述: 一次放学的时候,小明已经规划好了自己回家的路线,并且能够预测经过各个路段的时间.同时,小明通过学校里安装的"智慧光明"终端,看到了出发时刻路上经过的所有红绿灯的指示状态 ...

  10. CCF2018年:小明放学C++答案

    题目背景 汉东省政法大学附属中学所在的光明区最近实施了名为"智慧光明"的智慧城市项目.具体到交通领域,通过"智慧光明"终端,可以看到光明区所有红绿灯此时此刻的状 ...

最新文章

  1. 刚刚,特斯拉宣布终止私有化,马斯克最终没能说服大股东
  2. 内存溢出原因及解决方案
  3. wps 选择 高亮_WPS的这些功能,竟然如此好用
  4. Django Rest框架 APIView源码调用
  5. Linux环境下用vim编写编译运行C/C++程序
  6. oracle 创建数据库表 如果此表存在则删除后再重建
  7. H5 File 对象获取 Input type=file 文件详细信息
  8. idea 2019最新版无法打开报错问题,Error occurred during initialization of VM Initial heap size set to a larger va
  9. nyoj 643intersection set
  10. 囧。。。不知不觉破解了IDMan。。。木有注意最后一步咋破的。。。
  11. matlab word 查找 词组,matlab搜索word文档
  12. Mac屏幕录制GIF动图
  13. 高中计算机会考操作题程序设计,高中信息技术算法与程序设计题库(含答案)
  14. PS笔刷:80个自定义绘画画笔工具笔刷套装
  15. SiteSucker Mac版(整站下载工具)中文版
  16. android N编译
  17. 家用威联通NAS的硬盘方案
  18. 小豆苗与妈咪知道发布战略合作,打造母婴健康服务新生态
  19. 怎样对一个项目进行成本管理,具体步骤是啥?
  20. 跟着AI涨知识-量子纠缠

热门文章

  1. 面试题 17.13. 恢复空格
  2. 大数据WEB阶段(二十)更新丢失
  3. 机器人从计算机应用领域看,人的素质差异,本质地不在于他们即存知识信息量的差异,而在于他们思维能力的差异。( )...
  4. 鸿蒙开发者目前人数,苹果时隔两年公布大中华区开发者数据:440万,增长76%
  5. 2014年江苏省计算机二级c语言考试大纲,「二级C语言」江苏省计算机二级VFP考试大纲...
  6. linux共享内存变量 tiaojianbianliang,修改linux共享内存大小
  7. VS2010设置全局Include和Lib目录
  8. 克隆管理员帐号的方法
  9. CTreeCtrl控件的使用小记
  10. error LNK2005: _DllMain@12 already defined的解决办法