Two progressions CodeForce 125D 思维题

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

64 1 2 7 3 10

Output

1 2 3 4 7 10 

Input

51 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[30005],n;
bool vis[30005];
void print(vector<int> v)
{
    for(int i=0; i<v.size(); i++)    printf("%d ",v[i]);
    printf("\n");
}
bool check(vector<int> v)
{
    if(v.empty())    return false;
    else if(v.size()==1||v.size()==2)    return true;
    int d=v[1]-v[0];
    for(int i=1; i<v.size(); i++) {
        if(v[i]-v[i-1]!=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=-1;
    for(int i=0; i<=n; i++)    vis[i]=false;
    for(int i=1; i<=n; i++) {
        if(a[i]==get) {
            get+=d;
            v1.push_back(a[i]);
            last=i;
        } else {
            vis[i]=true;
        }
    }
    for(int i=1; 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=1; 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=1; i<=n; i++)    {
        scanf("%d",&a[i]);
    }
    if(n==2)    printf("%d\n%d",a[1],a[2]);
    else if(!solve(1,2)&&!solve(1,3)&&!solve(2,3))    printf("No solution\n");
    return 0;
}

原文地址:https://www.cnblogs.com/cautx/p/11616885.html

时间: 2024-11-06 18:58:18

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

Unique Encryption Keys (思维题 预处理)

题目 题意:给m个数字, q次询问, 询问b到e之间如果有重复数字就输出, 没有就输出OK 思路:用f[i]数组 记录从i开始向后最近的有重复数字的 位置, 如 1 3 2 2, 则f[1] = 4; 如果离a最近的重复数字的位置 都大于b, 就说明没有重复数字. f[]数组需要预处理,从后向前. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector>

sdut 2847 Monitor (思维题)

题目 题意:给定a, b, x, y;  求使c, d; 使c:d = x :y; 且c<=a, d<=b, 而且c, d尽量大. 先求最小倍数, 再用最小倍数乘 x, y; 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 long long gcd(long long a, l

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

学习方法_2011年编写和锻炼的思维题

1.多看,多练,多想,多总结,最重要就是不停的写代码! 给自己的目标:一天代码量最少800行,无论是什么代码,如果练习的量不够,就重复做已经写过的代码. 思维题: 找出这当中数字 1,11,31,4113,612314 的规律是怎样的? 1,11,表示前面的数有1个131,表示前面所有的数有3个14113,表示前面的所有的数有4个1.1个3以此类推,前面所有的数有6个1.2个3.1个4,即为612314 1.两个无窗的房间,其中一间有三个电灯,另一间里面有三个开关,三个开关各控制三个电灯.问:每

思维题 URAL 1718 Rejudge

题目传送门 1 /* 2 题意:数据加10组,再删掉第6组数据,问rejudge后最少最多几个作者收到邮件 3 思维题:当错在6时结果是不一定,错在7时是一定改变,因为会变成6 4 思路没错,但用结构题排序一直WA,代码有毒!学习使用set容器. 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #include <str

ZOJ 3829 贪心 思维题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题,自己智商不够,不敢搞,想着队友智商好,他们搞吧,但是没出来这题...... 以后任何时候,都自信点....该想的还是好好自己想,这类题感觉就是先去找性质,然后一点点找规律,如果必要的话,自己提出一点猜想,然后如果自己举不出来反例,就暂时认为是正确的 下午搞了一下午,发现还是悲剧,晚上参考了两个题解 http://blog.csdn.

ACM: Gym 101047K Training with Phuket&#39;s larvae - 思维题

Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Thai cuisine is known for combining seasonings so that every dish has flavors that are s

Acdreamoj1115(数学思维题)

题意:1,3是完美数,如果a,b是完美数,则2+a*b+2*a+2*b,判断给出的n是否是完美数. 解法:开始只看出来2+a*b+2*a+2*b=(a+2)*(b+2)-2,没推出更多结论,囧.没办法,只能暴力将所有的完美数求出来然后查表.正解是c+2=(a+2)*(b+2);完美数都是有质因子3或5组成的(5本身除外): 自己暴力代码: /****************************************************** * author:xiefubao *****

HDU5742 It&#39;s All In The Mind(思维题,水题)

Problem Description Professor Zhang has a number sequence a1,a2,...,an. However, the sequence is not complete and some elements are missing. Fortunately, Professor Zhang remembers some properties of the sequence: 1. For every i∈{1,2,...,n}, 0≤ai≤100.