818D - Multicolored Cars
818D - Multicolored Cars
题意
在 1 到 n 时刻,有 n 量有颜色的车通过,用数字表示颜色,Alice 选择一个颜色A,要求 Bob 选择一个颜色B,使得对于任意时刻 cnt(B) >= cnt(A),即通过的颜色为 B 的车始终不小于颜色为 A 的车。求任意满足条件的解,否则输出 -1 。
分析
举例:
11 4
1 2 3 3 4 1 2 5 4 3 4
以 4 为最右端分段,即1 2 3 3 4为第一段,1 2 5 4第二段,3 4第三段。
用一个集合维护可用的值,数组维护还可用的次数,对于第一段前面的值,直接更新即可,到第二段,1 2出现了,所以仍在集合中,5在前一段中未出现,所以不用考虑,但是3要被加到集合中,因为到第二段,3仍是满足条件的,虽然这一段没有,可以使用前面的3进行抵扣。第三段只出现了一个3,所以3为最终答案。
模拟就好了。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 5;
int a[MAXN];
int b[MAXN];
set<int> set1, set2;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, A;
cin >> n >> A;
int f = 0;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
if(x != A) {
if(!f || set2.count(x)) {
set1.insert(x);
a[x]++;
}
} else {
f = 1;
int cnt = 0;
for(auto it : set2) {
a[it]--;
if(a[it] == 0) b[cnt++] = it;
}
for(int j = 0; j < cnt; j++) set2.erase(b[j]);
set2.insert(set1.begin(), set1.end());
set1.clear();
}
}
if(f) {
if(set2.empty()) cout << "-1" << endl;
else cout << *set2.begin() << endl;
}
else {
cout << 1001 << endl;
}
return 0;
}
818D - Multicolored Cars的更多相关文章
- Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- Educational Codeforces Round 24 CF 818 A-G 补题
6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...
- codeforces Educational Codeforces Round 24 (A~F)
题目链接:http://codeforces.com/contest/818 A. Diplomas and Certificates 题解:水题 #include <iostream> ...
- Codeforces 335C Sorting Railway Cars
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...
- 周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏
Toy Cars time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 【题解】【数组】【Prefix Sums】【Codility】Passing Cars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS
C. Sorting Railway Cars An infinitely long railway has a train consisting of n cars, numbered from ...
随机推荐
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目4
2014-04-23 18:17 题目:设计一个停车位的类. 解法:停车位,就要有停车.取车的功能了.另外我还加了一个工作线程用于计费,每秒给那些有车的车位加1块钱费用. 代码: // 8.4 Des ...
- springboot注解使用,分页sql
https://blog.csdn.net/KingBoyWorld/article/details/78948304
- JavaScript中最常用的55个经典技巧,没事的时候看看,拓展解决问题的思路
都转烂了,不过还是贴上来了.查的时候方便... test 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 & ...
- python练习题及实现--文件处理、date日期
练习题作者:Vamei 出处:http://www.cnblogs.com/vamei http://www.cnblogs.com/vamei/archive/2012/07/19/2600135. ...
- Buildroot ipa image
参考: https://github.com/csmart/ironic-python-agent/tree/buildroot/imagebuild/buildroot#buildroot-iron ...
- LDA学习笔记
线性判别分析(Linear Discriminant Analysis,简称LDA)是一种经典的线性学习方法.其思想非常朴素,设法将样例投影到一条直线上,使得同类样例的投影点尽可能接近,异类的样例的投 ...
- Mini-MBA记录
最近学完了Mini-MBA的课程,对课程讲述的人力资源,创新,财务,战略,领导力等方面有了更深一些的了解,在此之上也做了一些笔记,如果课程信息披露是被允许的,后续把这些笔记贴出来,作为自己以后的参考.
- Android 程序怎么打log
常见的做法: 1. 定义一个常量(变量)作为是否输出log的flag: 2. 定义一个常量(变量)作为log级别设定: 2. 调试.打包时,按需要调整常量的值,从而控制log打印. 常见代码参考: h ...
- HDU 2295 Radar (二分 + Dancing Links 重复覆盖模型 )
以下转自 这里 : 最小支配集问题:二分枚举最小距离,判断可行性.可行性即重复覆盖模型,DLX解之. A*的启发函数: 对当前矩阵来说,选择一个未被控制的列,很明显该列最少需要1个行来控制,所以ans ...
- Thread 多线程 同步
当多个线程,访问同一个对象,调用同一个方法或访问同一个对象时.有时,必须保证访问的同步性.比如,一个银行信用卡账户具有5000元的信用额度.用户除具有一张主卡外,还办有多张子卡.这些卡的累计消费金额, ...