POJ-1887 Testing the CATCHER(dp,最长下降子序列)
Testing the CATCHER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16515 Accepted: 6082 Description
A military contractor for the Department of Defense has just completed a series of preliminary tests for a new defensive missile called the CATCHER which is capable of intercepting multiple incoming offensive missiles. The CATCHER is supposed to be a remarkable defensive missile. It can move forward, laterally, and downward at very fast speeds, and it can intercept an offensive missile without being damaged. But it does have one major flaw. Although it can be fired to reach any initial elevation, it has no power to move higher than the last missile that it has intercepted.
The tests which the contractor completed were computer simulations of battlefield and hostile attack conditions. Since they were only preliminary, the simulations tested only the CATCHER’s vertical movement capability. In each simulation, the CATCHER was fired at a sequence of offensive missiles which were incoming at fixed time intervals. The only information available to the CATCHER for each incoming missile was its height at the point it could be intercepted and where it appeared in the sequence of missiles. Each incoming missile for a test run is represented in the sequence only once.
The result of each test is reported as the sequence of incoming missiles and the total number of those missiles that are intercepted by the CATCHER in that test.
The General Accounting Office wants to be sure that the simulation test results submitted by the military contractor are attainable, given the constraints of the CATCHER. You must write a program that takes input data representing the pattern of incoming missiles for several different tests and outputs the maximum numbers of missiles that the CATCHER can intercept for those tests. For any incoming missile in a test, the CATCHER is able to intercept it if and only if it satisfies one of these two conditions:
The incoming missile is the first missile to be intercepted in this test. -or- The missile was fired after the last missile that was intercepted and it is not higher than the last missile which was intercepted. Input
The input data for any test consists of a sequence of one or more non-negative integers, all of which are less than or equal to 32,767, representing the heights of the incoming missiles (the test pattern). The last number in each sequence is -1, which signifies the end of data for that particular test and is not considered to represent a missile height. The end of data for the entire input is the number -1 as the first value in a test; it is not considered to be a separate test. Output
Output for each test consists of a test number (Test #1, Test #2, etc.) and the maximum number of incoming missiles that the CATCHER could possibly intercept for the test. That maximum number appears after an identifying message. There must be at least one blank line between output for successive data sets.
Note: The number of missiles for any given test is not limited. If your solution is based on an inefficient algorithm, it may not execute in the allotted time. Sample Input
389 207 155 300 299 170 158 65 -1 23 34 21 -1 -1 Sample Output
Test #1: maximum possible interceptions: 6
Test #2: maximum possible interceptions: 2
裸的最长下降子序列问题
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int dp[5000];
int a[5000];
int main()
{
int tot=1;
int x;
int max;
int cas=0;
while(scanf("%d",&x)!=EOF)
{
if(x==-1)
break;
cas++;
if(cas!=1)
printf("n");
a[0]=x;
scanf("%d",&x);
tot=1;
while(x!=-1)
{
a[tot++]=x;
scanf("%d",&x);
}
memset(dp,0,sizeof(dp));
a[tot]=-1;
for(int i=0;i<=tot;i++)
{
max=0;
for(int j=i-1;j>=0;j--)
{
if(a[j]>a[i])
{
if(max<dp[j])
max=dp[j];
}
}
dp[i]=max+1;
}
printf("Test #%d:n",cas);
printf(" maximum possible interceptions: %dn",dp[tot]-1);
}
}
- c++学习笔记之封装篇(上)
- c++学习笔记之继承篇
- c++学习笔记之继承篇
- silverlight:对象拖动的优雅解决方案
- Silverlgiht:快速去除/恢复对象的颜色
- deepin系统下如何设置wifi热点(亲测有效)
- Silverlight单元测试
- Silverlight:页面/控件继承的二种写法
- java学习:Hibernate入门
- Silverlight Telerik控件学习:GridView双向绑定
- XmlWriter/XmlReader示例代码
- Silverlight Telerik控件学习:RadComboBox之自动完成(AutoComplete)
- Silverlight Telerik控件学习:数据录入、数据验证
- AI与自动驾驶
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- CentOS 7下部署php7.1和开启MySQL扩展的方法教程
- Ubuntu系统下用Crontab命令定时执行PHP文件详解
- CentOS7 LNMP+phpmyadmin环境搭建 第三篇phpmyadmin安装
- Linux有限状态机FSM的理解与实现
- Linux下浅谈crond与crontab的命令用法
- centos 7系统下安装Jenkins的步骤详解
- linux tomcat配置https的方法
- Linux 中firewall的使用方法总结
- CentOS 7 安装vsftpd 服务器的具体操作步骤
- 详细介绍通过配置Apache实现404页面替换
- bug分支和feature分支_动力节点Java学院整理
- Linux下Python脚本自启动与定时任务详解
- Linux服务器tomact 8.0启动慢的完美解决方法
- vim学习高级技巧之序列的生成方法详解
- 【LoRa社区网关点亮活动】基于腾讯云IoT Explorer搭建开放的LoRaWAN网络