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 ...
随机推荐
- android showmessage
package com.example.yanlei.yl6; import android.annotation.TargetApi; import android.app.Activity; im ...
- Android应用开发-小巫CSDN博客客户端开发开篇
2014年9月8日 八月十五 祝各位中秋节快乐 小巫断断续续花了几个星期的时间开发了这么一款应用——小巫CSDN博客,属于私人定制的这样的一款应用,整个客户端的数据全部来自本人博客,是通过爬取本人博客 ...
- python matplotlib包图像配色方案
可选的配色方案: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_ ...
- C++11 并发指南系列(转)
本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...
- ffmpeg H264 编解码配置
ffmpeg H264编解码前面有文章介绍下,本文主要介绍一些参数配置. 编码: int InitEncoderCodec( int iWidth, int iHeight) { AVCodec * ...
- 数据库历险记(三) | 缓存框架的连环炮 数据库历险记(二) | Redis 和 Mecached 到底哪个好? 数据库历险记(一) | MySQL这么好,为什么还有人用Oracle? 面对海量请求,缓存设计还应该考虑哪些问题?
数据库历险记(三) | 缓存框架的连环炮 文章首发于微信公众号「陈树义」,专注于 Java 技术分享的社区.点击链接扫描二维码,与500位小伙伴一起共同进步.微信公众号二维码 http://p3n ...
- bash 的环境配置文件
http://www.cnblogs.com/ggjucheng/archive/2012/11/01/2750179.html bash 的环境配置文件 你是否会觉得奇怪,怎么我们什么动作都没有进行 ...
- Gson解析数组和list容器
Gson解析数组和list容器 使用Gson解析首先须要增加架包文件:gson-2.2.4.jar 定义一个类Student: public class Student { String name=& ...
- Windows 10遭遇百万粉丝“围攻”(挑刺)
9月30日,微软公布Win 10技术预览版,征求反馈意见. 出人意料的是.截止10月14日.在短短两周内,竟有百万粉丝下载试用(所谓"測试"),反馈了20万条改动意见.对此,微软真 ...
- Process Autocad by python
一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...