小明放学

第一次参见CSP考试认证,第二题竟然只得到60。仔细检查以后发现原来是数据溢出了,唉,经验不足,基础薄弱!

10^6*10^5为10^11,肯定溢出呀!

题目思路倒是不难

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     //0    1 2    3
 7     //红绿黄循环 0,r     r,r+g      r+g,r+g+y     根据pos的位置判断当前状态 
 8     //             r,0    g,0        y,0
 9     int r,y,g,n;    //红 黄 绿灯的设置 ,经过的道路段数和红绿灯数
10     cin>>r>>y>>g>>n; 
11     int k,t,pos;
12     long long time=0;    
13     int a[]={0,r,r+g+y,r+g};
14     for(int i=0;i<n;i++)
15     {
16         cin>>k>>t;
17         if(k==0)
18             time+=t;
19         else
20         {
21             pos=(a[k]-t+time)%(r+g+y);    //根据耗时计算当前pos的位置 
22             if(pos<r)    //此刻为红灯
23                 time+=(r-pos);     //红灯  等待
24             else if(pos<r+g)     //绿灯    直接通过
25                 ;
26             else                //黄灯 等待
27                 time+=(r+g+y-pos+r);
28         }
29     }
30     cout<<time;
31     return 0;
32 } 

猜你喜欢

转载自www.cnblogs.com/ManOK/p/10460303.html