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 ...
随机推荐
- 【Search in Rotated Sorted Array II 】cpp
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- SQL Server 分组取 Top 笔记(row_number + over 实现)
先看SQL语句(注意:这是在SQL Server 2005+ [包括2005] 的版本才支持的哦,o(∩_∩)o 哈哈~) SELECT col1,col2,col3 FROM table1 AS a ...
- android SharedPreferences 浅析
1. 介绍:SharedPreferences 的作用是使用键值对的方式存储数据.且支持多种不同的数据类型存储: Android数据持久化方法中最简单的一种,即使用Preferences的键值对存储方 ...
- python 学习分享-实战篇类 Fabric 主机管理程序开发
# 类 Fabric 主机管理程序开发: # 1. 运行程序列出主机组或者主机列表 # 2. 选择指定主机或主机组 # 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) # 4. 充分 ...
- python 学习分享-socketserver
SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端.即:每个客户端请求连接到服务器时,Socket服务端都会在服务器 ...
- obj = object(),所创建的obj实例到底是个啥?
In[1]: dir(obj) Out[1]:['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '_ ...
- 调整CodeIgniter错误报告级别
修改位置:CI根目录 index.php 为开发环境与生产环境定义错误报告级别 if (defined('ENVIRONMENT')) { switch (ENVIRONMENT) { case 'd ...
- [oldboy-django][2深入django]form表单clean_xx, clean完成数据验证+ form错误信息
form后台生成form里面的Input标签,以及设置Input的属性 # 需求 后台生成form里面的input标签,并设置input标签的属性, class RegisterForm(Form): ...
- HDU 4669 Mutiples on a circle 动态规划
参考了官方题解给的方法: 对于处理循环,官方给了一种很巧妙的方法: #include <cstdio> #include <cstring> #include <cstd ...
- js动态生成下拉列表
经常需要用到js动态生成下拉列表的功能,记录下来备用. 示例需求:通过ajax请求,从后台获取用户姓名列表,并添加到下拉列表中.js代码如下: function getNameList(){ //如果 ...