CCF-CSP 201812-2 小明放学

 

 需要注意的是该题需要用到上一题的条件,即灯光的变化是按照:红绿黄循环变化的

第一想法是模拟该过程即可,即根据条件得到到达某一个红绿灯时经过的时间,然后再根据该时间去循环该红绿灯的变化,得到当前时间的灯光状态,再进行计算叠加即可

#include<iostream>

using namespace std;

int r,y,g,n;
long long ans;

void fun(int t,int sub,int cur){
	t-=sub;
	do{
		if(cur==1){//红 
			cur=3;//红变绿 
		}else if(cur==2){//黄 
			cur=1;//黄变红 
		}else if(cur==3){//绿 
			cur=2;//绿变黄 
		}	
		if(cur==1){
			t+=r;
		}else if(cur==2){
			t+=(y);
		}else if(cur==3){
			t+=g;
		}
	}while(t<0);
	if(cur==1){
		ans+=t;
	}else if(cur==2){
		ans+=(r+t);
	}		
}

int main(){
	cin>>r>>y>>g;
	cin>>n;
	for(int i=0;i<n;i++){
		int k,t;
		cin>>k>>t;
		if(k==0){
			ans+=t;
		}else{
			if(t-ans>0){//不会影响当前红绿灯状态 
				if(k==1){
					ans+=t;
				}else if(k==2){
					ans+=(r+t);
				}		
			}else{
				fun(t,ans,k);
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

该代码只能通过60分,目前还未查询出问题所在

 思路2(区间模拟借鉴自

我们使用一个坐标轴来模拟时间的变化,由出发时刻得到的每一个t,我们都可以推算出在坐标轴上的位置,然后再进行时间的变化(即得到当前时刻的时间坐标),再判断还需要多少时间才会变成绿灯即可

k = 0,t表示行驶路程的时间,直接累加
k = 1;t表示人在起点时,该红灯剩余的时间
k = 2;t表示人在起点时,该黄灯剩余的时间
k = 3;t表示人在起点时,该绿灯剩余的时间
可以画一个坐标图模拟过时间cnt后灯的颜色,从而计算还需等待多久

在这里插入图片描述

 

#include<iostream>

using namespace std;

int r,y,g,n;
long long ans;

void fun(int t,int sub,int cur){
	t-=sub;
	do{
		if(cur==1){//红 
			cur=3;//红变绿 
		}else if(cur==2){//黄 
			cur=1;//黄变红 
		}else if(cur==3){//绿 
			cur=2;//绿变黄 
		}	
		if(cur==1){
			t+=r;
		}else if(cur==2){
			t+=(y);
		}else if(cur==3){
			t+=g;
		}
	}while(t<0);
	if(cur==1){
		ans+=t;
	}else if(cur==2){
		ans+=(r+t);
	}		
}

int main(){
	cin>>r>>y>>g;
	cin>>n;
	for(int i=0;i<n;i++){
		int k,t;
		cin>>k>>t;
		if(k==0){
			ans+=t;
		}else{
			if(t-ans>0){//不会影响当前红绿灯状态 
				if(k==1){
					ans+=t;
				}else if(k==2){
					ans+=(r+t);
				}		
			}else{
				fun(t,ans,k);
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZZWWWFFF_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值