[AGC001D]Arrays and Palindrome-并查集-构造
来源:互联网 发布:单机手游推荐不要网络 编辑:程序博客网 时间:2024/06/01 09:00
Arrays and Palindrome
Problem Statement
Snuke got a present from his mother on his birthday. The present was a pair of two sequences a and b, consisting of positive integers. They satisfied all of the following properties:
The sum of all elements of a is N.
The sum of all elements of b is N.
Any string of length N that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).
(1) Any of the following forms a palindrome: the first a1 letters, the following a2 letters, the following a3 letters and so on.
(2) Any of the following forms a palindrome: the first b1 letters, the following b2 letters, the following b3 letters and so on.
(3) All N letters are the same.
He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence a was a permutation of another sequence A of length M.
To bring him happiness again, his mother has decided to give him another pair of sequences a and b that satisfies his favorite properties and is consistent with his memory.
Constraints
1≦N≦105
1≦M≦100
1≦Ai≦105
The sum of all Ai equals N.
Input
The input is given from Standard Input in the following format:
N MA1 A2 … AM
Output
If there exists a pair of sequences a and b that satisfies the properties and is consistent with Snuke’s memory, print three lines. The first line must contain the sequence a, the second line must contain the length of the sequence b, and the third line must contain the sequence b.
If such a pair does not exist (because Snuke’s memory is wrong or some other reason), print a single line containing the word Impossible (case-sensitive).
Sample Input 1
3 22 1
Sample Output 1
1 213
Sample Input 2
6 16
Sample Output 2
631 2 3
Sample Input 3
55 101 2 3 4 5 6 7 8 9 10
Sample Output 3
Impossible
咱至今都记得在同学出的NOIP模拟赛中被这题狂虐花掉整整1h才A的快感……
做完以后看着奇短无比的代码整个人都是蒙圈的……
题意:
给出一个数字之和为N的顺序可变的
1.从头开始的
2.从头开始的
3.该字符串仅由同一字符构成。
思路:
画蚊香。(咱和同学都喜欢这么叫)
上图你们就知道什么意思了。
鼠绘太丑不要在意。
假设红线是
那么上图的
可以发现,可利用的回文关系可以很轻松画成上面的蚊香型图。
根据并查集思想,如果整张图能一笔画,那么就是有解。
那么接下来呢?
多画几张这样的蚊香图,可以发现,长度为奇数的回文串很没有前途。
有多没有前途呢?
没有前途到只能放在队首或队尾,超过两个就无解了。
那么无解的情况完成了,接下来就是构造
可以考虑这么构造:
把
相当于这样:
3 2 4 6 4 2 34 2 4 6 4 2 2
很明显中间的相对的
前端和后端构成了仅有
那么就做完了~
特殊情况记得特判即可。
(考试时写的代码有点奇怪不要在意)
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;inline int read(){ int x=0;char ch=getchar(); while(ch<'0' || '9'<ch)ch=getchar(); while('0'<=ch && ch<='9')x=x*10+(ch^48),ch=getchar(); return x;}const int M=1e5+9;int m;int a[M],b[M],top;int main(){ int n=read(),m=read(); int odd1=-1,odd2=-1; for(int i=1;i<=m;i++) { a[i]=read(); if(a[i]&1) { if(odd1==-1) odd1=i; else if(odd2==-1) odd2=i; else { puts("Impossible"); return 0; } } } if(m==1) { if(a[1]==1) printf("1\n1\n1\n"); else printf("%d\n2\n1 %d\n",a[1],a[1]-1); return 0; } if(odd1!=-1) swap(a[1],a[odd1]); if(odd2!=-1) swap(a[m],a[odd2]); for(int i=1;i<=m;i++) printf("%d%c",a[i]," \n"[i==m]); if(a[m]==1) { printf("%d\n",m-1); printf("%d ",a[1]+1); for(int i=2;i<m;++i) printf("%d ",a[i]); puts(""); return 0; } b[1]=a[1]+1; for(int i=2;i<m;i++) b[i]=a[i]; b[m]=a[m]-1; printf("%d\n",m); for(int i=1;i<=m;i++) printf("%d%c",b[i]," \n"[i==m]); return 0;}
- [AGC001D]Arrays and Palindrome-并查集-构造
- [agc001d]Arrays and Palindrome
- [构造] Atcoder AGC001 D. Arrays and Palindrome
- #9 (Div. 2 Only) E. Interesting Graph and Apples (并查集+构造图)
- bzoj 1116(构造+并查集)
- 【并查集】构造完全图
- 并查集+hdu(1232 and 1213)
- xi and bo (并查集问题)
- hdu5627 Clarke and MST (并查集)
- HDU5652India and China Origins 并查集
- Vegetable and Road [并查集]
- CF400D Dima and Bacteria【并查集】
- 【jzoj5359】【NOIP2017提高A组模拟9.12】【Arrays and Palindrome】
- 并查集+模拟构造 sgu 449. Dendrograms
- Codeforces #345 div1 C. Table Compression 并查集 构造
- POJ1703-Find them, Catch them 并查集构造
- Codeforces 9E Interesting Graph and Apples(并查集)
- NEU 1293: Gang Black and Gang White 并查集
- 突然想写些东西
- Java中super关键字和this关键字的比较
- Linux下安装epics中Base-3.15.5步骤(向胡勇老师致敬)
- B-树详细分析及B树B-树B+树B*树概念
- python lock
- [AGC001D]Arrays and Palindrome-并查集-构造
- linux下添加定时任务
- 卷积在图像处理中的应用
- [LintCode]39.恢复旋转排序数组
- 一张图讲解对象锁和关键字synchronized修饰方法(代码块)
- c++的模板类有什么好处?
- [AHK]用xmlpath方式解析xml的值
- BP神经网络拟合函数
- UVA 136 Ugly Number(优先队列)