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. Eclipse的修改编码插件使用

    最近因为编码问题,很是纠结,终于找到了一个Eclipse的修改编码插件com.lifesting.tool.encoding_1.0.0.jar,使用感觉还不错,记录一下使用方法. 第一步 将插件co ...

  2. iOS UIKit:view

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  3. Bootstrap V3使用Tab标签

    Bootstrap V3使用Tab标签 >>>>>>>>>>>>>>>>>>>> ...

  4. python之enumerate枚举 第二篇(六):enumerate枚举

    [Python之旅]第二篇(六):enumerate枚举   python enumerate枚举 摘要: 1.普通情况下打印列表中索引号及其对应元素     使用下面的循环: 1 2 3 4 5 6 ...

  5. Rouh set 入门知识1(基础定义篇)

    粗糙集理论是继概率论.模糊集.证据论后又一处理不完整性和不确定性的数学工具,建立在分类机制的基础上.无需提供问题所处理的数据集合之外的任何先验信息条件.并且能有效分析不精确.不一致.不完整等各种不完备 ...

  6. 【转】到底EJB是什么

    [转]到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:&quo ...

  7. acl操作记录

    官方文档内容: 1.CREATE_ACL Procedure创建ACL Note: This procedure is deprecated in Oracle Database 12c. While ...

  8. Objective-C中的分类与协议

    分类 在谈分类之前,我们可以先探究下,OC中为什么出现分类这种机制,有什么好处? 假设你接到一个大项目:计算两个整数的和,差.接到任务的你马上动手.编写代码如下: #import <Founda ...

  9. 在模型中获取网络数据,刷新tableView

    model .h #import <Foundation/Foundation.h> #import "AFHTTPRequestOperationManager.h" ...

  10. Java中实现对象的比较:Comparable接口和Comparator接口

    在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...