Codesforces 467E Alex and Complicated Task
2 seconds
256 megabytes
standard input
standard output
After you have read all the problems, probably, you think Alex is genius person. That's true! One day he came up with the following task.
Given a sequence of integer numbers a1, a2, ..., an. You are to find a longest sequence b1, b2, ..., b4m, that satisfies the following conditions:
- b4k + 1 = b4k + 3 for all valid integer k;
- b4k + 2 = b4k + 4 for all valid integer k;
- sequence b is subsequence of a (not necessarily contiguous subsequence).
And finally... Alex had given this complicated task to George, and George gave it to you. Help George to cope with the task.
The first line contains a single integer n (1 ≤ n ≤ 5·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In the first line print a single integer 4m — the maximal possible length of required sequence b. In the second line print 4m integersb1, b2, ..., b4m, that is required sequence.
If there are multiple optimal answers you may print any of them.
4
3 5 3 5
4
3 5 3 5
10
35 1 2 1 2 35 100 200 100 200
8
1 2 1 2 100 200 100 200
题目需要求一个最长的子序列 , 序列符合周期为4 ,然后奇数位,偶数位分别相等。
代码参考自美国coder : ecnerwal
#include<bits/stdc++.h> using namespace std; vector<int> res;
multiset<int> occ; //count number of occurrences
map<int, int> wrap; // wrapper for each value
vector<int> vals; // unwrapped values void finish(int a, int b) {
res.push_back(a);
res.push_back(b);
res.push_back(a);
res.push_back(b);
occ.clear();
wrap.clear();
vals.clear();
} int main() {
int N;
cin >> N;
for(int i = ; i < N; i++) {
int v; cin >> v;
if(wrap.count(v)) {
finish(wrap[v], v);
continue;
}
else {
if(occ.count(v)) {
int cnt = (occ.count(v) >= ) ? : ;
while(cnt || vals.back() != v) {
if(vals.back() == v) cnt--;
wrap[vals.back()] = v;
vals.pop_back();
}
}
occ.insert(v);
vals.push_back(v);
}
}
cout << res.size() << endl;
for(int i = ; i < res.size(); ++i ) cout << res[i] << ' '; cout << endl;
}
Codesforces 467E Alex and Complicated Task的更多相关文章
- Codeforces 461D. Appleman and Complicated Task 构造,计数
原文链接https://www.cnblogs.com/zhouzhendong/p/CF461D.html 题解 首先我们可以发现如果确定了第一行,那么方案就唯一了. 然后,我们来看看一个点的值确定 ...
- CF461D-Appleman and Complicated Task【并查集】
正题 题目链接:https://www.luogu.com.cn/problem/CF461D 题目大意 \(n*n\)的网格需要填上\(x\)或\(o\),其中有\(k\)个格子已经固定,求有多少中 ...
- [cf461D]Appleman and Complicated Task
假设该矩形是aij,那么有a(i,j)=a(i-1,j-1)^a(i-1,j+1)^a(i-2,j),不断递归下去可以发现a(i,j)=a(1,y-x+1)^a(1,y-x+3)^--^a(1,x+y ...
- [C6] Andrew Ng - Convolutional Neural Networks
About this Course This course will teach you how to build convolutional neural networks and apply it ...
- Unity3D游戏在iOS上因为trampolines闪退的原因与解决办法
http://7dot9.com/?p=444 http://whydoidoit.com/2012/08/20/unity-serializer-mono-and-trampolines/ 确定具体 ...
- CF 518 D. Ilya and Escalator
Ilya got tired of sports programming, left university and got a job in the subway. He was given the ...
- D. Ilya and Escalator
D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #263
http://codeforces.com/contest/461 A.水题 B.太挫了,竟然被hack了一发.... C.贪心..竟然没看出来时哈夫曼编码问题 D.题目大意:给一棵树,每一个点为白色 ...
- The Go Programming Language. Notes.
Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...
随机推荐
- Spring学习笔记(3)——快速入门
项目目录如下: Say.java为主函数通过ApplicationContext创建对象,利用方法ClassPathXmlApplicationContext访问配置文件Applicationcont ...
- 【学习总结】Python-3-round()函数的奇进偶弃的问题
参考: 本教程的评论区:菜鸟教程-Python3-Python数字 "4舍6入5看齐,奇进偶不进" 取代"四舍五入". round()函数: 可以在第二个参数指 ...
- js中Date的构造函数解读
javascript中的内置对象是我们经常会用到的,那么今天我们就来说说Date的四种构造方法吧 一.new Date() 这是我们最常使用也最熟悉不过的Date对象的构造方法了,通过无参数的构造函数 ...
- PCA算法和实例
PCA算法 算法步骤: 假设有m条n维数据. 1. 将原始数据按列组成n行m列矩阵X 2. 将X的每一行(代表一个属性字段)进行零均值化,即减去这一行的均值 3. 求出协方差矩阵C=1/mXXT 4. ...
- vim gdb使用
vim 8.0以上 :packadd termdebug :termdebug shell gdb中运行help all查看所有命令帮助 显示汇编窗口 layout asm
- Jenkins ant打包部署
选择项目 自由风格
- Java 实现文件复制的不同方法
用不同的方法实现文件的复制 1. 通道 Channel,它是一个对象,可以通过它读取和写入数据.拿NIO与原来的I/O比较,通道就像是流.是对接操作系统底层和缓冲区的桥梁. 2. 性能比较 内存映射最 ...
- Redis Key过期事件
解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态,如果未支付,则进行处理但是key过期了redis有通知吗?答案是肯定 ...
- cookie和session的区别有哪些
前言: cookie和session有着千丝万缕的联系,本文将详细介绍2者的区别. 1.存储位置不同 cookie的数据信息存放在客户端浏览器上. session的数据信息存放在服务器上. 2.存储容 ...
- boost serialization
Archive An archive is a sequence of bytes that represented serialized C++ objects. Objects can be ad ...