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行 ...
随机推荐
- docker学习笔记1:docke环境的查看
本文的操作是在ubuntu操作系统下的. 一.环境检查 当登录一个安装了docker的机器后,首先我们要检查下docker环境如何. 1.命令:docker -v 上述命令返回安装的docker的版本 ...
- 基于visual Studio2013解决算法导论之011快排改良
题目 快排改良 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...
- 使用回溯法求所有从n个元素中取m个元素的组合
不多说了,直接上代码,代码中有注释,应该不难看懂. #include <stdlib.h> #include <stdio.h> typedef char ELE_TYPE; ...
- 技术不牛如何才拿到国内IT巨头的Offer(转)
不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic...在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技 ...
- Ultra Office Control 2.0
http://www.ultrashareware.com/Ultra-Office-Control.htm
- WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘
原文:WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘 通过<再谈IIS与ASP.NET管道>的介绍,相信读者已经对IIS和ASP.NET的请求处理管道有了一个大致 ...
- wireshark删除filters记录
- u盘安装ubuntu10.04 server.txt
10.04 先将 ubuntu server 的 iso 放到优盘上,然后在提示无法找到光驱时,按 alt+f2 打开一个新的 console 窗口,将 iso mount 上,具体操作如下: ls ...
- js实现密码强度验证
<html> <head> <meta http-equiv="content-type" content="text/html" ...
- ubuntu 10.04安装qtcreator并汉化
最近最的项目中需要做出来一个带有界面的demo,所以想到了用qt做个简单的demo! 于是在ubuntu上安装了qt,很简单apt-get apt-get install qtcreator 大概几百 ...