URAL1965: Pear Trees (DP), ural1965pear

Source: Internet
Author: User

URAL1965: Pear Trees (DP), ural1965pear
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 specified Ed around all n trees and found out that the numbers on the plaques are pairwise distinct and fit the limits from 1 to n. obviusly, 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 th 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 demo-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. inputThe 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. outputPrint 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

Input Output
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
SourceOpen Ural FU Personal Contest 2013

Question: There are n numbers, which are 1 ~ N: Can I find two sequences in this arrangement at will? The two sequences must be ascending or descending, and there cannot be repeated ideas: we perform the same increment and decrease respectively, incrementally and incrementally perform DP and record the path. The final output is enough.
Finally, I got it. People were tired and went to bed.
# 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 () # define LL long longconst double pi = acos (-1.0 );# Define Len 100005 # define mod 1000000007 const int INF = 0x3f3f3f3f; // 0 is incremental; 1 is decreasing struct node {int x, y;} s [Len] [2]; // record path int n; int a [Len]; int dp [Len] [2]; // dp [I] [j], which is saved in the j state, the ascending or descending sequence of position I. The first 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]) // The subscript of the array corresponds to the subscript + 1 of dp, that is to say, I corresponds to I + 1 upp (I + 1, j, I, j, dp [I] [j]); if (dp [I] [j] <a [I]) // if the increment does not meet the conditions of j, then we can see whether it can be saved to the condition of j ^ 1 upp (I + 1, j ^ 1, I, j, a [I-1]);} return dp [n] [0] + dp [n] [1] <INF;} // fully decreasing 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) {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;} // an increment, an increment 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 +, I, 0, dp [I] [0]); if (dp [I] [0]> a [I]) upp (I +, I, 0, a [I-1] );} If (dp [I] [1] <INF) {if (a [I-1]> a [I]) upp (I +, 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 () // if one of the three matches, {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 () // if the entire sequence is ascending or descending, extract one to {ans [I]. push_back (ans [I ^ 1]. back (); ans [I ^ 1]. pop_back () ;}} printf ("% 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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.