E. Alex and Complicated Task
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains a single integer n (1 ≤ n ≤ 5·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

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.

Sample test(s)
input
4
3 5 3 5
output
4
3 5 3 5
input
10
35 1 2 1 2 35 100 200 100 200
output
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的更多相关文章

  1. Codeforces 461D. Appleman and Complicated Task 构造,计数

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF461D.html 题解 首先我们可以发现如果确定了第一行,那么方案就唯一了. 然后,我们来看看一个点的值确定 ...

  2. CF461D-Appleman and Complicated Task【并查集】

    正题 题目链接:https://www.luogu.com.cn/problem/CF461D 题目大意 \(n*n\)的网格需要填上\(x\)或\(o\),其中有\(k\)个格子已经固定,求有多少中 ...

  3. [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 ...

  4. [C6] Andrew Ng - Convolutional Neural Networks

    About this Course This course will teach you how to build convolutional neural networks and apply it ...

  5. Unity3D游戏在iOS上因为trampolines闪退的原因与解决办法

    http://7dot9.com/?p=444 http://whydoidoit.com/2012/08/20/unity-serializer-mono-and-trampolines/ 确定具体 ...

  6. 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 ...

  7. D. Ilya and Escalator

    D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #263

    http://codeforces.com/contest/461 A.水题 B.太挫了,竟然被hack了一发.... C.贪心..竟然没看出来时哈夫曼编码问题 D.题目大意:给一棵树,每一个点为白色 ...

  9. The Go Programming Language. Notes.

    Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...

随机推荐

  1. 【问题解决方案】Linux中进入目录下文件夹

    win系统中直接 cd+空格+文件夹名 Linux下 cd+空格+./+文件名 其中句点表示"当前目录" 除非在根目录不加,或者把路径写全用绝对路径进入 Linux下切换路径的相关 ...

  2. 负载均衡算法WeightedRoundRobin(加权轮询)简介及算法实现

    Nginx的负载均衡默认算法是加权轮询算法,本文简单介绍算法的逻辑,并给出算法的Java实现版本. 本文参考了Nginx的负载均衡 - 加权轮询 (Weighted Round Robin).     ...

  3. 条款2:尽量使用const ,enum,inline替换define

    宁可使用编译器而不用预处理器 假设我们使用预处理器: #define ABC 1.56 这标识符ABC也许编译器没看到,也许它在编译器处理源码前就被预处理器移走了,于是“标识符”ABC没有进入标识符列 ...

  4. Codeforces Round #420 (Div. 2) - C

    题目链接:http://codeforces.com/contest/821/problem/C 题意:起初有一个栈,给定2*n个命令,其中n个命令是往栈加入元素,另外n个命令是从栈中取出元素.你可以 ...

  5. EBCDIC to ASCII

    EBCDIC to ASCII https://www.ibm.com/support/knowledgecenter/SSZJPZ_11.7.0/com.ibm.swg.im.iis.ds.parj ...

  6. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

  7. Altium Designer 19使用

    铺铜之后运行DRC检查弹出警告: Design contains shelved or modified (but not repoured)polygons. The result of DRC w ...

  8. Java Web入门二

    Web应用服务器 供向外发布web资源的服务器软件. Web资源 存在于Web服务器可供外界访问的资源就是web资源.例如:存在于web服务器内部的Html.CSS.js.图片.视频等. 静态资源 w ...

  9. python3修改文件指定行

    方法可以有三个,但其实是一个方法,因为不同的方法都是文件存储的方法,文件修改就只有一个方法: 将文件导入list后,重新写入文件(另一个文件或者当前文件) 1.当前文件读取后,list修改内容,写入另 ...

  10. centos 6.5 安装 ant

    从ant官方网站下载ant安装包:apache-ant-1.9.7-bin.tar.gz,解压 tar xvf apache-ant-1.9.7-bin.tar.gz -C /usr/java/ 配置 ...