Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11397 | Accepted: 3630 |
Description
In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, 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 an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity.
Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.
Input
On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n).
There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]
Output
The only line of output will contain the number of the soldiers who have to get out of the line.
Sample Input
8
1.86 1.86 1.30621 2 1.4 1 1.97 2.2
Sample Output
4
题意:给n个士兵的身高,要求每个士兵向左或向右能看向无穷远处(新队列呈三角形分布),最少要剔除几个士兵; 思路:对数列分别顺序,逆序求最长上升子序列,然后枚举i和 j,使得以i结尾的上升子序列与以j开头的下降子序列的和最大;
#include<stdio.h>
#include<string.h> int main()
{
double a[];
int n;
while(~scanf("%d",&n))
{
for(int i = ; i <= n; i++)
scanf("%lf",&a[i]);
int dp_left[],dp_right[]; //顺序dp求上升子序列
for(int i = ; i <= n; i++)
{
dp_left[i] = ;
for(int j = ; j < i; j++)
{
if(a[i] > a[j] && dp_left[i] < dp_left[j]+)
dp_left[i] = dp_left[j]+; }
} //逆序dp求下降子序列;
for(int i = n; i >= ; i--)
{
dp_right[i] = ;
for(int j = n; j > i; j--)
{
if(a[i] > a[j] && dp_right[i] < dp_right[j]+)
dp_right[i] = dp_right[j]+;
}
} //枚举上升子序列的右端点和下降子序列的左端点;
int sum = -;
for(int i = ; i <= n-; i++)
{
for(int j = i+; j <= n; j++)
{
if(dp_left[i]+dp_right[j] > sum)
sum = dp_left[i]+dp_right[j];
}
}
printf("%d\n",n-sum);
}
return ;
}
sdut 2403 与上题有些类似;
单峰序列
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
明明最近遇到一个数学问题:给定n个数字(A1,A2,A3......An),每个数字均是小于2^31的正整数,现需要知道这n个数字中的最长单峰子序列长度是多少。所谓单峰序列是指满足如下条件之一的子序列:
(1)Ak1<Ak2<Ak3<......<Akm (k1,k2,k3....km均在1到n之间)
(2)Ak1>Ak2>Ak3>......>Akm (k1,k2,k3....km均在1到n之间)
(3)Ak1<Ak2<Ak3<...<Amid-1<Amid>Amid+1>...>Akm-2>Akm-1>Akm (k1,k2,k3....km均在1到n之间)
现在明明很忙,他想请你帮他解决这个问题,而解决这个问题的好处是他可以让你此时此刻多获得一个彩色气球。你能帮他吗?
(1)Ak1<Ak2<Ak3<......<Akm (k1,k2,k3....km均在1到n之间)
(2)Ak1>Ak2>Ak3>......>Akm (k1,k2,k3....km均在1到n之间)
(3)Ak1<Ak2<Ak3<...<Amid-1<Amid>Amid+1>...>Akm-2>Akm-1>Akm (k1,k2,k3....km均在1到n之间)
现在明明很忙,他想请你帮他解决这个问题,而解决这个问题的好处是他可以让你此时此刻多获得一个彩色气球。你能帮他吗?
输入
输入包含多组数据,每组数据的格式如下:
一个正整数n(1<=n<=1000),表示有多少个数字。
紧跟一行有n个正整数A1,A2....An。(0<=Ai<=2^31)
一个正整数n(1<=n<=1000),表示有多少个数字。
紧跟一行有n个正整数A1,A2....An。(0<=Ai<=2^31)
输出
对于每组输入,输出一个正整数ans,表示该组数据中最长单峰子序列的长度是多少。每个输出占一行。
示例输入
2
1 2
3
1 3 2
4
1 5 4 6
示例输出
2
3
3
#include<stdio.h>
#include<string.h> int main()
{
int a[];
int n;
while(~scanf("%d",&n))
{
for(int i = ; i <= n; i++)
scanf("%d",&a[i]);
int dp_left[],dp_right[];
for(int i = ; i <= n; i++)
{
dp_left[i] = ;
for(int j = ; j < i; j++)
{
if(a[i] > a[j] && dp_left[i] < dp_left[j]+)
dp_left[i] = dp_left[j]+; }
} for(int i = n; i >= ; i--)
{
dp_right[i] = ;
for(int j = n; j > i; j--)
{
if(a[i] > a[j] && dp_right[i] < dp_right[j]+)
dp_right[i] = dp_right[j]+;
}
} int sum = -;
for(int i = ; i <= n; i++)
{
int tmp = dp_left[i]+dp_right[i]-;
if(sum < tmp)
sum = tmp;
}
printf("%d\n",sum);
}
return ;
}