[STL] Codeforces 69E Subsegments
1 second
256 megabytes
standard input
standard output
Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in
, which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.
The first line contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n) — the number of array elements and the length of the segment.
Then follow n lines: the i-th one contains a single number ai ( - 109 ≤ ai ≤ 109).
Print n–k + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 … ai + k - 1that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".
5 3
1
2
2
3
3
1
3
2
6 4
3
3
3
4
4
2
4
Nothing
3
题意:给一个长度为n的数字,输出i从1到n-k-1,i到i+k-1这个区间内若存在只出现一次的区间最大值则输出这个值,否则输出Nothing
思路:先把前k个数排序并找出只出现一次的那些数,就可以的到第一次询问的答案,
然后从k+1开始,每加入一个数就删去上一个区间的第一个数,并看删去的那个数的个数
是否为1,若为1则加入可行集合,否则就看它是否在可行集合中(因为刚才删了一次,若它在可行集合中那么它的数量就为0了,要把它给删了)
若在就删了它。再看新加进来的那个数的个数是否为1,如果是就加入可行集合,否则就看它是否在可行集合中(如果它在可行集合中,那么它现在的数量为1,现在又加了一次它,它的个数就大于1,不符合条件了),若在就把它给删了
每次输出可行集合中最大的那个数
#include<bits/stdc++.h>
using namespace std;
const int amn=1e5+;
int a[amn];
map<int,int> mp; ///个数统计,a[i]最大是1e9,故用map统计
set<int> s; ///可行集合
int main(){
int n,k;
ios::sync_with_stdio();
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>a[i];
if(i<=k){
mp[a[i]]++;
if(mp[a[i]]==){
s.insert(a[i]);
}
else{
if(s.find(a[i])!=s.end()){
s.erase(a[i]);
}
}
if(i==k){
if(s.empty())printf("Nothing\n");
else{
printf("%d\n",*(--s.end())); ///set升序排序,输出最后一个
}
}
}
else{
mp[a[i]]++;
mp[a[i-k]]--;
if(mp[a[i-k]]==)s.insert(a[i-k]);
else{ ///可能为0了,如果这个数在可行集合中就要把它删掉
if(s.find(a[i-k])!=s.end()){
s.erase(a[i-k]);
}
}
if(mp[a[i]]==)s.insert(a[i]);
else{
if(s.find(a[i])!=s.end()){
s.erase(a[i]);
}
}
if(s.empty())printf("Nothing\n");
else{
printf("%d\n",*(--s.end()));
}
}
}
}
/***
先把前k个数排序并找出只出现一次的那些数,就可以的到第一次询问的答案,
然后从k+1开始,每加入一个数就删去上一个区间的第一个数,并看删去的那个数的个数
是否为1,若为1则加入可行集合,否则就看它是否在可行集合中(因为刚才删了一次,若它在可行集合中那么它的数量就为0了,要把它给删了)
若在就删了它。再看新加进来的那个数的个数是否为1,如果是就加入可行集合,否则就看它是否在可行集合中(如果它在可行集合中,那么它现在的数量为1,现在又加了一次它,它的个数就大于1,不符合条件了),若在就把它给删了
每次输出可行集合中最大的那个数
***/
[STL] Codeforces 69E Subsegments的更多相关文章
- codeforces每日一题1-10
目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn
题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...
- Codeforces 701C They Are Everywhere(Two pointers+STL)
[题目链接] http://codeforces.com/problemset/problem/701/C [题目大意] 给出 一个字符串,里面包含一定种类的字符,求出一个最短的子串,使得其包含该字符 ...
- Codeforces 799B - T-shirt buying(STL)
题目链接:http://codeforces.com/problemset/problem/799/B 题目大意:有n件T恤,每件T体恤都分别有价格(每件衣服的价格不重复).前面的颜色.背部的颜色三种 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- [Codeforces 1246B] Power Products (STL+分解质因数)
[Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...
- STL容器set用法以及codeforces 685B
以前没怎么用过set,然后挂训练赛的时候发现set的妙用,结合网上用法一边学一边写. 首先set是一种容器,可以跟其他STL容器一样用 set<int > s 来定义, 它包含在STL头文 ...
随机推荐
- Hive Functions
函数的分类 内置函数 操作符 复杂对象 UDF函数 数学函数 类型转换函数 日期函数 条件函数 UDTF函数 常用UDTF函数 explode posexplode inline stack json ...
- Vue1.0用法详解
Vue.js 不支持 IE8 及其以下版本,因为 Vue.js 使用了 IE8 不能实现的 ECMAScript 5 特性. 开发环境部署 可参考使用 vue+webpack. 基本用法 1 2 3 ...
- JobScheduler 和 JobService
使用AlarmManager.IntentService和PendingIntent相互配合,创走周期性的后台任务,实现一个完全可用的后台服务还需要手动执行以下操作. 计划一个周期性任务 ...
- python settings 中通过字符串导入模块
1. 项目文件结构 set_test ├─ main.py # 入口函数 │ ├─notify # 自定义的模块 │ ├─ email.py # 自定义模块 │ ├─ msg.py # 自定义模块 │ ...
- beego的安装以及bee的安装和使用
beego的安装以及bee的安装和使用 一.beego的安装 1.beego是什么 beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API.Web 及后端服务等各种应用, ...
- Django报Warning错误 RuntimeWarning: DateTimeField Goods.create_at received a naive datetime (2019-07-31 23:05:58) while time zone support is active
报错和UTC(世界标准时间)有关,在settings.py 文件中设置 USE_TZ = False 警告错误不再报
- DIV的失去焦点(blur)实现
用防抖实现DIV鼠标移出消失 由于div标签本身不支持onblur事件,所以对于点击一个按钮弹出的div,我们想要当这个div失去焦点的时候,让它消失不能使用的onblur来实现. 但是可以利用 ...
- apache搭建Tomcat集群(Cluster)
搭建集群: apache:特点处理静态资源(html 图片 js等) apache的请求操作,Cluster工具 tomcat:特点处理动态资源 apache+tomcat(apache是web服 ...
- 2020最新ArchLinux安装(KDE桌面)
许多网友反映之前的教程安装好后连不上互联网,最近我刚好又安装了一遍,总结出以下没毛病的过程 按照此教程需要你会基本的vim操作(或其他文本编辑工具比如nano),基本的fdisk分盘操作(或其他分盘工 ...
- Python - requests发送请求报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: 小明 is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
背景 在做接口自动化的时候,Excel作为数据驱动,里面存了中文,通过第三方库读取中文当请求参数传入 requests.post() 里面,就会报错 UnicodeEncodeError: 'lati ...