hdu 5748 Bellovin BestCoder #84 1002
来源:互联网 发布:sql server是什么软件 编辑:程序博客网 时间:2024/04/30 10:18
Problem:bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=718&pid=1002
acm.hdu.edu.cn/showproblem.php?pid=5748
题意:输入一个序列 a1,a2,…,an,要重构一个序列 b1,b2,…,bn,使得在各自的序列中,以对应位置的值 ai 和 bi 结尾的 LIS 的长度一样,而且b序列的字典序要最小
分析:基于用二分求 LIS 的方法,后来的元素不影响先进的元素的LIS的长度,每次读入一个 ai,在装 a 的数组中二分找到不大于此 ai 的最小值的位置 p
新构造的 b 序列的对应值 bi 要和它有同样的 LIS 长度,就应该处在装 b 的数组的同一个位置 p 上
如果 b 数组的这个位置有值,就直接输出,如果是末尾,就要拓展一个新值
如果新进的 ai 能替换掉原来 a 数组中的某个值而不用拓展 a 数组的长度,那 bi 也应该能在 b 数组中找到。b 数组应该是随着 a 数组的拓展而拓展的
#include <stdio.h>#define SIZE 100000int a[SIZE],b[SIZE];int find(int x, int end){ int left = 0,right = end-1; while( left <= right ) { int mid = left + right >> 1; if( a[mid] < x ) left = mid + 1; else right = mid - 1; } return left;}int main(){ int t; scanf("%d",&t); while(t--) { int i,n; int lena,lenb; // a,b数组的长度 int big; // 表示下一次b数组拓展时要用到的值 scanf("%d",&n); for( i=lena=lenb=0, big=1; i<n; i++) { int in; // 新进的ai int p; // ai应在的位置 scanf("%d",&in); p = find(in,lena); if( p == lena ) // a序列“不够装”,要拓展 a[lena++] = in; else a[p] = in; // 可以替换原序列的某个值 if( p == lenb ) printf("%d%c", b[lenb++]=big++, i+1==n?'\n':' '); else printf("%d%c", b[p], i+1==n?'\n':' '); } } return 0;}因为 big 总是从1开始以1为步长递增,所以装在 b 数组里的肯定是1,2,…,lenb这样的值,所以其实不用 b 数组,直接用下标,但是要+1
… … if( p == lenb ) printf("%d%c", lenb++ +1, i+1==n?'\n':' '); else printf("%d%c", p+1, i+1==n?'\n':' '); … …
0 0
- hdu 5748 Bellovin BestCoder #84 1002
- BestCoder Round #84 1002 Bellovin
- hdoj 5748 Bellovin (LIS) 【BestCoder Round #84 】
- BestCoder Round #84 Bellovin
- HDU 5748 BestCoder Round #84 Bellovin (LIS)(树状数组)
- hdu 5748 Bellovin(BestCoder Round #84——最长递增子序列)
- HDU 5748 Bellovin [DIS]
- HDU 5748 Bellovin
- HDU 5748 Bellovin
- LIS hdu 5748 (Bellovin)
- HDU-5748-Bellovin【LIS】
- HDU 5748 Bellovin
- hdu 5748 Bellovin(LIS)
- HDU 5748 (Bellovin LIS)
- HDU 5748 Bellovin(LIS)
- 【HDU】5748 - Bellovin(LIS)
- HDU Problem 5748 Bellovin 【LIS】
- HDU 5748 Bellovin(LIS)
- mysql-数据备份系列
- 从输入URL到页面加载完的过程
- Sort Algorithm-->Select Sort
- LeetCode: Best Time to Buy and Sell Stock II
- 自定义button相关
- hdu 5748 Bellovin BestCoder #84 1002
- SVG实现边框动画效果
- svm系列之最大分隔超平面
- POJ2403
- 当说起message queue的时候,都在说什么?
- JDK和Tomcat部署时,版本不同的问题解决
- keepalived + nginx组建高可用负载平衡Web server集群
- 3.9 数据链路层 本章小结
- Handle ,HMODULE ,HINSTANCE,HINSTANCE