An arithmetic progression is such a non-empty sequence of numbers where the difference between any two successive numbers is constant. This constant number is called common difference. For example, the sequence 3, 7, 11, 15 is an arithmetic progression. The definition implies that any sequences whose length equals 1 or 2 are arithmetic and all sequences whose length equals 0 are non-arithmetic.

You are given a sequence of different integers a1, a2, ..., an. You should either split it into two arithmetic progressions or find out that the operation is impossible to perform. Splitting assigns each member of the given sequence to one of two progressions, but the relative order of numbers does not change. Splitting is an inverse operation to merging.

Input

The first line contains a positive integer n (2 ≤ n ≤ 30000), n is the length of the given sequence. The second line contains elements of the given sequence a1, a2, ..., an ( - 108 ≤ ai ≤ 108). The elements of the progression are different integers.

Output

Print the required arithmetic progressions, one per line.
The progressions can be positioned in any order. Each progression
should contain at least one number. If there's no solution, then print
"No solution" (without the quotes)in the only line of the input file. If
there are several solutions, print any of them.

Examples

Input
6
4 1 2 7 3 10
Output
1 2 3 
4 7 10
Input
5
1 2 3 -2 -7
Output
1 2 3 
-2 -7

Note

In the second sample another solution is also possible (number three can be assigned to the second progression): 1, 2 and 3, -2, -7.

OJ-ID:
CodeForce 125D

author:
Caution_X

date of submission:
20191002

tags:
思维

description modelling:
给定一个序列,把它分成两个非空子序列s1,s2,(s1+s2=全集),每一个序列都是等差序列,如果可以输出两个子序列,否则输出No solution

解:
(1) 对每个数,要么在第一个子序列,要么在第二个子序列,根据鸽巢原理,将前三个元素放入两个数列必然有两个数在同一个数列,其中元素个数大于1的数列会形成一个公差,且公差d最多只有有三个值
(2) 根据其中一个公差d生成一个等差数列,然后将剩下的数放在另一个数列,判断另一个数列是不是等差数列
(3) 是等差数列则直接输出,否则将第一个数列尾部元素移到第二个数列,判断是否构成等差数列
(4) 如果构成则输出,否则枚举其他公差值

补充:
对(3)->(4)中将其中一个数列尾部元素移至另一个数列时若还不构成等差数列则应该枚举其他公差的证明:
从构造的等差数列里移动2个元素过来,假设,移动第一个后没有形成等差数列,移动第二个后形成了等差数列,那么这个新形成的等差数列公差和原来构建的等差数列的公差相等,也就是说这个新形成的等差数列和原来的等差数列可以放在同一个数列中,即:在拆成两个子序列之前,原数列就是等差数列,既然原数列是等差数列,那么在移动第一元素后新数列就已经是等差数列了,和我们假设的移动第一个后没有形成等差数列矛盾,因此得出:第一元素移动后没有形成等差数列,那么接下来无论移动多少个元素都不会形成等差数列,因此当第一个元素移动完之后若不是等差数列就直接枚举其他的公差。

AC CODE:

#include<bits/stdc++.h>
using namespace std;
int a[],n;
bool vis[];
void print(vector<int> v)
{
for(int i=; i<v.size(); i++) printf("%d ",v[i]);
printf("\n");
}
bool check(vector<int> v)
{
if(v.empty()) return false;
else if(v.size()==||v.size()==) return true;
int d=v[]-v[];
for(int i=; i<v.size(); i++) {
if(v[i]-v[i-]!=d) return false;
}
return true;
}
bool solve(int l,int r)
{
vector<int> v1,v2;
int d=a[r]-a[l],get=a[l],last=-;
for(int i=; i<=n; i++) vis[i]=false;
for(int i=; i<=n; i++) {
if(a[i]==get) {
get+=d;
v1.push_back(a[i]);
last=i;
} else {
vis[i]=true;
}
}
for(int i=; i<=n; i++) {
if(vis[i]) {
v2.push_back(a[i]);
}
}
if(check(v2)) {
print(v1);
print(v2);
return true;
} else {
v1.pop_back();
v2.clear();
vis[last]=true;
for(int i=; i<=n; i++) {
if(vis[i])
v2.push_back(a[i]);
}
if(check(v2)) {
print(v1);
print(v2);
return true;
}
}
return false;
}
int main()
{
//freopen("input1.txt","r",stdin);
//freopen("input2.txt","r",stdin);
scanf("%d",&n);
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
}
if(n==) printf("%d\n%d",a[],a[]);
else if(!solve(,)&&!solve(,)&&!solve(,)) printf("No solution\n");
return ;
}

Two progressions CodeForce 125D 思维题的更多相关文章

  1. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  2. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  3. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  4. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  5. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  6. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  8. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  9. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

随机推荐

  1. Python中yield解析

    小探yield 查看 python yield 文档 yield expressions: Using a yield expression in a function's body causes t ...

  2. JavaScript get set方法 ES5/ES6写法

    网上鲜有get和set的方法的实例,在这边再mark一下. get和set我个人理解本身只是一个语法糖,它定义的属性相当于“存储器属性” 为内部属性提供了一个方便习惯的读/写方式 ES5写法 func ...

  3. Gym 100851 Distance on Triangulation

    题意:给你一个N边形, 然后这个n边形有n-3条边,然后询问2点之间的最短路. 题解:分治. 我们可以找到一条边,使得这幅图能分成大小相同的2幅图,那么我们就可以确定那些被分割开的询问的答案是多少了. ...

  4. 牛客小白月赛5 D 阶乘 数学

    链接:https://www.nowcoder.com/acm/contest/135/D来源:牛客网 题目描述 输入描述: 输入数据共一行,一个正整数n,意义如“问题描述”. 输出描述: 输出一行描 ...

  5. 【LeetCode】[0002] 【两数之和】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 给出两个非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果 ...

  6. RobotFramework自动化测试框架-MongoDBLibrary库的使用

    笔者接着 RobotFramework自动化测试框架-DatabaseLibrary库的使用(对数据库的操作) 继续分享robotframework 对数据库中的MongoDB的详细操作. Mongo ...

  7. 【LeetCode】322-零钱兑换

    题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ...

  8. 数据库常用SQL语句(三):子查询

    一.为什么会使用子查询 虽然可以通过连接查询来实现多表查询数据记录,但不建议使用,因为连接查询的性能很差,为什么呢?我们来进行分析,例如 我们要查询部门表t_dept 和雇员表t_employee中的 ...

  9. Go操作kafka

    Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据,具有高性能.持久化.多副本备份.横向扩展等特点.本文介绍了如何使用Go语言发送和接收kafka消息. s ...

  10. 012 模块1-turtle库的使用

    目录 一.概述 二.turtle库基本介绍 2.1 turtle库概述 2.2 标准库 2.3 turtle的原(wan)理(fa) 2.4 turtle的魅力 三.turtle绘图窗体布局 3.1 ...