Codeforces 658B Bear and Displayed Friends【set】
题目链接:
http://codeforces.com/contest/658/problem/B
题意:
给定元素编号及亲密度,每次插入一个元素,并按亲密度从大到小排序。给定若干操作,回答每次询问的元素是否排在前k个。
分析:
先附上我的垃圾做法:
之前没怎么用过set,比赛的时候第一反应就是利用优先级队列,每次将元素直接插入队列中,由于k最大为6,所以遍历队列的前k个,然后看是否有查询的元素。
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1500005;
int t[maxn];
vector<int>v;
int main (void)
{
int n, k ,q;
scanf("%d%d%d", &n, &k, &q);
for(int i = 1; i <= n; i++){
scanf("%d", &t[i]);
}
int num, a;
priority_queue<int, vector<int>, less<int> >que;
for(int i = 0; i < q; i++){
scanf("%d%d", &num, &a);
if(num == 1){
que.push(t[a]);
}else{
int flag = 0, cnt = 0;
while(cnt < k && !que.empty() ){
int tmp = que.top();
if(tmp == t[a]){
printf("YES\n");
flag = 1;
break;
}
que.pop();
cnt++;
v.push_back(tmp);
}
for(int i = 0 ; i < v.size(); i++)
que.push(v[i]);
v.clear();
if(!flag) printf("NO\n");
}
}
其实用set可以非常快的解决:
#include <cstdio>
#include<iostream>
#include<set>
using namespace std;
set<int>s;
const int maxn = 150005;
int t[maxn];
int main (void)
{
int n, k, q; cin>>n>>k>>q;
for(int i = 1; i <= n; i++){
cin>>t[i];
}
int type, a;
for(int i = 0; i < q; i++){
cin>>type>>a;
if(type == 1){
s.insert(t[a]);
if(s.size() > k) s.erase(s.begin());
}else{
if(!s.count(t[a])) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
return 0;
}
Codeforces 658B Bear and Displayed Friends【set】的更多相关文章
- Educational Codeforces Round 35 A. Nearest Minimums【预处理】
[题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...
- Codeforces 526D - Om Nom and Necklace 【KMP】
ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...
- Educational Codeforces Round 31 A. Book Reading【暴力】
A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #445 A. ACM ICPC【暴力】
A. ACM ICPC time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces 653A Bear and Three Balls【水题】
题目链接: http://codeforces.com/problemset/problem/653/A 题意: 给定序列,找是否存在连续的三个数. 分析: 排序~去重~直接判断~~ 代码: #inc ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #177 (Div. 1) 题解【ABCD】
Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...
随机推荐
- kde framework概述(KDE Framework译文)
KDE Frameworks 基于QT框架,提供简单实用的类(例如那些KCoreAddons里的类)去为桌面应用的日常需要整合出解决方案(例如KNewStuff用于在应用中获取可下载的附加内容,或者那 ...
- php中include_path配置
在php.ini中可配置include_path来达到在任何文件中都可以直接引入该目录下文件 include_path = ".:/usr/share/php:/var/www/phpxwl ...
- sql server 2000备份还原数据库
转载请注明出处:http://blog.csdn.net/neochan1108/article/details/79248017 备份: -- Create the backup device fo ...
- uva1614 Hell on the Markets
贪心部分的理论依据:前i个数可以凑出1-sum[i]的所有整数. 证明:第二类数学归纳,n=1时成立,假设n=k之前所有项都成立,当n=k+1时.sum[k+1]=sum[k]+a[k+1].只需证明 ...
- 判断请求是否为ajax
判断请求是否为ajax 转载:http://www.cnblogs.com/tony-jingzhou/archive/2012/07/30/2615612.html x-requested-with ...
- DedeCMS文章标题长度最全修改方法
有时候DedeCMS首页或者其他页面不能全部展示文章标题,造成读者阅读体验差.一般来说标题精简.概括性强.有本文关键词就是一个好标题.写软文不比写“作文”,也不是论坛的标题党,软文是用来做排名的,主要 ...
- git 设置ss代理
git config --global https.proxy http://127.0.0.1:1080 git config --global https.proxy https:// ...
- 关闭Visual Studio 2015 关闭单击打开文件的功能
工具-->选项-->环境-->选项卡和窗口-->预览选项卡 去掉“在解决方案资源管理器中预览选定的文件(在按住Alt的同时单击可避免预览)(X)”的勾选
- ios之数据持久化
9.1 数据持久化概述 iOS中可以有四种持久化数据的方式: 属性列表.对象归档.SQLite3和Core Data 9.2 iOS应用程序目录结构 iOS应用程序运行在Mac os模拟器时候,有一下 ...
- poj3710 Christmas Game
题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...