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之间)
现在明明很忙,他想请你帮他解决这个问题,而解决这个问题的好处是他可以让你此时此刻多获得一个彩色气球。你能帮他吗?

输入

输入包含多组数据,每组数据的格式如下:
一个正整数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 ;
}

Alignment ( 最长上升(下降)子序列 )的更多相关文章

  1. 最长不下降子序列(LIS)

    最长上升子序列.最长不下降子序列,解法差不多,就一点等于不等于的差别,我这里说最长不下降子序列的. 有两种解法. 一种是DP,很容易想到,就这样: REP(i,n) { f[i]=; FOR(j,,i ...

  2. 最长不下降子序列 O(nlogn) || 记忆化搜索

    #include<stdio.h> ] , temp[] ; int n , top ; int binary_search (int x) { ; int last = top ; in ...

  3. tyvj 1049 最长不下降子序列 n^2/nlogn

    P1049 最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 ...

  4. 最长不下降子序列的O(n^2)算法和O(nlogn)算法

    一.简单的O(n^2)的算法 很容易想到用动态规划做.设lis[]用于保存第1~i元素元素中最长不下降序列的长度,则lis[i]=max(lis[j])+1,且num[i]>num[j],i&g ...

  5. 最长不下降子序列//序列dp

    最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 最长不下降 ...

  6. 【tyvj】P1049 最长不下降子序列

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数 第二行n个数 输出格式 最长不下降子序列的长度 测 ...

  7. hdu 4604 Deque(最长不下降子序列)

    从后向前对已搜点做两遍LIS(最长不下降子序列),分别求出已搜点的最长递增.递减子序列长度.这样一直搜到第一个点,就得到了整个序列的最长递增.递减子序列的长度,即最长递减子序列在前,最长递增子序列在后 ...

  8. 最长不下降子序列nlogn算法详解

    今天花了很长时间终于弄懂了这个算法……毕竟找一个好的讲解真的太难了,所以励志我要自己写一个好的讲解QAQ 这篇文章是在懂了这个问题n^2解决方案的基础上学习. 解决的问题:给定一个序列,求最长不下降子 ...

  9. SPOJ 4053 - Card Sorting 最长不下降子序列

    我们的男主现在手中有n*c张牌,其中有c(<=4)种颜色,每种颜色有n(<=100)张,现在他要排序,首先把相同的颜色的牌放在一起,颜色相同的按照序号从小到大排序.现在他想要让牌的移动次数 ...

  10. SPOJ 3943 - Nested Dolls 最长不下降子序列LIS(二分写法)

    现在n(<=20000)个俄罗斯套娃,每个都有宽度wi和高度hi(均小于10000),要求w1<w2并且h1<h2的时候才可以合并,问最少能剩几个. [LIS]乍一看跟[这题]类似, ...

随机推荐

  1. 动态代理与AOP

    1. 代理的分类: 静态代理:每个代理类只能为一个接口服务 动态代理:可以通过一个代理类完成全部的代理功能(由JVM生成实现一系列接口的代理类,即:生成实现接口的类的代理) 2. 动态代理: 在Jav ...

  2. CentOS 通过yum来升级php到php5.6,yum upgrade php 没有更新包怎么办?

    在文章中,我们将展示在centOS系统下如何将php升级到5.6,之前通过yum来安装lamp环境,直接升级的话,提示没有更新包,也就是说默认情况下php5.3.3是最新 1.查看已经安装的php版本 ...

  3. PHP编译错误Don't know how to define struct flock on this system, set --enable-opcache=no

    编辑 /etc/ld.so.conf 加入 /usr/local/lib 再执行 ldconfig

  4. spring 定时任务的 执行时间设置规则

    单纯针对时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行时间,只需要设置其cronExpressio ...

  5. C#中的三种 加密解密

    刚刚学会的C#的加密与解密(三种)MD5加密/RSA加密与解密/DES加密.也是刚刚申请的blog随便发布一下. (一).MD5加密 MD5 md5 = new MD5CryptoServicePro ...

  6. sql根据'/'截取最后的字符串

    filpath字段值:/DataFile/UpLoad/Logo/NoPhoto.jpg select filpath,REVERSE((SUBSTRING(REVERSE(FilPath),0,CH ...

  7. ab安装和使用

    apache bench(专门用于 HTTP Server .单url).win8: 下载地址:http://httpd.apache.org/download.cgi#apache24 安装apac ...

  8. 【转】 iOS KVO KVC

    原文: http://www.cocoachina.com/industry/20140224/7866.html Key Value Coding Key Value Coding是cocoa的一个 ...

  9. 关于 ASP.NET 验证码

    Session["CheckCode"] 这个..不懂神马意思.. .创建一个用户控件 用户名:TextBox 密码: TextBox 验证码:TextBox 验证码图片 < ...

  10. Java设计模式(学习整理)----装饰模式

    1.概念: (在我看来,模式就像是是一种思想,在这种思想的指引下,对代码和结构的一番加工和整合而已!都是套路!) 装饰模式又称包装(Wrapper)模式,是以对客户端透明的方式扩展对象的功能,是继承关 ...