Codesforces 467E Alex and Complicated Task
2 seconds
256 megabytes
standard input
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.
The first line contains a single integer n (1 ≤ n ≤ 5·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
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.
4
3 5 3 5
4
3 5 3 5
10
35 1 2 1 2 35 100 200 100 200
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的更多相关文章
- Codeforces 461D. Appleman and Complicated Task 构造,计数
原文链接https://www.cnblogs.com/zhouzhendong/p/CF461D.html 题解 首先我们可以发现如果确定了第一行,那么方案就唯一了. 然后,我们来看看一个点的值确定 ...
- CF461D-Appleman and Complicated Task【并查集】
正题 题目链接:https://www.luogu.com.cn/problem/CF461D 题目大意 \(n*n\)的网格需要填上\(x\)或\(o\),其中有\(k\)个格子已经固定,求有多少中 ...
- [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 ...
- [C6] Andrew Ng - Convolutional Neural Networks
About this Course This course will teach you how to build convolutional neural networks and apply it ...
- Unity3D游戏在iOS上因为trampolines闪退的原因与解决办法
http://7dot9.com/?p=444 http://whydoidoit.com/2012/08/20/unity-serializer-mono-and-trampolines/ 确定具体 ...
- 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 ...
- D. Ilya and Escalator
D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #263
http://codeforces.com/contest/461 A.水题 B.太挫了,竟然被hack了一发.... C.贪心..竟然没看出来时哈夫曼编码问题 D.题目大意:给一棵树,每一个点为白色 ...
- The Go Programming Language. Notes.
Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...
随机推荐
- css标签学习-vertical-align标签
今天在学习查阅代码的时候,发现了一个不认识的CSS代码,于是进行学习. <html> <head> <style type="text/css"> ...
- JS高级 —— 普通函数、构造函数、对象方法的调用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 233-基于TMS320C6678+XC7K325T的CPCIe开发平台
基于TMS320C6678+XC7K325T的CPCIe开发平台 一.板卡概述 该DSP+FPGA高速信号采集处理板由我公司自主研发,包含一片TI DSP TMS320C6678和一片 ...
- 8VC Venture Cup 2017 - Elimination Round - A
题目链接:http://codeforces.com/contest/755/problem/A 题意:给定一个正整数N,问你是否存在一个数M使得N*M+1不是素数(M的范围在[1,1000]). 思 ...
- Sass函数-数字函数-floor()函数
floor() 函数刚好与 ceil() 函数功能相反,其主要将一个数去除其小数部分,并且不做任何的进位.也就是只做舍,不做入的计算: >> floor(2.1) 2 >> f ...
- H2database创建表
语法和sql server大同小异 create table users(id int primary key not null int identity, name varchar(20))
- phpstorm ftp不能连接服务器
环境: ubuntu phpstorm 问题一. 服务器ftp功能没有开启 解决方法:在服务器上安装 ftp 服务 https://i.cnblogs.com/EditPosts.aspx?posti ...
- ThinkPHP import 类库导入 include PHP文件
ThinkPHP 模拟了 Java 的类库导入机制,统一采用 import 方法进行类文件的加载.import 方法是 ThinkPHP 内建的类库和文件导入方法,提供了方便和灵活的文件导入机制,完全 ...
- 08.@Scheduled定时任务、整合jdbcTemplate、mybatis区分多数据源
@Scheduled注解执行定时任务 import org.springframework.scheduling.annotation.Scheduled; import org.springfram ...
- Random 生成随机数
Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要 ...