Two progressions CodeForce 125D thinking title

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.

ID-OJ:
codeforces 125D

author:
Caution_X

DATE of submission:
20,191,002

Tags:
Thinking

description modelling:
Given a sequence, it is divided into two nonempty sequences s1, s2, (s1 + s2 = Collection), each sequence is arithmetic sequence if the two sequences can be output, the output No solution or

solutions:
(1) for each number, either a first sub-sequence, or in the second subsequences, according to pigeonhole principle, the first three elements into two columns must have the same number of two columns, wherein the number of elements is greater than the number of column 1 is formed a tolerance, and tolerance of a maximum of only three values d
(2) d wherein a tolerance to generate a arithmetic sequence, then the number of the remaining number of columns on the other, the other number of columns is not determined arithmetic sequence
(3) is an arithmetic sequence directly output, or the first number to a second number of trailing elements column column is determined whether it constitutes arithmetic sequence
(4) If the constitution is output, or other enumeration values tolerance

NOTE:
to (3) -> (4) wherein a number of columns in the trailing elements to another when the number of columns when the columns are not configured arithmetic enumeration should prove other tolerance:
moving from two arithmetic sequence was constructed over elements, it is assumed, is not formed after the first movement arithmetic sequence, a movement formed after the second arithmetic sequence, then the arithmetic sequence form of the new tolerance and tolerance build the original arithmetic sequence are equal, that is arithmetic sequence of this newly formed and the original arithmetic sequence can be placed in the same number of columns, namely: before the split into two sub-sequences, the original series is arithmetic sequence, since the original series is arithmetic sequence, then the first mobile after a series of new elements already is the arithmetic sequence, moving and we assume no first after the formation of the arithmetic sequence contradiction it follows that: the first element is not formed arithmetic sequence after the move, the next move no matter how much elements will not form the arithmetic series, so when the first mobile element is finished if the arithmetic series of another direct enumeration of tolerance.

 

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;
}

 

Guess you like

Origin www.cnblogs.com/cautx/p/11616885.html