Codeforces 962D Merge Equals ( 模拟 )
题意 : 给出一个序列,然后每次将重复出现的元素进行求和合并(若有多个,则优先取最小的进行合并),若某重复元素有很多,那么取最左边的那两个进行合并且合并后元素位于原来右边元素的位置,例如 3 2 6 2 2 这里 2 是重复元素,取最左边的两个 2 进行一次求和合并,合并后将变成 4 ,且这个 4 的位置位于较右边的 2 的位置,即序列变成 3 6 4 2。进行这样的操作,直到没有元素重复为止,输出最终序列。
分析 :
考察模拟能力
首先注意到,输出最后的序列
只需要知道各个元素的相对位置即可
不必知道元素真正的位置下标是多少
即不用真正实时更新位置下标,因为这个操作会很麻烦
对于整个序列,需要知道哪些数是重复了的
这里用一个 set 维护
而对于每一个重复的数,又要知道其每个元素的位置
而且最好让这些位置升序排序,才方便取出最左边的两个进行合并
考虑此题的数据量并不是很大
所以考虑使用 map< int, set<int> > 来维护
最后就是模拟操作即可
所有的操作完成后答案就存在于 map 中
将元素信息取出来,然后丢到一个 vector 中
排序输出即可
#pragma GCC optimize("O3")
#define LL long long
#define pi pair<long long, long long>
#define MK(i, j) make_pair(i, j)
#include <bits/stdc++.h>
using namespace std;
map<LL, set<LL> > mp;
set<LL> s;
vector<pi> ans;
int N;
LL num;
int main(void)
{
ios_base::sync_with_stdio(); cin.tie();
cin>>N;
; i<N; i++){
cin>>num;
mp[num].insert(i);
)
s.insert(num);
}
while(!s.empty()){
set<LL>::iterator it = s.begin();
mp[(*it)].erase(mp[(*it)].begin());
mp[(*it)<<].insert((*mp[(*it)].begin()));
mp[(*it)].erase(mp[(*it)].begin());
].size() > ) s.insert((*it)<<);
) s.erase((*it));
}
for(map<LL, set<LL> >::iterator it=mp.begin(); it!=mp.end(); it++){
){
ans.push_back(MK(*(*it).second.begin(), (*it).first));
}
}
sort(ans.begin(), ans.end());
cout<<ans.size()<<endl;
for(vector<pi>::iterator it=ans.begin(); it!=ans.end(); it++)
cout<<(*it).second<<" ";
cout<<endl;
;
}
Codeforces 962D Merge Equals ( 模拟 )的更多相关文章
- Codeforces 962D - Merge Equals
链接: http://codeforces.com/problemset/problem/962/D 题意: 给出一个整数序列.选择其中最小且出现两次(或以上)的数,把最左边的两个从序列中移除,然后把 ...
- Educational Codeforces Round 42 (Rated for Div. 2) D. Merge Equals
http://codeforces.com/contest/962/problem/D D. Merge Equals time limit per test 2 seconds memory lim ...
- Educational Codeforces Round 42D. Merge Equals(STL)
D. Merge Equals time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- D Merge Equals Educational Codeforces Round 42 (Rated for Div. 2) (STL )
D. Merge Equals time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...
- D. Merge Equals(from Educational Codeforces Round 42 (Rated for Div. 2))
模拟题,运用强大的stl. #include <iostream> #include <map> #include <algorithm> #include < ...
- Educational Codeforces Round 42 D. Merge Equals (set + pll)
CF962D 题意: 给定一个数列,对于靠近左端的两个相同大小的值x可以合并成一个点.把x 乘以2 放在第二个点的位置,问最后的数列大小和每个位子的值. 思路: 利用set 配上 pair 就行了,感 ...
- Codeforces 738D. Sea Battle 模拟
D. Sea Battle time limit per test: 1 second memory limit per test :256 megabytes input: standard inp ...
- Codeforces 626A Robot Sequence(模拟)
A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- CodeForces - 589D(暴力+模拟)
题目链接:http://codeforces.com/problemset/problem/589/D 题目大意:给出n个人行走的开始时刻,开始时间和结束时间,求每个人分别能跟多少人相遇打招呼(每两人 ...
随机推荐
- 【Qt开发】qt中涉及到空格包含路径的解决办法
qt中涉及到空格路径,qmake是无法正确编译的. 需要在空格路径前面加上$$quote INCLUDEPATH += $$quote(C:/Program Files/MySQL/MySQL Ser ...
- python关键字以及含义,用法
Python常用的关键字 1.and , or and , or 为逻辑关系用语,Python具有短路逻辑,False and 返回 False 不执行后面的语句, True or 直接返回Tru ...
- Linux系统管理和调优(内存、CPU、磁盘IO、网络)
系统管理 Author:Rich七哥 查看 CPU 负载相关工具,找出系统中使用 CPU 最多的进程 查看 Memory 运行状态相关工具,找出系统中使用内存最多的进程 查看 IO 运行状态相关工具, ...
- Solr 4.4.0利用dataimporthandler导入本地pdf、word等文档
1. 创建本地目录 $ mkdir /usr/local/contentplatform/solr/solr/core1/file1 $ ls -lh total 88M -rw-r--r-- tnu ...
- windows如何使用bat快速安装计划任务?
关键词:windows定时任务,schtasks,at ,bat schtasks 部分转自: https://www.cnblogs.com/yumianhu/p/3710743.html at的详 ...
- 修改python pip3镜像源
方法一: pip3 install 包名 -i 镜像源url 主要的镜像源: pip3 install tornado -i https://pypi.douban.com/simple/ pip ...
- hadoop-mapreduce 详解
mapreduce 完整流程解析 1. 在客户端启动一个 job: hadoop jar share/hadoop/tools/lib/hadoop-streaming-2.6.5.jar -file ...
- Homebrew学习(二)之安装、卸载、更新
安装 1.网上的安装方法都是用curl,从官网找到命令复制到终端,然后回车,结果报错请求超时 /usr/bin/ruby -e "$(curl -fsSL https://raw.githu ...
- nginx服务报403错误的解决方法
1.可能是文件权限问题,看一下网站所在的文件夹权限,用户组和用户名是否属于www:www,因为nginx.conf顶头写的是user www:www
- 原型链—— javascript
目录 js面向对象编程 js原型链 共享方法 原型继承 class继承 js面向对象编程 js面向对象编程不同于 java 的类和对象 JavaScript 不区分类和实例的概念,而是通过原型(pro ...