845D - Driving Test(贪心)

845D - Driving Test

竟然做法和题解一样~~~贪心

其实当时没想着能过,虽然并不是很难~

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int inf=1e9;
 4 const int maxn=2e5+10;
 5 int n;
 6 stack<int> s;
 7
 8 int main(){
 9     scanf("%d",&n);
10     int ans=0;
11     int v=0,lim=inf,past=0;  //初始速度为0,不限速(lim==inf),可超车(past==0为可超)
12
13     int id,val;
14     scanf("%d%d",&id,&val);
15     v=val;
16     s.push(inf);
17     for(int i=1;i<n;i++){
18         scanf("%d",&id);
19         if(id==1||id==3) scanf("%d",&val);
20         if(id==1){ //变速
21             while(val>lim){    //如果车速超过限制,就要无视限制
22                 if(!s.empty()){
23                     s.pop();
24                     ans++;
25                     lim=s.top();
26                 }
27             }
28             v=val;
29         }
30         if(id==2){ //超车
31             if(past){  //如果不可超车,就无视掉
32                 ans+=past;
33                 past=0;
34             }
35         }
36         if(id==3){ //限速val
37             if(v>val){  //当前车速就大于val,直接无视限制
38                 ans++;
39             }else{      //否则把限速存到栈里
40                 s.push(val);
41                 lim=val;
42             }
43         }
44         if(id==4){  //允许超车
45             past=0;
46         }
47         if(id==5){  //不限速
48             while(!s.empty()) s.pop();
49             s.push(inf);
50             lim=inf;
51         }
52         if(id==6){  //不许超车
53             past++;
54         }
55     }
56     printf("%d\n",ans);
57 }

时间: 2024-10-05 04:33:34

845D - Driving Test(贪心)的相关文章

Codeforces 845D - Two TVs(贪心)

原题链接:http://codeforces.com/problemset/problem/845/D 题意:一个人在驾照考试中,路边有"限速XX"."没有限速"."可以超车"."不能超车"路牌, 以及这个人在某一时刻开车速度.是否超车,题目按时间顺序输入以上情况,其中可能会有违反交规的情况,但这个人可以狡辩说没看到一些路牌.一开始默认是没有限速而且可以任意超车的. 问他要"假装"没看到几个路牌才能使他&q

To Fill or Not to Fill(贪心模拟)

题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are

【贪心】PAT 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to fin

nyoj248 BUYING FEED(贪心orDP)

题目248 题目信息 运行结果 本题排行 讨论区 BUYING FEED 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 Farmer John needs to travel to town to pick up K (1 <= K <= 100)pounds of feed. Driving D miles with K pounds of feed in his truck costs D*K cents. The county feed lot has N

1033. To Fill or Not to Fill (25) -贪心算法

题目如下: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are

题目1437:To Fill or Not to Fill(贪心算法)

题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro