HDU - 1257 最少拦截系统(LIS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257

Problem Description
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.

Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)

Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.

Sample Input
8 389 207 155 300 299 170 158 65

Sample Output
2

下面的各种方法,总的来说核心是相同的,即不断更新每个拦截系统的最小值,使后序若有更大值则生成新的拦截系统。

代码如下:

①贪心法

#include<cstdio>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 100000;
int n, a[maxn];
struct node {
	int v, xb;
}vis[maxn];
bool operator >(const node& A, const node& B) {
	return B.v < A.v;
}
int main(void) {
	while (~scanf("%d", &n)) {
		priority_queue<node, vector<node>, greater<node> >pq;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			vis[i].v = inf;
			vis[i].xb = i;
			pq.push(vis[i]);
		}
		for (int i = 1; i <= n; i++) {
			vector<int>s;
			while (1) {
				s.push_back(pq.top().xb); pq.pop();
				if (vis[s.back()].v >= a[i]) {
					vis[s.back()].v = a[i];
					for (int j = 0; j < s.size(); j++)
						pq.push(vis[s[j]]);
					break;
				}
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; i++)
			if (vis[i].v != inf)ans++;
		printf("%d\n", ans);
	}
	return 0;
}

②DP法(LIS)

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 10000;
int n, h[maxn], dp[maxn];
int main(void) {//每个拦截系统选出一个数构成递增序列,求出该递增序列的最长长度
	while (~scanf("%d", &n)) {
		for (int i = 1; i <= n; i++)
			scanf("%d", &h[i]);
		memset(dp, 0, sizeof(dp));
		dp[1] = 1;
		int ans = 0;
		for (int i = 2; i <= n; i++) {
			int num = 0;
			for (int j = 1; j < i; j++) 
				if (h[j]<h[i] && dp[j]>num)
					num = dp[j];//将第i个元素视为最新拦截系统的最高高度
			dp[i] = num + 1;
			if (dp[i] > ans)ans = dp[i];
		}
		printf("%d\n", ans);
	}
	return 0;
}

③LCS法
思想:设另一个数组,元素为h[]从大到小的排序,求最长公共序列长度

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1000;
int n, h[maxn], h2[maxn], dp[maxn][maxn];
int main(void) {
	while (~scanf("%d", &n)) {
		for (int i = 1; i <= n; i++) {
			scanf("%d", &h[i]);
			h2[i] = h[i];
		}
		sort(h2 + 1, h2 + 1 + n);
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				if (h[i] == h2[j])
					dp[i][j] = dp[i - 1][j - 1] + 1;
				else
					dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
			}
		printf("%d\n", dp[n][n]);
	}
	return 0;
}

④技巧法
思想:不断更新每个拦截系统的最小值,使之代表一个拦截系统

#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 10000;
int n, h[maxn];
int main(void) {
	while (~scanf("%d", &n)) {
		for (int i = 1; i <= n; i++)
			scanf("%d", &h[i]);
		vector<int>d;
		d.push_back(h[1]);
		for (int i = 2; i <= n; i++) {
			if (h[i] > d.back())
				d.push_back(h[i]);
			else for (int j = 0; j < d.size(); j++)
				if (h[i] <= d[j]) {
					d[j] = h[i]; break;
				}
		}
		printf("%d\n", d.size());
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值