HDU 1025 DP + 二分
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1025
求最长递增子序列,O(n^2)的复杂度超时,需要优化为O(n*logn)
f[i]存储长度为i的最小末尾
#include<stdio.h>
int poor[500010], f[500010];
int main()
{
int n, k = 1;
while (scanf("%d", &n) != EOF)
{
int m, m1;
for (int i = 0; i<n; i++)
{
scanf("%d%d", &m, &m1);
poor[m] = m1;
}
f[1] = poor[1];
int length = 1;
for (i = 2; i<=n; i++)
{
int low = 1;
int high = length;
while (low<=high)//二分
{
int mid = (low + high)/2;
if (f[mid] < poor[i]) low = mid + 1;
else high = mid - 1;
}
f[low] = poor[i];
if (low > length) length ++;
} printf("Case %d:\n", k ++);
if (length == 1)
printf("My king, at most 1 road can be built.\n\n");
else
printf("My king, at most %d roads can be built.\n\n",length);
}
return 0;
}
HDU 1025 DP + 二分的更多相关文章
- HDU 3433 (DP + 二分) A Task Process
题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...
- HDU 1025 (LIS+二分) Constructing Roads In JGShining's Kingdom
这是最大上升子序列的变形,可并没有LIS那么简单. 需要用到二分查找来优化. 看了别人的代码,给人一种虽不明但觉厉的赶脚 直接复制粘贴了,嘿嘿 原文链接: http://blog.csdn.net/i ...
- hdu 1025 dp 最长上升子序列
//Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】
主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- mybatis+postgresql平台
mybatis+postgresql平台 最近有个项目的数据库使用postgresql,使用原生态的mybatis操作数据,原生态的没什么不好,只不过国内有个tk.mybatis的工具帮 ...
- linux历史发展
1.什么是开源? 开源就是软件和源代码都是公开的. 可以修改(完善作者的代码)和创建自己的软件. 2.免费软件不同于自由软件,虽然它是免费的,但他不公布源代码,共享软件与免费软件有点类似,其初起是不收 ...
- Spring Cache使用详解
Spring Cache Spring Cache使用方法与Spring对事务管理的配置相似.Spring Cache的核心就是对某个方法进行缓存,其实质就是缓存该方法的返回结果,并把方法参数和结果用 ...
- 基于Visual C++2013拆解世界五百强面试题--题17-程序结果分析1
分析程序结果,分析过程我们就写在程序注释里面. 写出下列代码的输出内容 #include <stdio.h> int inc(int a) { return (++a); } int mu ...
- 在cnblog中使用syntax方法
<pre name="code" class="brush: cpp;"> 代码 </pre> #include<cstdio&g ...
- Java输入输出流(1)
1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java全部的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...
- js调用.net后台
<head runat=server> <script type="text/javascript"> $(function () { var js_tim ...
- 在 Windows系统中编译node.js 源代码
Node.js 在 Windows 下只能通过 Microsoft Visual Studio 编译,因此你需要首先安装 Visual Studio 或者免费的 Visual Studio Expre ...
- 两种MD5最后的值不一样,因为两种做法不一样
//MD5加密 private static string Md5Hash(string input) { MD5CryptoServiceProvider m ...
- codeforces 569A Music
codeforces 569A Music 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88890#pro ...