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. [转] Android LocalService与RemoteService理解

    前段时间被别人问到相关的问题,没有回答对,发现自己原来理解的有偏差,最近看了下,写了个小Demo实验了下,现在将其记录下来,以后千万别犯同样的错误就好了. 一.LocalService(本地服务) 不 ...

  2. linux下sed命令笔记

    sed 流编辑器 Stream EDitor三大文本处理工具:grep,sed,awk 语法:sed 'AddressCommand' file ...Address:    1,StartLine, ...

  3. 基于JAVA网络编程的聊天小程序

    package com.neusoft.edu.socket; import java.io.BufferedReader; import java.io.IOException; import ja ...

  4. TFS 服务器更换后工作区无法绑定

    需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...

  5. Android常用第三方框架

    1.volley (截击) 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JSON,图像等的异步下载: (2)  网络请求的排序(sch ...

  6. ASP.NET Excel数据导入数据库

    <identity impersonate="true"/> 是指模拟IIS身份验证 導入錯誤時可刪除 protected void btnImport_Click(o ...

  7. 【转】iOS-Core-Animation-Advanced-Techniques(六)

    原文:http://www.cocoachina.com/ios/20150106/10839.html 基于定时器的动画和性能调优 基于定时器的动画 我可以指导你,但是你必须按照我说的做. -- 骇 ...

  8. Java如何连接到MySQL数据库的

    下载:mysql-connector-java-5.1.38.tar.gz http://dev.mysql.com/downloads/connector/j/ tar zxvf mysql-con ...

  9. [C#]获取最近在Windows上所使用的文件

    class RecentlyFileHelper { public static string GetShortcutTargetFile(string shortcutFilename) { var ...

  10. winform 窗体大小变化时,如何设置使控件一起按照比例变大

    public Form() { InitializeComponent(); + ; float[] factor = new float[count]; ; factor[i++] = Size.W ...