HDU 5125 magic balls
来源:互联网 发布:ubuntu端口流量监控 编辑:程序博客网 时间:2024/06/07 15:42
题目 :LINK
和普通的LIS相比,是多了一个可以交换m次的机会。不考虑复杂度的话,可以很类似求普通LIS O(n^2)的方法,每次求前面小于当前的值且LIS最长的那一个进行转移。
如果现在还是按照这样的话,复杂度是O(n*n*m),优化的话,可以考虑优化如何快速找到,在交换x次的情况下前面比当前值小的最长LIS是多大。
我们可以采用树状数组,进行维护m+1个树状数组每次寻找当前前面的最大值,复杂度O(n*m*log(n)),线段树的操作虽然也是log(n)但是常数比树状数组大,可能会耗时较多。
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <cmath> #include <queue> #include <map> #include <set> using namespace std; #define INF 1000000000 //typedef __int64 LL; #define N 1005 int t, n, m, a[N], b[N], c[N][2*N], nn, num[2*N]; int lowbit(int x) { return x & (-x); } int get_max(int x, int k) { int ret = 0; while(x > 0) { ret = max(ret, c[k][x]); x -= lowbit(x); } return ret; } void add(int x, int ad, int k) { while(x <= nn) { c[k][x] = max(ad, c[k][x]); x += lowbit(x); } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif // ONLINE_JUDGE scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); nn = 0; for(int i = 1;i <= n;i ++) { scanf("%d%d", &a[i], &b[i]); nn ++; num[nn] = a[i]; nn ++; num[nn] = b[i]; } sort(num+1, num+1+nn); nn = unique(num+1, num+1+nn) - num - 1; for(int i = 1; i<= n; i++) { a[i] = lower_bound(num+1, num+1+nn, a[i]) - num; b[i] = lower_bound(num+1, num+1+nn, b[i]) - num; } memset(c, 0, sizeof(c)); int ans = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j <= m; j++) { int temp; temp = get_max(a[i] - 1, j) + 1; add(a[i], temp, j); ans = max(ans, temp); if(j!=m) { temp = get_max(b[i] - 1, j+1) + 1; add(b[i], temp, j); ans = max(ans, temp); } } } printf("%d\n", ans); } return 0; }
0 0
- HDU 5125 magic balls
- hdu 5125 magic balls
- hdu 5125 magic balls
- hdu 5125 magic balls(dp)
- hdu 5125 magic balls(DP)
- HDU 5125 Magic Balls DP(LIS变形)
- HDU 5125 magic balls(dp+线段树优化)
- hdu 5125 magic balls LIS (dp+树状数组优化)
- HDOJ 5125 magic balls DP
- magic balls
- HDOJ 5125 magic balls(树状数组优化)
- hdu5125 magic balls BIT求LIS
- hdu 3635 Dragon Balls
- HDU-3635 Dragon Balls
- HDU 3635 Dragon Balls
- HDU-3635-Dragon Balls
- hdu 3635 Dragon Balls
- HDU 3635 Dragon Balls
- What technical details should a programmer of a web application consider before making the site publ
- 51nod 1090 3个数和为0 & 51nod 1267 4个数和为0(标记二分)
- 【BZOJ】【P3275】【Number】【题解】【最小割】
- Python笔记(20141130)
- 分支-07. 比较大小(10)
- HDU 5125 magic balls
- 【UNIX】什么是线程的同步和互斥
- 7个示例科普CPU Cache
- Android图灵机器人的实现(一)
- Linux下Python获取IP地址的代码
- 快速排序算法
- PhotoShop算法实现--伪彩色图像处理(八)
- CentOS下用OpenSSH构建SSH服务器
- 分拆素数和