Pear Trees

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

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、两个子序列没有共同元素且元素数目和为 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;
}
MATLAB主噪声和振控制算法——对较大的次级路径变化具有鲁棒性内容概要:本文主要介绍了一种在MATLAB环境下实现的主噪声和振控制算法,该算法针对较大的次级路径变化具有较强的鲁棒性。文中详细阐述了算法的设计原理与实现方法,重点解决了传统控制系统中因次级路径态变化导致性能下降的问题。通过引入自适应机制和鲁棒控制策略,提升了系统在复杂环境下的稳定性和控制精度,适用于需要高精度噪声与振抑制的实际工程场景。此外,文档还列举了多个MATLAB仿真实例及相关科研技术服务内容,涵盖信号处理、智能优化、机器学习等多个交叉领域。; 适合人群:具备一定MATLAB编程基础和控制系统理论知识的科研人员及工程技术人员,尤其适合从事噪声与振控制、信号处理、自化等相关领域的研究生和工程师。; 使用场景及目标:①应用于汽车、航空航天、精密仪器等对噪声和振敏感的工业领域;②用于提升现有主控制系统对参数变化的适应能力;③为相关科研项目提供算法验证与仿真平台支持; 阅读建议:建议读者结合提供的MATLAB代码进行仿真实验,深入理解算法在不同次级路径条件下的响应特性,并可通过调整控制参数进一步探究其鲁棒性边界。同时可参考文档中列出的相关技术案例拓展应用场景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值