Pear Trees

来源:互联网 发布:数据库报表是什么 编辑:程序博客网 时间:2024/06/04 18:31

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45173
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Description

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 (3n100000). 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 (n1) 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

88 7 1 6 4 3 5 2
63 5 1 2 6 4
63 5 2 1 6 4

Sample Output

3 51 4 58 7 6 3 2
3 33 5 61 2 4
Fail

Source

Problem Author: Sergey Pupyrev
Problem Source: Open Ural FU Personal Contest 2013

Soution & Code

题意是说让你在一个 n 的排列中找到两个子序列,满足:
1、两个子序列没有共同元素且元素数目和为 n
2、每个子序列都必须满足单调性(要么单调递增要么单调递减)
所以如果这样的两个子序列存在的话,总共就以下两种情况:
1、两个序列均为单调递增/单调递减序列
2、一个序列单调递增一个序列单调递减
对于情况1,可以用贪心处理,比如均单调递减,如果当前这个数比两个答案序列的尾巴都大,那么就把它接在更大的尾巴后面。
对于情况2,可以用动归处理:
f[i] 表示 numb[i] 放在上升序列时,下降序列尾巴的最大值
g[i] 表示 numb[i] 放在下降序列时,上升序列尾巴的最小值
转移如下:
if(numb[i]>numb[i1])f[i]=max(f[i],f[i1])
if(numb[i]<numb[i1])g[i]=min(g[i],g[i1])
if(numb[i]>g[i1])f[i]=max(f[i],numb[i1])
if(numb[i]<f[i1])g[i]=min(g[i],numb[i1])
在转移的时候记录方案就可以了,详见代码

#include <cstdio>#include <algorithm>#include <queue>using namespace std;const int maxn = 1e6 + 5;const int inf = 1e6;int n, numb[maxn];int f_ans[maxn], g_ans[maxn], f_lnth, g_lnth;priority_queue<int> q1, q2;bool work1(){    while(!q1.empty()) q1.pop();    while(!q2.empty()) q2.pop();    q1.push(numb[1]);    for(int i = 2; i <= n; ++i){        if(q1.top() < numb[i]){            q1.push(numb[i]);        } else{            if(q2.empty()) q2.push(numb[i]);            else if(q2.top() < numb[i]) q2.push(numb[i]);                 else return false;        }    }    if(q1.size() == n){        q2.push(q1.top());        q1.pop();    }    f_lnth = q1.size();    g_lnth = q2.size();    for(int i = f_lnth; i >= 1; --i) f_ans[i] = q1.top(), q1.pop();    for(int i = g_lnth; i >= 1; --i) g_ans[i] = q2.top(), q2.pop();    printf("%d %d\n", f_lnth, g_lnth);    for(int i = 1; i <= f_lnth; ++i) printf("%d%c", f_ans[i], " \n"[i==f_lnth]);    for(int i = 1; i <= g_lnth; ++i) printf("%d%c", g_ans[i], " \n"[i==g_lnth]);}bool work2(){    while(!q1.empty()) q1.pop();    while(!q2.empty()) q2.pop();    q1.push(-numb[1]);    for(int i = 2; i <= n; ++i){        if(q1.top() < -numb[i]){            q1.push(-numb[i]);        } else{            if(q2.empty()) q2.push(-numb[i]);            else if(q2.top() < -numb[i]) q2.push(-numb[i]);                 else return false;        }    }    if(q1.size() == n){        q2.push(q1.top());        q1.pop();    }    f_lnth = q1.size();    g_lnth = q2.size();    for(int i = f_lnth; i >= 1; --i) f_ans[i] = -q1.top(), q1.pop();    for(int i = g_lnth; i >= 1; --i) g_ans[i] = -q2.top(), q2.pop();    printf("%d %d\n", f_lnth, g_lnth);    for(int i = 1; i <= f_lnth; ++i) printf("%d%c", f_ans[i], " \n"[i==f_lnth]);    for(int i = 1; i <= g_lnth; ++i) printf("%d%c", g_ans[i], " \n"[i==g_lnth]);}int f[maxn], g[maxn], f_path[maxn], g_path[maxn]; // path用来记录方案bool used[maxn];bool work3(){    for(int i = 1; i <= n; ++i) f[i] = 0;    for(int i = 1; i <= n; ++i) g[i] = inf;    f[1] = inf, g[1] = 0;    for(int i = 2; i <= n; ++i){        if(numb[i] > numb[i-1] && f[i-1] > f[i]) f[i] = f[i-1], f_path[numb[i]] = numb[i-1];        if(numb[i] < numb[i-1] && g[i-1] < g[i]) g[i] = g[i-1], g_path[numb[i]] = numb[i-1];        if(numb[i] > g[i-1] && numb[i-1] > f[i]) f[i] = numb[i-1], f_path[numb[i]] = g[i-1];        if(numb[i] < f[i-1] && numb[i-1] < g[i]) g[i] = numb[i-1], g_path[numb[i]] = f[i-1];    }    if(f[n] == 0 && g[n] == inf) return false; // 代表找不到这样的方案    if(f[n] == 0){        for(int i = numb[n]; i != 0 && i != inf; i = g_path[i]) g_ans[++g_lnth] = i, used[i] = true;        f_lnth = n - g_lnth;        printf("%d %d\n", g_lnth, f_lnth);        for(int i = g_lnth; i >= 1; --i) printf("%d%c", g_ans[i], " \n"[i==1]);    } else{        for(int i = numb[n]; i != 0 && i != inf; i = f_path[i]) f_ans[++f_lnth] = i, used[i] = true;        g_lnth = n - f_lnth;        printf("%d %d\n", f_lnth, g_lnth);        for(int i = f_lnth; i >= 1; --i) printf("%d%c", f_ans[i], " \n"[i==1]);    }    for(int i = 1; i <= n; ++i) if(!used[numb[i]]) printf("%d ", numb[i]); puts("");    return true;}int main(){    scanf("%d", &n);    for(int i = 1; i <= n; ++i) scanf("%d", &numb[i]);    if(work1()) return 0; // 均为单调上升序列    if(work2()) return 0; // 均为单调下降序列    if(work3()) return 0; // 一个上升一个下降    puts("Fail");    return 0;}
0 0
原创粉丝点击