hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】
主题链接: pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025
题意:本求最长公共子序列。但数据太多。
转化为求最长不下降子序列。太NB了。复杂度n*log(n).
解法:dp+二分
代码:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
#include <set>
#include <map>
#include <math.h>
using namespace std;
const int MAXN = 500010;
int n, pos;
int a[MAXN];
int dp[MAXN];
int h, k;
int search(int num,int low,int high)
{
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (num >= dp[mid]) low = mid + 1;
else high = mid - 1;
}
return low;
}
int main()
{
int cases = 1;
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &h, &k);
a[h] = k;
}
memset(dp, 0, sizeof(dp));
dp[0] = -1; dp[1] = a[1];
int len = 1;
// n*log(n) 求解
for (int i = 2; i <= n; i++)
{
if (a[i] >= dp[len])
{
len = len + 1;
dp[len] = a[i];
}
else
{
pos = search(a[i],1,len);
dp[pos] = a[i];
}
}
printf("Case %d:\n",cases++);
if (len == 1)
printf("My king, at most %d road can be built.\n\n",len);
else
printf("My king, at most %d roads can be built.\n\n",len);
}
return 0;
}
版权声明:转载请注明出处。
hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】的更多相关文章
- 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 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
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 + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom[动态规划/nlogn求最长非递减子序列]
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+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
- hdu 1025 Constructing Roads In JGShining's Kingdom
本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...
- 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom
最长上升子序列o(nlongn)写法 dp[]=a[]; ; ;i<=n;i++){ if(a[i]>dp[len]) dp[++len]=a[i]; ,dp++len,a[i])=a[i ...
随机推荐
- Android Tombstone/Crash的log分析和定位
有一句话叫做常在河边走,哪有不湿鞋.我们这些研究和开发Android的project师正应了这句话,相必大家在调试的时候常常会遇到这么个东西吧 *** *** *** *** *** *** *** ...
- 1008: University
&method=showdetail&id=1008" style="background-color:rgb(255,0,0)">台州ACM:10 ...
- Spring Assert主张 (参议院检测工具的方法-主张)
Web 收到申请表格提交的数据后都需要对其进行合法性检查,假设表单数据是不合法的,该请求将被拒绝.分类似的,当我们写的类方法,该方法还经常需要组合成参 法国检查.假设参议院不符合要求,方法通过抛出异常 ...
- Source Insight 3.X 插件支持utf8,完美解决中国乱码,连接到美丽的轮廓
上次SI多标签插件之后,由于公司内部编码改为utf8编码,因此特意做了这个Source Insight 3.X utf8插件. 下载地址:http://pan.baidu.com/s/1mgyZous ...
- 正确使用Git Flow
Git 在团队中的最佳实践--如何正确使用Git Flow 我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确 ...
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- memset功能的具体说明
1.void *memset(void *s,int c,size_t n)总的效果:内存空间开辟了 s 第一 n 字节的值设置为一个值 c. 2.样本#include void main(){cha ...
- 白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连续7-电话问卷调查
[例7-3 文本编辑框创建一个简单的调查问卷] 01 <!DOCTYPEhtml> 02 <html> 03 <head> 04 ...
- Cookie概念
ASP.NET Cookie概念.CURD操作.原理.实际运用 会话就WEB开发来说,一个会话就是你通过浏览器与服务器之间的一次通话,只不过这种通话是以用浏览器浏览的方式来实现的. 就会话的应用来 ...
- 开销是有益的:AppCan 至HTML5移动创新和创业精神和健康
2014年移动创业更趋向理性,消费级App市场接近饱和,BAT等巨头的竞争更加激烈,市场版图及格局基本定型.而企业级移动应用却迎来爆发增长,替代进入红海的消费级App市场,企业级定制APP开发成为 ...