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
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 (
Output
Print in the first line two integers between
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
题意是说让你在一个
1、两个子序列没有共同元素且元素数目和为
2、每个子序列都必须满足单调性(要么单调递增要么单调递减)
所以如果这样的两个子序列存在的话,总共就以下两种情况:
1、两个序列均为单调递增/单调递减序列
2、一个序列单调递增一个序列单调递减
对于情况1,可以用贪心处理,比如均单调递减,如果当前这个数比两个答案序列的尾巴都大,那么就把它接在更大的尾巴后面。
对于情况2,可以用动归处理:
转移如下:
在转移的时候记录方案就可以了,详见代码
#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;}
- Pear Trees
- URAL1965:Pear Trees(DP)
- pear
- pear
- PEAR
- [Open Ural FU Personal Contest 2013]E.Pear Trees wuyiqi's method
- trees
- 安装pear
- pear::QuickForm
- PEAR::Spreadsheet_Excel_Writer
- 关于pear
- PEAR Installation
- PEAR简介
- 升级pear
- install pear
- 安装Pear
- 安装PEAR
- pear 安装
- springmvc 整合 shiro
- Python_学习_字符串和数字
- javaScript--04 JavaScript基础&DOM 上
- C语言复合数据类型
- mybatis+oracle主键自增
- Pear Trees
- 多线程笔记
- 给groupBox添加滚动条
- Java提高篇(三四)-----fail-fast机制
- Window的对象Scroll
- Android Volley & Retrofit2 & Http基础知识 笔记
- 类初始化
- PTA 2-1 列出连通集【DFS+BFS基础】
- pycharm 快捷键