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算法。通俗易懂,在此就不赘述如何设计此算法了。

代码总览

  1. /*
  2. Title:HDOJ.1025
  3. Author:pengwill
  4. Date:2017-2-15
  5. */
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <cstdio>
  9. #include <cstring>
  10. #define nmax 500005
  11. using namespace std;
  12. int a[nmax],dp[nmax];
  13. int BS(int dt[],int t[],int left, int right,int i)
  14. {
  15. int mid;
  16. while(left<right){
  17. mid = (left+right)/2;
  18. if(dt[mid]>=t[i]) right = mid;
  19. else left = mid+1;
  20. }
  21. return left;
  22. }
  23. int main()
  24. {
  25. // freopen("in.txt","r",stdin);
  26. int n,t = 0;
  27. while(scanf("%d",&n) != EOF){
  28. printf("Case %d:\n",++t);
  29. int x,y;
  30. for(int i = 1;i<=n; ++i){scanf("%d%d",&x,&y);a[x] = y;}
  31. dp[1] = a[1];
  32. int len=1;
  33. // for(int i = 1; i<=n; ++i){
  34. // int m = 0;
  35. // for(int j = 1; j<i ;++j ){
  36. // if(a[i]>a[j] && dpa[j]>m)
  37. // m = dpa[j];
  38. // }
  39. // dpa[i] = m+1;
  40. // }
  41. for(int i = 2; i<=n;++i){
  42. if(a[i]>dp[len]) dp[++len] = a[i];
  43. else{
  44. int pos = BS(dp,a,1,len,i);
  45. dp[pos] = a[i];
  46. }
  47. }
  48. if(len == 1) printf("My king, at most 1 road can be built.\n\n");
  49. else printf("My king, at most %d roads can be built.\n\n",len);
  50. }
  51. return 0;
  52. }

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. 会声会影X10x9x8最新教程

    会声会影X10x9x8最新最全教程,全部都是干货,包含素材的,下载地址:百度网盘, https://pan.baidu.com/s/1AyVS-C_VcTEz_ir70u08xQ 以下为部分内容截图: ...

  2. Linux命令应用大词典-第23章 进程和服务管理

    23.1 ps:报告当前进程的快照 23.2 top:显示当前正在运行的进程 23.3 pgrep:按名称和其他属性查找进程 23.4 pidof:查找正在运行的进程的进程号 23.5 pstree: ...

  3. 孤荷凌寒自学python第八十二天学习爬取图片2

    孤荷凌寒自学python第八十二天学习爬取图片2 (完整学习过程屏幕记录视频地址在文末) 今天在昨天基本尝试成功的基础上,继续完善了文字和图片的同时爬取并存放在word文档中. 一.我准备爬取一个有文 ...

  4. Aizu - 2249

    注意先保证距离最短,再来判断价格 邻接矩阵回朝内存  ,要用邻接表的 #include<bits/stdc++.h> using namespace std; #define inf 0x ...

  5. apache访问403错误

    1.排查selinux 2.目录权限 3.WEB主目录是否正确

  6. Martin Fowler关于IOC和DI的文章(原版)

    Inversion of Control Containers and the Dependency Injection pattern In the Java community there's b ...

  7. python学习笔记03:python的核心数据类型

    从根本上讲,Python是一种面向对象的语言.它的类模块支持多态,操作符重载和多重继承等高级概念,并且以Python特有的简洁的语法和类型,OOP十分易于使用.Python的语法简单,容易上手. Py ...

  8. iOS-创建UIScrollerView(封装UIScrollerView)

    创建继承于UIView的类WJImageScrollView,代码实现如下: WJImageScrollView.h #import <UIKit/UIKit.h> /**点击图片bloc ...

  9. 会话模型与SSO

    关于会话模型其实网站已有很多帖子说明,其中有关于sessionid,cookie以及他们之间的关系,自己先了解吧 1 会话模型 会话模型是客户端和服务端交互的一种模型,会话模型友好的处理了客户端有无通 ...

  10. 基于3D卷积神经网络的人体行为理解(论文笔记)(转)

    基于3D卷积神经网络的人体行为理解(论文笔记) zouxy09@qq.com http://blog.csdn.net/zouxy09 最近看Deep Learning的论文,看到这篇论文:3D Co ...