hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接:
最少拦截系统
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12863 Accepted Submission(s): 5100
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.
8 389 207 155 300 299 170 158 65
2
算法:贪心 || Dp 【本质一样】
思路:
/*
最长上升子序列(一维形式DP)
opt[i]=max(opt[j])+1, opt[i]) (0<=j<i 且num[j]<=num[i]) {最长非下降序列}
opt[i]=max(opt[j])+1, opt[i]) (0<=j<i 且num[j]>num[i]) {最长下降序列}
该算法复杂度为O(n^2)
*/
但是这样就还不如贪心了效率。后来又去找了下 LIS 的资料:
207 155 300 299 170 158 65
int index = upper_bound(h,h+len+1,a[i])-h; //保证 h[index] 是数组 h 中第一个 >= a[i] 的
code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 1000+10;
const int INF = 30000+10; //导弹高度不会超过 30000
int a[maxn]; //存导弹的高度
int h[maxn]; // h[i] 表示第 i 个导弹系统拦截的最低高度 int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
h[i] = INF; //初始化保证每一个拦截系统都能拦截所有的导弹
} for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++) //往前找开发了的导弹系统,看是否能拦截当前导弹, 最坏的结果是每个导弹都需要一个新的导弹系统来拦截,所以遍历到 i
{
if(h[j] >= a[i]) //一旦找到符合条件的拦截系统
{
h[j] = a[i]; // 第 j 个拦截系统拦截了第 i 个导弹 , 更新它的目前可以拦截的导弹的高度
break; //第 i 个导弹已经拦截,跳出里面那层循环
}
} } int tot = 0;
for(int i = 0; i < n; i++) //计算总共用了几个导弹系统
if(h[i] != INF) //如果第 i 个导弹系统的高度不等于初始值说明它用过
tot++;
printf("%d\n", tot);
}
return 0;
}
/*****************************************************
dp[i] = max(dp[i], dp[j]+1) 【0<=j<i, a[i] > a[j]】
如果当前导弹 i 的高度 > 前面的导弹 j 的高度,
那么拦截当前导弹 i 的系统,一定是拦截 j 的后面的系统
******************************************************/
#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn = 1000+10;
const int INF = 30000+10; int a[maxn]; //存导弹的高度
int dp[maxn]; //d[i] 表示第 i 个导弹是被第 dp[i] 个拦截系统拦截的 int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
dp[i] = 1;
} for(int i = 0; i < n; i++)
{
for(int j = 0; j < i; j++)
if(a[i] > a[j])
dp[i] = max(dp[i], dp[j]+1);
} int ans = 0;
for(int i = 0; i < n; i++)
ans = max(ans, dp[i]);
printf("%d\n", ans);
}
return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn = 1000+10; int a[maxn]; //导弹高度
int h[maxn]; // h[i] 表示第 i 个系统目前拦截的高度 int find(int h[], int len, int ha) //返回 index , 数组h[] 中, 第一个h[index] >= ha
{
int left = 0;
int right = len; while(left <= right)
{
int mid = (left+right) / 2; if(ha > h[mid]) left = mid+1;
else if(ha < h[mid]) right = mid-1;
else return mid;
}
return left;
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
} h[0] = -1;
h[1] = a[0];
int len = 1; for(int i = 1; i < n; i++)
{
int index = find(h,len, a[i]);
h[index] = a[i];
//printf("test : h[%d] = %d\n", index, h[index]);
if(index > len)
len = index;
}
printf("%d\n", len);
}
return 0;
}
| D | Accepted | 236 KB | 0 ms | C++ | 814 B | 2013-08-05 11:34:41 |
#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn = 1000+10;
const int INF = 30000+10;
int a[maxn]; //导弹高度
int h[maxn]; // h[i] 表示当前第 i 个系统拦截的高度 int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
// h[i] = INF;
} h[0] = -1;
h[1] = a[0];
int len = 1; for(int i = 1; i < n; i++)
{
int index = upper_bound(h,h+len+1,a[i])-h; //保证 h[index] 是数组 h 中第一个 >= a[i] 的
h[index] = a[i];
//printf("test: h[%d] = %d\n", index, h[index]);
if(index > len)
len = index;
}
//for(int i = 0; i <= n; i++) printf("%d ", h[i]); printf("\n");
printf("%d\n", len);
}
return 0;
}
hdu 1257 最少拦截系统【贪心 || DP——LIS】的更多相关文章
- HDU 1257 最少拦截系统(dp)
Problem Description 某国为了防御敌国的导弹突击,发展出一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以到达随意的高度,可是以后每一发炮弹都不能超过前一发的 ...
- hdu 1257 最少拦截系统(贪心)
解题思路:[要充分理解题意,不可断章取义] 贪心:每个防御系统要发挥其最大性能, 举例: Input : 9 389 207 155 300 299 170 155 158 65 Output: 2 ...
- HDU 1257 最少拦截系统 最长递增子序列
HDU 1257 最少拦截系统 最长递增子序列 题意 这个题的意思是说给你\(n\)个数,让你找到他最长的并且递增的子序列\((LIS)\).这里和最长公共子序列一样\((LCS)\)一样,子序列只要 ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1257 最少拦截系统(贪心 or LIS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1257最少拦截系统[动态规划]
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1257 最 ...
- POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)
Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...
- HDU 1257 最少拦截系统 (DP || 贪心)
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- HDU 1257——最少拦截系统——————【LIS变型题】
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
随机推荐
- leetcode——Lowest Common Ancestor of a Binary Tree
题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次 ...
- Powershell 获取文件版本信息
获取文件版本信息,通过FileVersionInfo::GetVersioninfo(file) 来获取信息 function Check-DdpstoreFileVersion{ $Ddpstore ...
- yum安装Apache,Mysql,PHP
用yum安装Apache,Mysql,PHP. 用yum安装Apache,Mysql,PHP. 2.1安装Apache yum install httpd httpd-devel 安装完成后,用/e ...
- 深度CTR预估模型中的特征自动组合机制演化简史 zz
众所周知,深度学习在计算机视觉.语音识别.自然语言处理等领域最先取得突破并成为主流方法.但是,深度学习为什么是在这些领域而不是其他领域最先成功呢?我想一个原因就是图像.语音.文本数据在空间和时间上具有 ...
- 0044 spring框架的applicationContext.xml的命名空间
Spring框架中,创建bean,装配bean,事务控制等,可以用xml配置或者注解扫描的方法实现.如果用注解扫描,在xml配置中得加上 <context:component-scan base ...
- Oracle之批量生成数据
一.引言 由于测试程序,需要大量的数据 二.方法 1.pl/sql的Generate Data,在tool菜单中可以找到,但是我这里不能用,老是出现错误,应该是软件的原因,但是没找到解决办法,如下图: ...
- Extjs的完成按钮和位置
this.toolbar.add('->') ---重点是这个箭头,他是控制位置的 this.CompleteDataAction = new Ext.Action({ text : '完成', ...
- invalid comparison: java.util.ArrayList and java.lang.String——bug解决办法
今天碰到个问题,解决了很久才搞定,来记录下,希望可以帮助到大家 贴错误源码: 这是一个根据list集合的查找数据的 sql,在接收list的时候加了判断 list != ‘ ’ “”,引起了集合与St ...
- springbatch操作DB
一.需求分析 使用Spring Batch对DB进行读写操作: 从一个表中读取数据, 然后批量的插入另外一张表中. 二.代码实现 1. 代码结构图: 2. applicationContext.xml ...
- MYSQL数据库连接
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...