URAL1965:Pear Trees(DP)

来源:互联网 发布:淘宝优惠券在哪里领取 编辑:程序博客网 时间:2024/06/06 19:31
Vova was walking along one of Shenzhen streets when he noticed young pear trees, growing along the pavement. Each tree had a plaque attached to it containing some number. Vova walked around all n trees and found out that the numbers on the plaques are pairwise distinct and fit the limits from 1 to n. Obviously, the trees were first planned to be planted in the given order, but at the moment they were strangely mixed: the sixth one, then the fourth one, then the third one, then the fifth one...
There was an ice cream tray nearby. The ice cream seller noticed Vova staring at the pear trees with a bewildered look and told him that he saw the trees planted. The foreman entrusted two workers with the task and told them to plant the trees according to the numbers on the plaques. Then he left and the workers divided the trees between themselves and started working. As the foreman wasn't specific about the increasing or decreasing order of numbers on the plaques, each worker made his own decision without consulting the partner. Both workers planted trees moving in the same direction, from the same end of the street.
Let's look at the sample. Let's assume that n = 8 and the workers divided the trees between themselves in such a way that the first one got trees number 1, 4 and 5 and the second one got trees number 2, 3, 6, 7 and 8. As a result, they got such sequence of numbers on the plaques: 8, 7, 1, 6, 4, 3, 5, 2 (the first one planted the trees in the increasing order and the second one planted the trees in the decreasing order).
Vova wrote down all numbers on the plaques in the order, in which the trees were planted and wanted to determine which trees were planted by the first worker and which trees were planted by the second one. Help him to do this.

Input

The first line contains an integer n that is the number of trees (3 ≤ n ≤ 100 000). The second line contains n distinct integers between 1 and n describing the number permutation Vova wrote.

Output

Print in the first line two integers between 1 and (n − 1) that are the number of trees the first and the second worker planted, respectively. In the second line print the numbers of the trees planted by the first worker, in the third line print the number of the trees planted by the second worker. The numbers must follow in the order, in which the worker planted these trees. If there are multiple solutions, you may print any of them. If there's no solution, print “Fail”.

Sample Input

inputoutput
88 7 1 6 4 3 5 2
3 51 4 58 7 6 3 2
63 5 1 2 6 4
3 33 5 61 2 4
63 5 2 1 6 4
Fail

Source

Open Ural FU Personal Contest 2013


题意:有n个数,分别是1~n,随意排列,问能不能在这个排列中找出两个序列,两个序列必须是递增或者递减,而且不能有重复的
思路:我们分别对同递增,同递减,一递增一递减进行DP并记录路径,最后输出即可

总算搞出来了,人也很累了,睡觉去了

#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <algorithm>using namespace std;#define ls 2*i#define rs 2*i+1#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define w(a) while(a)#define LL long longconst double pi = acos(-1.0);#define Len 100005#define mod 1000000007const int INF = 0x3f3f3f3f;//0为递增,1为递减struct node{    int x,y;} s[Len][2];//记录路径int n;int a[Len];int dp[Len][2];//dp[i][j],保存的就是看在j状态下,第i个位置所在的连续递增或递减序列,最前面那个比这个连续序列第一个大或者小的数vector<int> ans[2];void upp(int x,int y,int i,int j,int m){    if(dp[x][y]>m)    {        dp[x][y] = m;        s[x][y].x = i;        s[x][y].y = j;    }}void downn(int x,int y,int i,int j,int m){    if(dp[x][y]<m)    {        dp[x][y] = m;        s[x][y].x = i;        s[x][y].y = j;    }}//全递增int set1(){    int i,j;    dp[1][0] = dp[1][1] = -1;    up(i,1,n-1)    {        dp[i+1][0]=dp[i+1][1] = INF;        up(j,0,1)        {            if(dp[i][j]<INF)            {                if(a[i-1]<a[i])//数组的下标对应dp的下标+1,也就是说i对应i+1                    upp(i+1,j,i,j,dp[i][j]);                if(dp[i][j]<a[i])//如果不满足j条件下的递增,那么就看能否保存到j^1的条件下                    upp(i+1,j^1,i,j,a[i-1]);            }        }    }    return dp[n][0]+dp[n][1]<INF;}//全递减int set2(){    int i,j;    dp[1][0] = dp[1][1] = INF;    up(i,1,n-1)    {        dp[i+1][0]=dp[i+1][1] = -1;        up(j,0,1)        {            if(dp[i][j]>=0)            {                if(a[i-1]>a[i])                    downn(i+1,j,i,j,dp[i][j]);                if(dp[i][j]>a[i])                    downn(i+1,j^1,i,j,a[i-1]);            }        }    }    return dp[n][0]+dp[n][1]>0;}//一个递增,一个递减int set3(){    int i,j;    dp[1][0] = INF;    dp[1][1] = -1;    up(i,1,n-1)    {        dp[i+1][0] = -1;        dp[i+1][1] = INF;        if(dp[i][0]>0)        {            if(a[i-1]<a[i])                downn(i+1,0,i,0,dp[i][0]);            if(dp[i][0]>a[i])                upp(i+1,1,i,0,a[i-1]);        }        if(dp[i][1]<INF)        {            if(a[i-1]>a[i])                upp(i+1,1,i,1,dp[i][1]);            if(dp[i][1]<a[i])                downn(i+1,0,i,1,a[i-1]);        }    }    return dp[n][0]>0 || dp[n][1]<INF;}void solve(int x,int y){    if(x<=0) return;    ans[y].push_back(a[x-1]);    solve(s[x][y].x,s[x][y].y);}int main(){    int i,j,k;    w(~scanf("%d",&n))    {        mem(s,0);        up(i,0,n-1)        scanf("%d",&a[i]);        if(set1() || set2() || set3())//三个只要有一个符合即可        {            ans[0].clear();            ans[1].clear();            if(dp[n][0]>=1 && dp[n][0]<=n) solve(n,0);            else solve(n,1);            up(i,0,1)            {                if(ans[i].empty())//如果整个序列是递增或者递减,那么取出一个来                {                    ans[i].push_back(ans[i^1].back());                    ans[i^1].pop_back();                }            }            printf("%d %d\n",ans[0].size(),ans[1].size());            up(i,0,1)            {                down(j,ans[i].size()-1,0)                {                    printf("%d",ans[i][j]);                    if(j)                        printf(" ");                }                printf("\n");            }        }        else            puts("Fail");    }    return 0;}


0 0
原创粉丝点击