hdu 1025 Constructing Roads In JGShining's Kingdom(二分法+最长上升子序列)
来源:互联网 发布:云交易软件 编辑:程序博客网 时间:2024/05/16 09:39
题目大意:河的两岸有两个不同的国家,一边是穷国,一边是富国,穷国和富国的村庄的标号是固定的,穷国要变富需要和富国进行交流,需要建桥,并且建的桥不能够有交叉。问最多可以建多少座桥。
思路:建路时如下图所示
当一边的点已经固定了的时候,另外一边按照从小到大的序列与当前的边连接,得到最少的交叉。
题目给的第二组测试数据,如果按照图一则可以建2座桥,图二建一座桥
31 22 33 1
图一 图二
一开始用的是一般的最长上升子序列,结果超时。后来想到之前poj做过的3903 Stock Exchange(二分法),当时也是超时后来用的是二分法做的就过了。又敲了一遍代码还是没有过,提示的是超时。看了讨论区以后发现输出格式不对,存在一条以上的路时road为复数roads。以前从来没有遇到这样的输出,这次也算是一次积累吧!改了以后还是超时。上网百度了一下人家的做法,发现了一个新的技巧,因为每一个点只有一个所以不用排序,输入的u v可以直接用数组标记,line[u] = v;而我之前做的代码是先用sort函数将u排序后求解,时间变长。嗯,不错这又是一个好方法。
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;#define MAX 500005int point[MAX];int num, stack[MAX], top;int main(){ int i, cnt, max, a, b; int low, high, mid; cnt = 0; while (scanf("%d", &num)!=EOF) { cnt++; max = 0; for (i=0; i<num; i++) { scanf("%d%d", &a, &b); point[a] = b;//不用排序 } top = 0; stack[0] = -1; for (i=1; i<=num; i++) { if (top==0 || point[i]>stack[top]) { top++; stack[top] = point[i]; } else { low = 0; high = top; while (low <= high) { mid = (high+low) / 2; if (point[i] > stack[mid]) low = mid + 1; else high = mid - 1; } stack[low] = point[i]; } } if (top==1) printf("Case %d:\nMy king, at most %d road can be built.\n\n", cnt, top); else printf("Case %d:\nMy king, at most %d roads can be built.\n\n", cnt, top); } return 0;}
转载请注明出处:http://blog.csdn.net/u010499449
0 0
- hdu 1025 Constructing Roads In JGShining's Kingdom(二分法+最长上升子序列)
- Constructing Roads In JGShining's Kingdom(最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列+n*logn算法)
- hdu 1025 Constructing Roads In JGShining's Kingdom(树状数组求最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom (最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列(nlogn)
- HDU-1025 Constructing Roads In JGShining's Kingdom O(nlogn)的最长上升子序列
- 【DP】HDU 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列
- 最长上升子序列 nlogn算法 hdu 1025 Constructing Roads In JGShining's Kingdom
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列nlogn算法)
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列
- HDU 1025 —— Constructing Roads In JGShining's Kingdom 最长上升子序列
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP,LIS最长上升子序列)
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS最长上升子序列)
- 【HDU 1025】Constructing Roads In JGShining's Kingdom(最长上升子序列LIS)
- HDU 1025 A - Constructing Roads In JGShining's Kingdom(最长上升子序列)
- HDU 1025 - Constructing Roads In JGShining's Kingdom(最长上升子序列)
- javadoc生成方法及中文乱码处理
- Android核心基础——Day08_1
- linux下C编程之文件系统编程
- GET和POST请求的区别
- 第13周项目5-完整复制字符串
- hdu 1025 Constructing Roads In JGShining's Kingdom(二分法+最长上升子序列)
- 偏最小二乘 PLS erro 误差部分 < 三>
- python 实现Graph class
- PHP产生不重复随机数的方法
- BZOJ 1103 POI 2007 大都市meg 树状数组
- 第十三周 项目五--字符串操作(2.1)字符串复制功能
- hadoop 2.5.2 安装时遇到的异常
- 可以对 SSI 说,我叫 SI !
- PHP网站开发常用8个小技巧