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. 【ASE高级软件工程】Alpha 阶段 backend组 scrum1 记录

    本次会议于11月4日,19:10 在微软北京西二号楼13478召开,持续40分钟. 与会人员:Haifeng Chen, Zhikai Chen, Hao Wang, Jia Ning 请假: Xin ...

  2. CentOS 7 LNMP环境搭建 Zabbix3.4

    概述:在CentOS 7 64位操作系统环境下搭建LNMP(Linux+Nginx+MySQL+PHP)来运行Zabbix 3.4 监控程序 预先安装: yum install -y autoconf ...

  3. office2016pro专业版命令行激活

    1.首先查看Office2016安装目录在哪里,如果是默认安装,没有修改路径,是在C:\Program Files\Microsoft Office\Office16目录下,具体路径还得自行查看,我的 ...

  4. 【串线篇】spring boot配置文件大全【上】

    一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: • application.properties • application.yml 配置文件的作用:修改SpringB ...

  5. 【串线篇】加谈数据库之连接join

    主题:内连接.左连接(左外连接).右连接(右外连接) 建表语句: CREATE TABLE `a_table` (  `a_id` int(11) DEFAULT NULL,  `a_name` va ...

  6. 微信小程序中的自定义组件 以及 相关的坑

    Step1 我们初始化一个小程序(本示例基础版本库为 1.7 ),删掉里面的示例代码,并新建一个 components 文件夹,用于存放我们以后开发中的所用组件,今天我们的目的是实现一个 首页 组件, ...

  7. shell脚本特殊变量与变量子串相关知识

    一.shell脚本特殊变量 1.shell中常用特殊位置变量说明: $0 获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,那么就包含了脚本路径 $n 获取当前执行的shell脚本的第n个 ...

  8. 阿里下一代云分析型数据库AnalyticDB入选Forrester云化数仓象限

    前言 近期, 全球权威IT咨询机构Forrester发布"The Forrester Wave: CloudData Warehouse Q4 2018"研究报告,阿里巴巴分析型数 ...

  9. JS中算法之排序算法

    1.基本排序算法 1.1.冒泡排序 它是最慢的排序算法之一. 1.不断比较相邻的两个元素,如果前一个比后一个大,则交换位置. 2.当比较完第一轮的时候最后一个元素应该是最大的一个. 3.按照步骤一的方 ...

  10. k-近邻算法(kNN)测试算法:作为完整程序验证分类器

    #测试算法:作为完整程序验证分类器 def datingClassTest(): hoRatio = 0.10 #设置测试集比重,前10%作为测试集,后90%作为训练集 datingDataMat,d ...