Codesforces 467E Alex and Complicated Task

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 = 0; 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) >= 2) ? 1 : 0;
                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 = 0  ; i < res.size(); ++i ) cout << res[i] << ' '; cout << endl;
}

 

转载于:https://www.cnblogs.com/hlmark/p/3990518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值