POJ 1836 Alignment

Source: Internet
Author: User

Links: http://poj.org/problem?id=1836

Alignment
Time limit:1000ms Memory limit:30000ktotal submissions:14457 accepted:4690
DescriptionIn the army, a platoon was composed by N soldiers. During the morning inspection, the soldiers is aligned in a straight line in front of the captain. The captain is not a satisfied with the A-soldiers is aligned; It was true that the soldiers was aligned in order by their code number:1, 2, 3, ..., N, but they was not aligned B Y their height. The captain asks some soldiers to get out of the the line, as the soldiers that remain on the line, without changing their pla CES, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the Line ' s extremity (left or right). A soldier see a extremity if there is ' t any soldiers with a higher or equal height than he height between him and that Extremity.

Write a program this, knowing the height of each soldier, determines the minimum number of soldiers which has to get out of line.


InputThe first line of the input is written the number of the soldiers N. On the second line was written a series of n floating numbers with at most 5 digits precision and separated by a space char Acter. The k-th number from the "This" line represents the height of the soldier, who has the code K (1 <= k <= N).

There is some restrictions:
? 2 <= N <= 1000
? The height is floating numbers from the interval [0.5, 2.5]

OutputThe only line of output would contain the number of the soldiers who has to get out of the the line .

Sample Input8
1.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output4

Source
Romania OI 2002

The main idea-draw some people from the team, so that everyone left at least can see the end of the side. Q: Given a number n, indicating the length of the team, but also known to the players of each of their own high, the minimum number of people to meet the above conditions.

Idea--the problem is converted to a maximum number of columns that are strictly decremented from the middle to the end. Then we can find the longest increment subsequence from left to right and right to left for this column, and then get the longest sequence descending from the middle to the end so this is similar to POJ 2533. The result is n minus the length of the longest sequence.

Complexity analysis--time complexity: O (n^2), spatial complexity: O (n)

Attach the AC code:


#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip > #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include < algorithm> #include <queue> #include <vector> #include <set> #include <map>using namespace std;typedef unsigned int ui;typedef long long ll;typedef unsigned long long ull;typedef long double ld;const double pi = a cos ( -1.0); const DOUBLE E = exp (1.0); const int MAXN = 1005;int PREV[MAXN], NEXT[MAXN]; The length of the LIS, respectively, from left and right, double HIGH[MAXN]; The height of the soldier int main () {Ios::sync_with_stdio (false); int N;while (scanf ("%d", &n)! = EOF) {for (int i=0; i<n; i++) scanf (" %lf ", &high[i]);p rev[0] = next[n-1] = 1;for (int i=1; i<n; i++) {//From left to lis length prev[i] = 1;for (int j=0; j<i; j + +) if (High[j] < high[i]) prev[i] = max (Prev[i], prev[j]+1);} for (int i=n-2; i>=0; i--) {//from the right to find lis length next[i] = 1;for (int j=n-1; j>i; j--) if (High[j] < high[i]) next[I] = max (Next[i], next[j]+1);} int ans = 0;for (int i=0; i<n-1; i++) for (int j=i+1; j<n; j + +) ans = max (ans, prev[i]+next[j]);p rintf ("%d\n", N-ans); }return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1836 Alignment

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.