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
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≤100000
). The second line contains
n
distinct integers between
Output
Print in the first line two integers between
Sample Input
8
8 7 1 6 4 3 5 2
6
3 5 1 2 6 4
6
3 5 2 1 6 4
Sample Output
3 5
1 4 5
8 7 6 3 2
3 3
3 5 6
1 2 4
Fail
Source
Problem Author: Sergey Pupyrev
Problem Source: Open Ural FU Personal Contest 2013
Soution & Code
题意是说让你在一个
n
的排列中找到两个子序列,满足:
1、两个子序列没有共同元素且元素数目和为
2、每个子序列都必须满足单调性(要么单调递增要么单调递减)
所以如果这样的两个子序列存在的话,总共就以下两种情况:
1、两个序列均为单调递增/单调递减序列
2、一个序列单调递增一个序列单调递减
对于情况1,可以用贪心处理,比如均单调递减,如果当前这个数比两个答案序列的尾巴都大,那么就把它接在更大的尾巴后面。
对于情况2,可以用动归处理:
f[i]
表示
numb[i]
放在上升序列时,下降序列尾巴的最大值
g[i]
表示
numb[i]
放在下降序列时,上升序列尾巴的最小值
转移如下:
if(numb[i]>numb[i−1])f[i]=max(f[i],f[i−1])
if(numb[i]<numb[i−1])g[i]=min(g[i],g[i−1])
if(numb[i]>g[i−1])f[i]=max(f[i],numb[i−1])
if(numb[i]<f[i−1])g[i]=min(g[i],numb[i−1])
在转移的时候记录方案就可以了,详见代码
#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;
}