[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头文 ...
随机推荐
- OpenCA搭建
前言: OpenCA是OpenCA开源组织使用Perl对OpenSSL进行二次开发而成的一套完善的PKI免费软件,主要由四部分组成:CA.RA.PUB和NODE.简而言之,PUB是对外提供服务的接口, ...
- 解决appium升级后不支持使用name定位的问题
前言 之前一直用的appium1.4版本,最近升级到了1.6突然发现之前的脚本好多都跑失败了,一看报错: selenium.common.exceptions.InvalidSelectorExcep ...
- Python-rediscluster客户端
# -*- coding: UTF-8 -*- import redis import sys from rediscluster import StrictRedisCluster #host = ...
- elasicsearch数据自动清理脚本
elasticsearch随着保存的数据越来越多,磁盘占用越来越大,有必要进行定期自动清理. 直接上脚本 cat es-index-clear.sh #/bin/bash #查看索引信息 #curl ...
- 【系统篇】Archlinux系统安装
本教程为最新安装Linux的教程,想看更详细可以到我B站主页看视频教程 ArchLinux安装配置手册[系统篇] 本教程参考自 https://wiki.archlinux.org/index.php ...
- scrapy爬虫-代理IP中间件
class ProxyDownloaderMiddleware(object): # Not all methods need to be defined. If a method is not de ...
- Ⅱ:python入门
一.编程语言介绍 编程语言的分类: 机器语言 汇编语言 高级语言(编译型.解释型号) 总结: 执行效率:机器语言>汇编语言>高级语言(编译型>解释型) 开发效率:机器语言<汇编 ...
- App崩溃监控
常见马虎导致崩溃 1 数组越界: 2 多线程问题,在子线程刷新UI: 3 主线程无响应,主线程超过系统规定的时间没有响应,就会被watchdog杀掉: 4 野指针: 崩溃信息的收集却并没有那么简单.因 ...
- fsLayuiPlugin数据字典使用
概述 数据字典主要解决下拉框数据填充和数据表格转义处理,一个数据字典可以多处使用. 1.多个页面下拉框使用同样的数据,改一个地方需要把所有页面都要修改 2.数据表格转义代替自己手动写templet解析 ...
- moment太重? 那就试试miment--一个超轻量级的js时间库
介绍 Miment 是一个轻量级的时间库(打包压缩后只有1K),没有太多的方法,Miment的设计理念就是让你以几乎为零的成本快速上手,无需一遍一遍的撸文档 由来 首先 致敬一下Moment,非常好用 ...