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 ...
随机推荐
- Eclipse4.4.2手动安装Veloeclipse-2.0.8
引言: 新安装了Eclipse最新版本 4.4.2 Luna(月神),由于项目中使用到了模板引擎Velocity,所以想安装一个Velocity插件, 在网上找了一下,看到Google的vel ...
- Atitit.软件的仪表板(8)--os子系统--监控资源使用情况
Atitit.软件的仪表板(8)--os系统--资源占用监測 CPU使用 内存使用 磁盘队列 任务管理器 网络速度 插件列表( 资源管理器插件,浏览器插件,360optim) 启动项管理 (350) ...
- 【UFLDL】多层神经网络
请参见原始英文教程地址:http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks 本文是在学习该教程时记得笔记,供參 ...
- Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)
Qt/C++开发人员眼中的Obj-C 对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...
- adapter pattern
对象适配器 9.7 适配器模式总结 适配器模式将现有接口转化为客户类所期望的接口,实现了对现有类的复用,它是一种使用频率非常高的设计模式,在软件开发中得以广泛应用,在Spring等开源框架.驱动程序设 ...
- 谷歌技术面试要点(Google面试)(14年5月20日交大专场)
技术面试的主题 1.简要自我介绍: 姓名.学校.专业 做过的项目与实习 个人主要成就 2.技术评估: 构建与开发算法 编程 计算机基础知识 数据结构 现实世界问题解决能力 设计问题(主要针对博士生) ...
- python udp编程实例
与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...
- Python 显示LinkedIn用户作业
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-18 @author: guaguastd @name: j ...
- [LeetCode238]Product of Array Except Self
题目: Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- 让Emeditor支持markdown编辑博客
让Emeditor支持markdown编辑博客 1. 关于高亮显示 2.生成HTML文件并预览 用惯了Emeditor,最近又开始学习用markdown写博客,怎么让Emeditor支持markdow ...