Codeforces 845D - Two TVs(贪心)

原题链接: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\n", res1+res2);
62     return 0;
63 }

 

转载于:https://www.cnblogs.com/MasterSpark/p/7523886.html

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值