Hundred Practice/2017 computer Science summer camp on-Machine exam: E (longest ordered sub-column)

Source: Internet
Author: User
Tags time limit

Source: http://noi.openjudge.cn/ch0206/4977/ 4977: The flying wing of the strange Thief Kidd

Total time limit: 1000ms memory limit: 65536kB

Description

The Pirate Kidd is a legendary thief with a special focus on jewelry as a super thief. His most prominent place was that he was able to escape the heavy containment of the village police, which was largely thanks to the easy-to-operate glider he was carrying.

One day, the strange thief Kidd as usual stole a precious diamond, but was Conan children saw through the camouflage, and his glider wing of the power plant was Conan kicked out of the football destroyed. The last resort was to escape from the damage of the gliding wing only.

Suppose a total of n buildings in a city are lined up in a single line, each building has a different height. At the beginning, The strange thief Kidd can be at the top of any building. He can escape in one direction, but cannot change direction halfway (because the senjing will chase behind). As the glider was damaged, he could only glide down (ie: only from the higher building to the lower building). He wants to go through the top of different buildings as much as possible, which can slow down the impact and reduce the likelihood of injury. Ask how many different buildings he can pass at the top (including the original building).

input

The first line of input data is an integer k (k < 100), which represents a K-group of test data.
Each set of test data contains two lines: The first line is an integer n (n < 100), representing N buildings. The second line contains n different integers, each of which corresponds to the height of a building H (0 < H < 10000), given in the order in which the buildings are arranged.

Output

For each set of test data, the output line contains an integer that represents the maximum number of buildings that can be passed by the monster Thief Kidd.

Sample Input

3

8

300 207 155 299 298 170 158 65

8

65 158 170 298 299 155 207 300

10

2 1 3 4 5 6 7 8 9 10

Sample Output

6

6

9

-----------------------------------------------------

Thinking of solving problems

The longest ordered child column problem

Dynamic planning

From the No. 0 element, computes the longest ordered child at the end of the I element, computes the nth longest ordered child (a[0]~a[n-1], and calculates the result

Note There are 2 escape directions: Left to right/right to left, so calculate 2 times of dynamic planning

-----------------------------------------------------

Code

The longest ordered sub-column problem//Note that both positive and reverse order are possible, so do 2 times #include <iostream> #include <fstream> #include <vector> #include <

Map> using namespace std;
	int main () {#ifndef Online_judge int n, I, K, J, JJ, TMP, ANS1, ANS2;
	Vector<int> Buil;
	Ifstream fin ("xly2017E.txt");
	Fin >> N;
		for (i=0; i<n; i++) {fin >> k;
		Buil.clear ();
			for (j=0; j<k; J + +) {fin >> tmp;
		Buil.push_back (TMP);
			} if (k==1) {cout << 1 << endl;
		Continue
		} int *substr = new Int[k];
		Substr[0] = 1;
			Maximum ascending child column length for (j=1; j<k; J + +) {tmp = 0;  for (JJ = j-1; JJ >= 0; jj--) {if (buil.at (JJ) < buil.at (j) && SUBSTR[JJ] > tmp) {tmp =
				SUBSTR[JJ];
		}} Substr[j] = tmp+1;
		} tmp = 0;
			for (j=0; j<k; J + +) {if (Substr[j] > tmp) {tmp = Substr[j];
		}} ans1 = tmp;
			Maximum descending child column length for (j=1; j<k; J + +) {tmp = 0; for (JJ = j-1; JJ >= 0; jj--) {if (BUIL.AT (JJ) > buil.at (j) && SUBSTR[JJ] > tmp) {tmp = SUBSTR[JJ];
		}} Substr[j] = tmp+1;
		} tmp = 0;
			for (j=0; j<k; J + +) {if (Substr[j] > tmp) {tmp = Substr[j];
		}} ANS2 = tmp;
		cout << ((ans1>ans2) ans1:ans2) << Endl;
	Delete[] substr;
	} fin.close ();
return 0;
	#endif #ifdef Online_judge int n, I, K, J, JJ, TMP, ANS1, ANS2;
	Vector<int> Buil;
	CIN >> N;
		for (i=0; i<n; i++) {cin >> K;
		Buil.clear ();
			for (j=0; j<k; J + +) {cin >> tmp;
		Buil.push_back (TMP);
			} if (k==1) {cout << 1 << endl;
		Continue
		} int *substr = new Int[k];
		Substr[0] = 1;
			Maximum ascending child column length for (j=1; j<k; J + +) {tmp = 0;  for (JJ = j-1; JJ >= 0; jj--) {if (buil.at (JJ) < buil.at (j) && SUBSTR[JJ] > tmp) {tmp =
				SUBSTR[JJ];
		}} Substr[j] = tmp+1;
		} tmp = 0; for (j=0; j<k; J + +) {if (Substr[j] > tmp) {
				TMP = Substr[j];
		}} ans1 = tmp;
			Maximum descending child column length for (j=1; j<k; J + +) {tmp = 0;  for (JJ = j-1; JJ >= 0; jj--) {if (buil.at (JJ) > buil.at (j) && SUBSTR[JJ] > tmp) {tmp =
				SUBSTR[JJ];
		}} Substr[j] = tmp+1;
		} tmp = 0;
			for (j=0; j<k; J + +) {if (Substr[j] > tmp) {tmp = Substr[j];
		}} ANS2 = tmp;
		cout << ((ans1>ans2) ans1:ans2) << Endl;
	Delete[] substr;
} return 0; #endif}


Related Article

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.