HDOJ(HDU).1025 Constructing Roads In JGShining’s Kingdom (DP)

点我挑战题目

题目分析

题目大意就是给出两两配对的poor city和rich city,求解最多能修几条不相交的路。此题可以转化为LIS问题。转化过程如下:

数据中有2列,为方便表述,暂且叫做第一列和第二列。

1.若第一列是是递增的(给出的2个样例都是递增的),那么要想尽可能多的做连线,则那么就需要找出第二列中最长的递增子序列,若出现非递增的序列,那么连线后一定会相交

2.若第一列不是递增的,排序后按照1分析即可。

综上所述,题目便转换成LIS问题。

LIS有2种写法,一种是o(n²)的写法,一种是o(nlogn)的写法。题目中给出n<=500,500.采用o(n²)必定超时,最佳策略是o(nlogn)。

推荐一篇介绍这种写法的博文 最长上升子序列nlogn算法。通俗易懂,在此就不赘述如何设计此算法了。

代码总览

/*
Title:HDOJ.1025
Author:pengwill
Date:2017-2-15
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define nmax 500005
using namespace std;
int a[nmax],dp[nmax];
int BS(int dt[],int t[],int left, int right,int i)
{
int mid;
while(left<right){
mid = (left+right)/2;
if(dt[mid]>=t[i]) right = mid;
else left = mid+1;
}
return left;
}
int main()
{
// freopen("in.txt","r",stdin);
int n,t = 0;
while(scanf("%d",&n) != EOF){
printf("Case %d:\n",++t);
int x,y;
for(int i = 1;i<=n; ++i){scanf("%d%d",&x,&y);a[x] = y;}
dp[1] = a[1];
int len=1;
// for(int i = 1; i<=n; ++i){
// int m = 0;
// for(int j = 1; j<i ;++j ){
// if(a[i]>a[j] && dpa[j]>m)
// m = dpa[j];
// }
// dpa[i] = m+1;
// }
for(int i = 2; i<=n;++i){
if(a[i]>dp[len]) dp[++len] = a[i];
else{
int pos = BS(dp,a,1,len,i);
dp[pos] = a[i];
}
}
if(len == 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",len); }
return 0;
}

HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)

    点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...

  6. HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...

  7. hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】

    主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...

  8. hdu 1025 Constructing Roads In JGShining's Kingdom

    本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...

  9. 最长上升子序列 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 ...

随机推荐

  1. linux部署MantisBT(二)部署php

    二.部署php 1.下载php安装包 http://php.net/downloads.php 2.安装php yum install libxml2 yum install libxml2-deve ...

  2. ortp打印日志

    //向字符串中打印数据 static char* ms_strdup_vprintf(const char *fmt, va_list ap) { ; char *p,*np; #ifndef WIN ...

  3. php导出excel表格的使用

    网站后台有很多列表数据,常常都会有导出excel表格的需求,和大家分享一个实用的导出excel表格方法: 不多说,上代码: /** * @param array $data 要导出的数据 * @par ...

  4. TW实习日记:第20-21天

    为什么上周五没写呢,因为上周五一直在熟悉业务流程...根本不会写一些复杂的业务代码,因为没有业务流程图!!!在学校的上需求分析和UML建模课的时候,还有软件工程课的时候,想着这都什么鬼啊,听来干嘛,写 ...

  5. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  6. kafka stream 低级别的Processor API动态生成拓扑图

    public class KafkaSream { public static void main(String[] args) { Map<String, Object> props = ...

  7. logisitic回归

    线性回归目的是找到一条直线(或者超平面)尽可能地接近所有的训练数据点,而对数几率回归的目的是找到一条直线(或者超平面)尽可能地分开两种不同类别的数据点. 对数几率回归感觉更像是一个分类问题.https ...

  8. [Install] TeamViewer

    安装TeamViwer 1. $ sudo apt-get -f install 2. 使用gdebi安装TeamViwer. 所以先安装gdebi package. $ sudo apt-get i ...

  9. Python3 Tkinter-Text

    1.创建 from tkinter import * root=Tk() t=Text(root) t.pack() root.mainloop() 2.添加文本 from tkinter impor ...

  10. Docker学习笔记总结

    Docker学习笔记 https://yeasy.gitbooks.io/docker_practice/content/   一 环境搭建 Ubuntu安装 .添加软件源的GPG密钥 curl -f ...