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的更多相关文章

  1. 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 ...

  2. Educational Codeforces Round 24 CF 818 A-G 补题

    6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...

  3. codeforces Educational Codeforces Round 24 (A~F)

    题目链接:http://codeforces.com/contest/818 A. Diplomas and Certificates 题解:水题 #include <iostream> ...

  4. Codeforces 335C Sorting Railway Cars

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. CF#335 Sorting Railway Cars

    Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. 【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 ...

  7. 周赛-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 ...

  8. 【题解】【数组】【Prefix Sums】【Codility】Passing Cars

    A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...

  9. 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 ...

随机推荐

  1. 19、AngularJs知识点总结 part-1

    1.AngularJs AngularJs是一款JavaScript开源库,由Google维护,用来协助单一页面应用程序: AngularJs的目标是通过MVC模式增强基于浏览器的应用,使开发和测试变 ...

  2. scp 将数据从一台linux服务器复制到另一台linux服务器

    原文地址:http://www.cnblogs.com/peida/archive/2013/03/15/2960802.html  完整内容参考原文! scp 是secure copy的简写,用于在 ...

  3. rownum浅谈(一)

    只要做web开发,几乎没有不需要分页查询的,在oracle中,rownum就是用来进行处理分页的. 1.rownum是oracle对结果集返回的一个伪列,也就是说是先查询完结果之后再加上的一个虚列,相 ...

  4. js获取页面元素位置函数(跨浏览器)

    function getElementPos(elementId) { var ua = navigator.userAgent.toLowerCase(); var isOpera = (ua.in ...

  5. 【Luogu】P3239亚瑟王(概率DP)

    题目链接 请看luogu第一篇题解 #include<cstdio> #include<algorithm> #include<cstring> #include& ...

  6. tcp slowstart (TCP 慢启动)

    tcp slowstart (TCP 慢启动) 慢启动定义 慢启动,是传输控制协议使用的一种拥塞控制机制.慢启动也叫做指数增长期.慢启动是指每次TCP接收窗口收到确认时都会增长.增加的大小就是已确认段 ...

  7. jira7.3/7.2安装、中文及破解

    jira是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪(bug管理).客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域. 现在我们来介绍,有关jira安装. ...

  8. shell脚本——项目2

    案例名称:发送告警邮件 背景: 外部邮箱的服务器(163等) 安装mailx(yum) 配置邮箱信息 vim /etc/mail.rc #配置自己的邮箱信息 set from=18906534060@ ...

  9. webpack最佳入门实践系列(6)

    10.css模块化 10.1.什么是css模块? CSS模块就是所有的类名都只有局部作用域的CSS文件,当你在一个JavaScript模块中导入一个CSS文件时,CSS模块将会定义一个对象,将文件中类 ...

  10. poj 3729 Facer’s string

    Facer’s string Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2155   Accepted: 644 Des ...