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. psfaddtable - 添加一个Unicode字符表到控制台字体中

    总览 psfaddtable 字体文件 表文件 [输出文件] 描述 Psfaddtable 命令融合了 字体文件 提供的 .psf 格式的控制台字体和 表文件 提供的Unicode字符表, 生成一个带 ...

  2. python时间的获取

    一.获取当前时间 import datetime # 2019-7-9 print(datetime.datetime.now().year) # 2019 print(datetime.dateti ...

  3. liunx-网络基础

    liunx 网络配置  ifconfig: ipconfig -a ;显示信息 ifconfig eth1 up //开启网络接口 ifconfig eth1 down //关闭网络接口 ifconf ...

  4. DNS域名系统,简述工作原理

    DNS工作原理: 当DNS客户端需要在程序中使用名称时,它会查询DNS服务器来解析该名称.客户端发送的每条查询信息包括三条信息:指定的DNS域名,指定的查询类型,DNS域名的指定类别.基于UDP服务, ...

  5. 对于一般情况X1+X2+X3+……+Xn=m 的正整数解有 (m-1)C(n-1) 它的非负整数解有 (m+n-1)C(n-1)种

    对于一般情况X1+X2+X3+……+Xn=m 的正整数解有 (m-1)C(n-1) 它的非负整数解有 (m+n-1)C(n-1)种

  6. chrome插件研发手册

    chrome插件研发手册 一:需求前景 对于研发的小伙伴来说,总会遇到这样的需求,想要通过代码操作已有网站的行为动作,如:自动填充表格内容(表单内容太多,想一键将表单内容填充):自动登录网站(网站登录 ...

  7. mac终端方式修改host

    打开终端 cd /      #进入根目录 ls      #查看根目录下列表,确定有需要打开的目录 cd etc   #进入配置文件目录 ls sudo vim hosts      #用vim打开 ...

  8. PHP计算经纬度在百度多边形区域内

    最近做一个项目需要使用到区域,并且要判断当前的经纬度是否在区域内,已便对应业务需求变化.废话不多说直接上代码: /** * 验证区域范围 * @param array $coordArray 区域 * ...

  9. python字符串非空判断

    1. 字符串非空判断 2. list 非空判断

  10. 如何隐藏一个让人很难发现的bug?

    程序员的日常三件事:写bug.改bug.背锅.连程序员都自我调侃道,为什么每天都在加班?因为我的眼里常含bug. 那么如何写出一个让(坑)人(王)很(之)难(王)发现的bug呢? - 1 - 新手开发 ...