Codeforces 845D

原题链接:http://codeforces.com/problemset/problem/845/D

题意:一个人在驾照考试中,路边有“限速XX”、“没有限速”、“可以超车”、“不能超车”路牌, 以及这个人在某一时刻开车速度、是否超车,题目按时间顺序输入以上情况,其中可能会有违反交规的情况,但这个人可以狡辩说没看到一些路牌。一开始默认是没有限速而且可以任意超车的。

问他要“假装”没看到几个路牌才能使他“没有”违反交规。

思路:有意思的题目。 事实上超速情况和超车情况可以分开来考虑,线性枚举。

先来讨论超车情况,可以简单的在遇到“不能超车”时用add记录总共的“不能超车”数;当有超车行为发生时,在结果加上add,并将add还原为0;当遇到“可以超车”时,add归零。

然后就是超速情况,与超车情况类似,在枚举时遇到速度变换就更新速度,当“没有限速”时add归零,但对于“限速XX”情况,我们需要把限速速度压入栈内,因为他要“忽略”的限速牌,要刚好使没被“忽略”的最后一个限速牌大于当前速度,如果用add粗暴叠加,可能会使结果大于答案。

AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<stack>
 5 using namespace std;
 6 const int MAXN=2e5+10;
 7 const int INF=1e9+10;
 8 struct Node{
 9     int sp;
10     int type;
11 }n1[MAXN],n2[MAXN];
12 int dp1[MAXN], dp2[MAXN];
13 int main()
14 {
15     int n,t1,t2;
16     scanf("%d", &n);
17     t1=t2=0;
18     int q,m;
19     for(int i=0;i<n;i++){
20         scanf("%d", &q);
21         if(q==1||q==3){
22             scanf("%d", &m);
23             n1[t1].sp=m;
24             n1[t1].type=q;
25             t1++;
26         }
27         if(q==5){
28             n1[t1].type=q;
29             t1++;
30         }
31         if(q==2||q==4||q==6){
32             n2[t2].type=q;
33             t2++;
34         }
35     }
36     
37     stack<int> st;
38     int add=0,res1=0;
39     int speed=n1[0].sp,limit;
40     for(int i=1;i<t1;i++){
41         if(n1[i].type==1) speed=n1[i].sp;
42         if(n1[i].type==3) st.push(n1[i].sp);
43         if(n1[i].type==5) while(!st.empty()) st.pop();
44          while(!st.empty()&&speed>st.top()){
45             st.pop();
46             res1++; 
47         }
48     }
49     //cout<<'*'<<endl;
50     int res2=0;
51     add=0;
52     for(int i=0;i<t2;i++){
53         if(n2[i].type==2){
54             res2+=add;
55             add=0;
56         }
57         if(n2[i].type==4) add=0;
58         if(n2[i].type==6) add++;
59     }
60     //cout<<res1<<' '<<res2<<endl; 
61     printf("%d
", res1+res2);
62     return 0;
63 }
原文地址:https://www.cnblogs.com/MasterSpark/p/7523886.html