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
  1. 4
    3 5 3 5
output
  1. 4
    3 5 3 5
input
  1. 10
    35 1 2 1 2 35 100 200 100 200
output
  1. 8
    1 2 1 2 100 200 100 200

题目需要求一个最长的子序列 , 序列符合周期为4 ,然后奇数位,偶数位分别相等。

代码参考自美国coder : ecnerwal

  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> res;
  6. multiset<int> occ; //count number of occurrences
  7. map<int, int> wrap; // wrapper for each value
  8. vector<int> vals; // unwrapped values
  9.  
  10. void finish(int a, int b) {
  11. res.push_back(a);
  12. res.push_back(b);
  13. res.push_back(a);
  14. res.push_back(b);
  15. occ.clear();
  16. wrap.clear();
  17. vals.clear();
  18. }
  19.  
  20. int main() {
  21. int N;
  22. cin >> N;
  23. for(int i = ; i < N; i++) {
  24. int v; cin >> v;
  25. if(wrap.count(v)) {
  26. finish(wrap[v], v);
  27. continue;
  28. }
  29. else {
  30. if(occ.count(v)) {
  31. int cnt = (occ.count(v) >= ) ? : ;
  32. while(cnt || vals.back() != v) {
  33. if(vals.back() == v) cnt--;
  34. wrap[vals.back()] = v;
  35. vals.pop_back();
  36. }
  37. }
  38. occ.insert(v);
  39. vals.push_back(v);
  40. }
  41. }
  42. cout << res.size() << endl;
  43. for(int i = ; i < res.size(); ++i ) cout << res[i] << ' '; cout << endl;
  44. }

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. 微信小程序(18)-- 自定义头部导航栏

    最近做的项目涉及相应的页面显示相应的顶部标题,所以就需要自定义头部导航了. 首先新建一个顶部导航公用组件topnav,导航高度怎么计算? 1.wx.getSystemInfo 和 wx.getSyst ...

  2. Sass Maps的函数-map-keys($map)

    map-keys($map) 函数将会返回 $map 中的所有 key.这些值赋予给一个变量,那他就是一个列表.如: map-keys($social-colors); 其返回的值为: "d ...

  3. springboot使用异步查询数据

    主要适用于需要查询多种类型的数据,而且二者的参数没有关联的情况. 1.开启异步调用注解 2.创建抽象类,定义相关方法 /** * @author:YZH * time: 2019/8/8 12:16 ...

  4. [BZOJ5073] [Lydsy1710月赛]小A的咒语 后缀数组+dp+贪心

    题目链接 首先这种题一看就是dp. 设\(dp[i][j]\)表示\(A\)序列中到\(i\)位之前,取了\(j\)段,在\(B\)中的最长的长度. 转移也比较简单 \[ dp[i][j] \to d ...

  5. Kettle数据同步速度调优记录

    Msyql到Vertica 1.mysql中在openshop 数据库中选择其中一个300W左右数据的表 create table ip_records_tmp_01 AS SELECT * FROM ...

  6. Quartz.Net 任务调度之时间策略(4)

    在复杂的业务逻辑中,cron可以灵活的制定时间策略 先说使用方法 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trig ...

  7. jmeter+ant+jenkins搭建接口自动化测试环境

    jmeter+ant+jenkins搭建接口自动化测试环境(基于win) 1.jmeter jmeter依赖java运行环境,所以需要提前下载jdk并配置好环境变量 官网下载(http://jmete ...

  8. CF 1172E Nauuo and ODT ——LCT

    题目:http://codeforces.com/contest/1172/problem/E LCT好题. 考虑对每个颜色求出 “不是该颜色的点组成的连通块的 siz2 之和” .每个颜色用 LCT ...

  9. [杂题]:C/c(二分答案)

    题目传送门(内部题54) 输入格式 第一行一个整数表示$n$.第二行$n$个整数表示初始序列.(这行原题没有,是我加的)接下来$2n$行每行两个整数,分别表示$X_i,Y_i$.数据保证至少存在一种方 ...

  10. python input() 与raw_input()

    使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的1:纯数字输入 当输入为纯数字时 input返回的是数值类型,如int,float   ...