Missile:双状态DP
题目
描写叙述
Long , long ago ,country A invented a missile system to destroy the missiles from their enemy . That system can launch only one missile to destroy multiple missiles if the heights of all the missiles
form a non-decrease sequence .
But recently , the scientists found that the system is not strong enough . So they invent another missile system . The new system can launch one single missile to destroy many more enemy missiles . Basically , the system can destroy the missile from near
to far . When the system is begun , it chooses one enemy missile to destroy , and then destroys a missile whose height is lower and farther than the first missile . The third missile to destroy is higher and farther than the second missile … the odd missile
to destroy is higher and farther than the previous one , and the even missile to destroy is lower and farther than the previous one .
Now , given you a list of the height of missiles from near to far , please find the most missiles that can be destroyed by one missile launched by the new system .
输入
The input contains multiple test cases .
In each test case , first line is an integer n ( 0<n<=1000 ) , which is the number of missiles to destroy . Then follows one line which contains n integers ( <=109 ) , the height of the missiles followed by distance .
The input is terminated by n = 0 .
输出
For each case , print the most missiles that can be destroyed in one line .
例子输入
4
5 3 2 4
3
1 1 1
0
例子输出
3
1
大意:
思路
完整代码:
#include<iostream>
using namespace std;
int a[1001],b[1001];
int str[1001];
int main()
{
//freopen("aa.txt","r",stdin);
int n,i,j,max2;
while(scanf("%d",&n)!=EOF)
{
if(!n)
break;
for(i=0;i<n;i++)
scanf("%d",&str[i]);
for(i=0;i<n;i++)
{
a[i]=1; //as higher
b[i]=0; //as lower
}
for(i=1;i<n;i++)
{
int max=1,max1=0;
for(j=0;j<i;j++)
{
if(str[i]>str[j])
{
a[i]=b[j]+1;
if(a[i]>max)
max=a[i];
}
if(str[i]<str[j])
{
b[i]=a[j]+1;
if(b[i]>max1)
max1=b[i];
}
}
b[i]=max1;
a[i]=max;
}
max2=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max2)
max2=a[i];
if(b[i]>max2)
max2=b[i];
}
cout<<max2<<endl;
}
}
验证
Missile:双状态DP的更多相关文章
- [Swust OJ 1084]--Mzx0821月赛系列之情书(双线程dp)
题目链接:http://acm.swust.edu.cn/problem/1084/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 洛谷P2770 双路DP // 网络流
https://www.luogu.org/problemnew/show/P2770 第一眼看过去,觉得这不是一个经典的双路DP模型吗,将一条过去一条回来互不相交的路径看作是起点出发了两条路径一起走 ...
- hdu 4614 pieces 状态DP
题意:给你一个长度小于等于16的字符串,每次可以删除一个回文传,问你最少删除干净的字数. 状态+dp dp[i] = min(dp[i],dp[j]+dp[j^i]);(j是i的字串): 连接:htt ...
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...
- POJ 3254 压缩状态DP
题意:一个矩形网格,可以填0或1, 但有些位置什么数都不能填,要求相邻两个不同时为1,有多少种填法.矩形大小最大 12*12. 压缩状态DP大多有一个可行的state的范围,先求出这个state范围, ...
- Codevs 1427 特种部队(双路DP)
1427 特种部队 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一 ...
- nyist 61 传纸条 nyist 712 探 寻 宝 藏(双线程dp问题)
http://acm.nyist.net/JudgeOnline/problem.php?pid=61 http://acm.nyist.net/JudgeOnline/problem.php?pid ...
- 传纸条(一)(双线程dp)
传纸条(一) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...
随机推荐
- @(报错)could not find the main class, Program will exit(已解决)
原文 @(报错)could not find the main class, Program will exit(已解决) (很抱歉,如果你希望能更加清楚地看清图片或是图上的文字的话,你可以 ...
- mysql 结合keepalived测试
vip:192.168.32.66 192.168.32.6 主库: mysql> show variables like '%read_only%'; +------------------+ ...
- Agg vs. Cairo 二维绘图引擎之比较和选择 .
Agg vs. Cairo 二维绘图引擎之比较和选择 cheungmine 当今时代对于作为二维图形软件开发者, 是幸运的.因为除了Windows GDI/GDI+之外,我们还有很多其他的选择.而且这 ...
- 【Apache ZooKeeper】理解ZooKeeper中的ZNodes
理解ZooKeeper中的ZNodes 翻译自:http://zookeeper.apache.org/doc/r3.1.2/zookeeperProgrammers.html ZooKeeper中的 ...
- Android解析Excel文档完整示例
MainActivity如下: package cc.testexcel; import java.io.File; import jxl.Cell; import jxl.CellType; imp ...
- servlet 与缓存(4)
1.设置缓存的两种情况: 第一种:对于不常常变化的数据,在servlet中能够为其设置合理的缓存时间值,以避免浏览器频繁向server发送请求,提升server的性能. 另外一种:假设要实现一种高级功 ...
- QString与中文,QString与std::wstring的相互转换(使用fromStdWString和u8关键字)
Qt版本:5.5.1 Qt的QString功能丰富,对非英语语言的支持也不是问题,但支持得不够直接.例如,像 ? 1 QString str("死亡使者赛维"); 这样直接用带中文 ...
- 配置Eclipse使用外部Maven
当集成Maven时,Eclipse会安装上一个内嵌的Maven, 这个内嵌的Maven通常会比较新,但不一定很稳定,而且往往也会和命令行使用的Maven不是同一个版本.这里又会出现两个潜在的问题:首先 ...
- java 一个函数EnumMap返回多个值
java 一个函数如何返回多个值 在开发过程中,经常会有这种情况,就是一个函数需要返回多个值,这是一个问题!! 网上这个问题的解决方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值 ...
- 把VS2010的智能代码提示和注解从英文变成中文
最近安装了个高级点的VS2010,起初还没留意.今天无意发现提示信息只能提示英文.....头大oooo. 我以为是个别现象,于是GG了下,发现有很多盆友都有这种. 记录下来了,以后省事儿: 访问MS的 ...