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 ...
随机推荐
- 在intellij中使用checkStyle进行代码规范
1 编写代码检测规则可以参考阿里和google的规则和checkstyle的官网文档.checkstyle官网地址http://checkstyle.sourceforge.net/ 假设自己的sty ...
- 多线程-Executor,Executors,ExecutorService,ScheduledExecutorService,AbstractExecutorService
引用 系统启动一个新线程的成本是比较高的,因为涉及与操作系统交互.使用线程池可以很好地提高性能,尤其是当程序中需要创建大量生存期很短的线程时,更应该考虑使用线程池.线程池在系统启动时即创建大量空闲的线 ...
- 安全DNS
国内首家云安全DNS:(114DNS)114.114.114.114114.114.115.115 将 DNS 地址设为114.114.114.119 和 114.114.115.119 ,拦截钓鱼病 ...
- hdu 1018 Big Number 数学结论
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- SqlServer 数据分页
select * from ( select ROW_NUMBER() over (partition by name order by name) rowid,* from table ) t
- Ubuntu 12.04下PostgreSQL-9.1安装与配置详解(在线安装) [转]
说明: 我是用root用户在终端登陆的,如果是非root用户,那在命令前需要加上"sudo",你懂的... 第一步:在Ubuntu下安装Postgresql ...
- 查看网络连接数目(解决TIME_WAIT过多造成的问题_转)
转自:解决TIME_WAIT过多造成的问题 (eroswang的csdn) #netstat -n | awk '/^tcp/ {++S[$NF]} END { for(a in S) print ...
- httpClient使用中报错org.apache.commons.httpclient.HttpMethodBase - Going to buffer response body of large or unknown size.
在使用HttpClient发送请求,使用httpMethod.getResponseBodyAsString();时当返回值过大时会报错: org.apache.commons.httpclient. ...
- hdu6069 Counting Divisors 晒区间素数
/** 题目:hdu6069 Counting Divisors 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意:求[l,r]内所有数的k次方 ...
- Reducing the Dimensionality of Data with Neural Networks
****************内容加密中********************