题意:给你一串行驶指令,问你要最少要忽略多少个指令,才能使考试及格。
解题思路:哇,一开始以为改变速度和超车也是一个sign,但其实不是!!题目问的是要忽略多少个sign,但超车和改变速度指令不算sign,所以直接贪心就好了!那些会使你挂科的指令全部都要忽略掉!详见代码!
#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#define INF 1<<29
using namespace std;
int main(){
int n;
scanf("%d",&n);
int v,tv;//当前速度和临时变量
int ans=0;//答案
int tmp=0;//保存在你超车前有多少个不准超车指令
vector<int> vec;//限速指令列表
for(int i=0;i<n;i++){
int k;
scanf("%d",&k);
if(k==1){
scanf("%d",&tv);
v=tv;//改变速度
//看一下你前面有多少个限速指令比你的速度小的
while(!vec.empty()&&vec.back()<v){
ans++;
vec.pop_back();
}
}
if(k==2){
ans+=tmp;//看看前面有多少个不准超车指令
tmp=0;
}
if(k==3){
scanf("%d",&tv);
vec.push_back(tv);
//看看这个限速牌符不符合你的速度
while(!vec.empty()&&vec.back()<v){
ans++;
vec.pop_back();
}
}
if(k==4)//一旦给你超车,前面的不准超车指令数要置0
tmp=0;
if(k==5)//无速度限制,清空限速指令列表
vec.clear();
if(k==6)
tmp++;
}
cout<<ans<<endl;
return 0;
}