第一题:模拟题,直接按照输入顺序模拟即可
#include<iostream>
using namespace std;
const int N=105;
int red,yellow,green;
//红->绿->蓝
int main(){
cin>>red>>yellow>>green;
int n,k,t,ans=0;
cin>>n;
while(n--){
cin>>k>>t;
switch(k){
case 0:ans+=t;
break;
case 1:ans+=t;
break;
case 2:ans+=t+red;
break;
case 3: break;
}
}
cout<<ans;
return 0;
}
第二题:处理技巧为将所有的灯信号向红灯一开始的时间进行对齐。
#include<iostream>
using namespace std;
typedef long long LL;
LL red,yellow,green;
//红->绿->蓝
//把所有的基准调至红灯刚开始
LL costTime(LL color){
if(color<=red){
return red-color;
}
else if(color<=red+green){
return 0;
}
else{
return red+green+yellow-color+red;//黄灯剩余以及红灯
}
}
int main(){
cin>>red>>yellow>>green;
LL n,k,t,ans=0,cycle,time=0,color,wait;
cin>>n;
cycle=red+yellow+green;
while(n--){
cin>>k>>t;
switch(k){
case 0:time+=t;
break;
case 1:color=(red-t+time)%cycle;
time+=costTime(color);
break;
case 2:color=(cycle-t+time)%cycle;
time+=costTime(color);
break;
case 3: color=(red+green-t+time)%cycle;
time+=costTime(color);
break;
}
}
cout<<time;
return 0;
}