Constructing Roads In JGShining's Kingdom
本题目是考察 最长递增子序列的 有n^2 n(logn) n^2 会超时的
下面两个方法的代码 思路 可以百度LIS LCS
dp里面存子序列
n(logn) 代码
<span style="font-size:18px;">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#define N 500000
using namespace std; int road[N],dp[N],n,len; int two_part(int *a,int L,int R, int aim) //二分法找更新的位置
{
int zz;
if(len==1&&dp[1]==0) return 1;
while(L<=R)
{
int mid=(L+R)/2;
if(aim>a[mid]&&aim<a[mid+1]) return mid+1;
else if(aim>a[mid]) L=mid+1;
else if(aim<a[mid]) R=mid-1;
}
if(aim<=dp[1]) return 1; //目标数字比第一个数小则更新dp[1]
return ++len; //找不到则在末尾更新
}
int ans()
{
int i;
for(i=1; i<=n; i++) //把每个数字在dp数组中更新
{
int wz=two_part(dp,1,len,road[i]);
dp[wz]=road[i];
// print();
}
return len;
} int main()
{
int t=1;
while(scanf("%d",&n)!=EOF)
{
int i;
len=1;
memset(road,0,sizeof(road));
memset(dp,0,sizeof(dp));
for(i=1; i<=n; i++)
{
int ra,rb;
scanf("%d%d",&ra,&rb);
road[ra]=rb;
}
int answer=ans();
// printf("%d\n",ans());
printf("Case %d:\n",t++);
if(answer==1) printf("My king, at most %d road can be built.\n\n",answer);
else printf("My king, at most %d roads can be built.\n\n",answer);
}
return 0;
}</span>
<span style="font-size:18px;">
</span>
n^2代码
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#define N 500000
using namespace std; int road[N],dp[N],n; //int two_part(int *a,int L,int R, int aim)
//{
// while(L<=R)
// {
// int mid=(L+R)/2;
// if(aim==a[mid]) return mid;
// else if(aim>mid)
// {
// L=mid+1;
// }
// else
// {
// R=mid-1;
// }
// }
// return -1;
//} int ans()
{
int sum=0;
int i,j;
for(i=0;i<n;i++)
{
j=0;
while(1)
{
if(road[i]<dp[j]||!dp[j])
{
dp[j]=road[i];
break;
}
j++;
}
}
for(i=0;i<n;i++)
if(dp[i]) sum++;
return sum;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int i;
memset(road,0,sizeof(road));
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
int ra,rb;
scanf("%d%d",&ra,&rb);
road[ra]=rb;
}
int answer=ans();
// printf("%d\n",ans());
if(answer==1) printf("My king, at most %d road can be built.\n\n",answer);
else printf("My king, at most %d roads can be built.\n\n",answer);
}
return 0;
}
Constructing Roads In JGShining's Kingdom的更多相关文章
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- [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(二维LIS)
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/LIS+二分)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- Constructing Roads In JGShining's Kingdom(HDU 1025 LIS 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 + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 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(二分查找)
题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 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(LIS+二分优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Problem Des ...
随机推荐
- Go -- LFU类(缓存淘汰算法)(转)
1. LFU类 1.1. LFU 1.1.1. 原理 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频 ...
- 椭圆人头跟踪bmp图像序列 BMP Image Sequences for Elliptical Head Tracking
BMP Image Sequences for Elliptical Head Tracking The BMP image sequences used in the head tracking d ...
- 手把手教你如何利用Meterpreter渗透Windows系统
在这篇文章中,我们将跟大家介绍如何使用Meterpreter来收集目标Windows系统中的信息,获取用户凭证,创建我们自己的账号,启用远程桌面,进行屏幕截图,以及获取用户键盘记录等等. 相关Payl ...
- HttpClient获取Cookie的两种方式
转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...
- Win7安装软件,界面上中文显示乱码的解决方案
“Control panel”->"Clock,Language and Region"->"Region and Language"->第四 ...
- C/C++中怎样获取日期和时间
C/C++中怎样获取日期和时间摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时.时间的获取.时间的计算和显示格式等方面进行了阐述.本文还通过大量的实 ...
- mysql生产环境____主从同步修复案例
一. 硬件环境 Master: Dell R720 Intel(R)Xeon(R) CPU E5-2640 v2 @ 2.00GHz MEM 64G.disk 4*2.5 SAS 网络4* 千兆 ...
- 轻松搞定RabbitMQ(三)——消息应答与消息持久化
转自 http://blog.csdn.net/xiaoxian8023/article/details/48710653 这个官网的第二个例子中的消息应答和消息持久化部分.我把它摘出来作为单独的一块 ...
- Java泛型 类型变量的限定
有时候,类和方法须要对类型变量加以约束.比方你有一个方法,你仅仅希望它接收某个特定类型及其子类型作为參数. 以下就举一个方法限定接收參数的类型的样例来说明怎样限定类型变量. 首先有几个简单的辅助类: ...
- FFmpeg解码详细流程
FFmpeg在解码一个视频的时候的函数调用流程.为了保证结构清晰,其中仅列出了最关键的函数,剔除了其它不是特别重要的函数. 下面解释一下图中关键标记的含义. 函数背景色 函数在图中以方框的形式表现出来 ...