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行 ...
随机推荐
- 教你在mac上配置adb环境变量
1.打开终端,一次输入如下命令 cd ~ touch .bash_profile open -e .bash_profile 2.这时候会在TextEdit中打开一个空白文档,输入下面的语句 a. 输 ...
- 清华集训2014 day2 task3 矩阵变换
题目 算法 稳定婚姻系统(其实就是贪心) 一个方案不合法,当且仅当下面这种情况: 设第\(i\)行选了数字\(x\),如果第\(j\)行有一个\(x\)在第\(i\)行的\(x\)后面,并且第\(j\ ...
- 云计算被视为继大型计算机、个人计算机、互联网之后的第4次IT产业革命,顺应了当前各行业整合计算资源和服务能力的要求(转)
云计算被视为继大型计算机.个人计算机.互联网之后的第4次IT产业革命,顺应了当前各行业整合计算资源和服务能力的要求,成为引领当今世界信息技术变革的主力军.越来越多的金融企业认识到只有与云计算结合,才能 ...
- python gzip 压缩文件
压缩数据创建gzip文件 先看一个略麻烦的做法 ? 1 2 3 4 5 6 import StringIO,gzip content = 'Life is short.I use python' zb ...
- 基于visual Studio2013解决C语言竞赛题之1047百马问题
题目 解决代码及点评 /* 47.马百瓦问题.有100匹马,100块瓦,大马驮3块, 小马驮2块,两个马驹驮1块.问大马.小马.马驹各多少? 要求:① 不许用for循环; ② 循环次数 ...
- Java_1Lesson
cmd使用 进入硬盘分区:D: E: F: 查看目录 dir 进入文件夹 “cd 文件名” .使用javac编译器编译运行. Javac 文件名 运行java程序 Java 文件名 第一个程序 pub ...
- 跟我一起写 Makefile(一)
跟我一起写 Makefile 陈皓 概述—— 什么是makefile?也许非常多Winodws的程序猿都不知道这个东西,由于那些Windows的IDE都为你做了这个工作,但我认为要作一个好的和pro ...
- 史上最全的java随机数/字符串生成算法(转)
代码如下: package com.zuidaima.core.util; import java.util.Random; public class RandomUtil { public stat ...
- hdu 4691 Front compression (后缀数组)
hdu 4691 Front compression 题意:很简单的,就是给一个字符串,然后给出n个区间,输出两个ans,一个是所有区间的长度和,另一个是区间i跟区间i-1的最长公共前缀的长度的数值的 ...
- [置顶] C++之TinyXML的使用介绍
一.引子: 最近在做GBT28181国标平台对接的工作,涉及到一些进程间消息通讯,消息体有xml格式,之前测试的时候都是拿他们当做字符串去解析,现在正儿八经地开发的时候,就想到了用xml库去解析,由于 ...