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. InnoDB锁冲突案例演示

      Preface       As we know,InnoDB is index organized table.InnoDB engine supports row-level lock bas ...

  2. Qt-QPalette-调色板学习

    已经很久没有更新博客了,一是因为换了公司,完全是断网开发了,没有时间来写博客,最主要的就是温水煮青蛙,自己在舒适的环境中越来越懒了,最近打算强制自己更新一波.不知道能坚持多久.由于目前没有具体的Qt项 ...

  3. egrep及扩展正则

    模式:pattern 正则: grep:基本正则,查找速度慢 Extended grep:扩展正则 fgrep:fast grep,不支持正则,直接查找字符串,执行速度快 基本正则: . :任意单个字 ...

  4. Charles使用及mock数据

    1.下载charles 3.9.2[破解版地址:https://download.csdn.net/my] 下方有一种方法可破解4.2以前的版本 // Charles Proxy License // ...

  5. javaee开发工具及环境配置过程

    在配置javaee开发环境的过程中遇到过很多问题,在此系统的整理一下我之前的配置过程 注:配置过程学习自<JSP&Servlet学习笔记(第二版)>详细过程可以阅读此书.在文章的最 ...

  6. 学习笔记,99乘法表,嵌套while循环

    line = 0 #定义外循环初变量 while line < 9: #外循环判断语句 line += 1 #改变外循环初变量,避免陷入死循环 row = 0 #定义内循环初变量 while r ...

  7. Microsoft Edge 浏览器开始支持webkit私有样式

    微软表示新版的浏览器Edge(spartan)不会再增加新的私有属性,同时移除了部分-ms-属性,但很多标准在没有支持到之前,会使用webkit的api.Edge开发工程师Jacob Rossi列出了 ...

  8. 剑指offer-二叉搜索树的后序遍历序列23

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. class Solution: def Verif ...

  9. nordic mesh 任务调度实现

    nordic mesh 任务调度实现 nordic mesh的任务调度室基于定时器实现的,有两个链表结构维护任务. 需要注意的是,任务调度的部分接口只能在"bearer event" ...

  10. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...