题意:求a数组的LIS,但是加了一个条件,为了LIS最大 b[i] a[i]可以交换。最多交换mci;

赤果果的dp啊,可是这个题用线段树的话却会TLE,,由于查询的只是1-x的最大值 因此我们可以用树状数组来查询最值,话说树状数组真的比线段树快乐好多啊。

本地随机数据,线段树2.7s左右,,树状数组不到1s。。。

大致思路:建m个树状数组,对于位置i的数字有两种状态①交换a b②不交换。两种情况分开,

Code:

 #include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
int a[maxn],b[maxn];
int c[maxn][maxn<<];
int vec[maxn<<],idx;
inline int lowbit (int x)
{
return x & -x;
}
void add(int x,int d,int k)
{
while (x < *maxn)
{
c[k][x] = max(d,c[k][x]);
x += lowbit(x);
}
}
int get_max(int x,int k)
{
int res = ;
while (x)
{
res = max(res,c[k][x]);
x -= lowbit(x);
}
return res;
}
int hash_(int x)
{
return lower_bound(vec,vec+idx,x) - vec + ;
} int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t;
scanf ("%d",&t);
while (t--)
{
int n,m;
scanf ("%d%d",&n,&m);
m = min(n,m);
idx = ;
memset(c,,sizeof(c));
for (int i = ; i < n; i++)
{
scanf ("%d%d",a+i,b+i);
vec[idx++] = a[i];
vec[idx++] = b[i];
}
sort(vec,vec+idx);
idx = unique(vec,vec+idx) - vec;
for (int i = ; i< n; i++)
{
a[i] = hash_(a[i]);
b[i] = hash_(b[i]);
}
int ans = ;
for (int i = ; i < n ; i++)
{
for (int j = ; j <= m; j++)
{
int tmp = ;
tmp += get_max(a[i]-,j);
add(a[i],tmp,j);
ans = max(tmp,ans);
if (j)
{
tmp = ;
tmp += get_max(b[i]-,j+);
add(b[i],tmp,j);
ans = max(tmp,ans);
}
}
}
printf("%d\n",ans);
}
return ;
}

附树状数组求最值模板。!!此时求最大值时 修改操作只能 改大,不可以改小。求最小值时相反。

 int lowbit (int x)
{
return x & -x;
}
int c[N];
void add(int x,int val) // 位置i的数更新为val
{
while (x < N)
{
c[x] = max(c[x],val);
x += lowbit(x);
}
}
int get_max(int x)
{
int res = ; //这个0 不是固定的,,取一个较小的数就可以了。
while (x)
{
res = max(res,c[x]);
x -= lowbit(x);
}
return res;
}

代码如下

HDU5125--magic balls(LIS)的更多相关文章

  1. ZOJ 1093 Monkey and Banana (LIS)解题报告

    ZOJ  1093   Monkey and Banana  (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. 浅谈最长上升子序列(LIS)

    一.瞎扯的内容 给一个长度为n的序列,求它的最长上升子序列(LIS) 简单的dp n=read(); ;i<=n;i++) a[i]=read(); ;i<=n;i++) ;j<i; ...

  3. ZOJ 2477 Magic Cube(魔方)

    ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds      Memory Limit: 65536 KB This is a very popular gam ...

  4. 最长递增子序列(LIS)(转)

    最长递增子序列(LIS)   本博文转自作者:Yx.Ac   文章来源:勇幸|Thinking (http://www.ahathinking.com)   --- 最长递增子序列又叫做最长上升子序列 ...

  5. Poj 2533 Longest Ordered Subsequence(LIS)

    一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...

  6. Poj 3903 Stock Exchange(LIS)

    一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...

  7. DP——最长上升子序列(LIS)

    DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...

  8. 最长上升子序列(LIS)nlogn模板

    参考https://www.cnblogs.com/yuelian/p/8745807.html 注意最长上升子序列用lower_bound,最长不下降子序列用upper_bound 比如123458 ...

  9. 低价购买 (动态规划,变种最长下降子序列(LIS))

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

随机推荐

  1. 10 Questions To Make Programming Interviews Less Expensive--reference

    Conducting Interview is not cheap and costs both time and money to a company. It take a lot of time ...

  2. UITableView的编辑(插入、删除、移动)

    先说两个方法beginUpdates和endUpdates,几点注意事项: 一般我们把行.块的插入.删除.移动写在由这两个方法组成的函数块中.如果你不是在这两个函数组成的块中调用插入.删除.移动方法, ...

  3. Java基础知识强化之集合框架笔记01:集合的由来与数组的区别

    1. 集合的由来: 我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储.而要想存储多个对象,就不能是一个基本的变量,而应该 ...

  4. 查看linux系统版本

    1.查看内核版本 #cat /proc/version Linux version 2.6.18-164.el5 (mockbuild@x86-003.build.bos.redhat.com) (g ...

  5. nginx 站点80跳443配置

    server { listen 80; server_name www.furhacker.cn; location /{# return 301; rewrite ^(.*)$ https://$h ...

  6. 新浪微博开放平台OAuth授权解决方案(含代码)

    前几日一位朋友项目中需要使用新浪微博的接口,故和这位朋友一同研究了新浪微博开放平台上面所提供的资料,首先要使用这些接口是需要用户登录并且授权的,新浪微博开放平台其实是提供两种授权方式的,第一种是:OA ...

  7. hdu 5105

    题意: y=|a*x^3+b*x^2+c*x+d|    求y的最大值? 题目是bc上的,之前写的时候,没考虑0的情况(太笨了).... 水题吧.... AC代码: #include <iost ...

  8. 粘帖屏幕截图到web页面插件 screenshot-paste

    在很多场合下,我们可能有这样的需求:提供个屏幕截图上传到系统,作为一个凭证.传统的操作方式是:屏幕截图,保存文件到本地,在web页面上选择本地文件并上传,这里至少需要三步.有没有可能直接将截图粘帖到w ...

  9. HTML5触摸屏touch事件使用实例1

    1.源码: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ...

  10. 关于JS、JQuery、CSS的小知识点

    1.将字符串转换成json列表格式如下: var getaddress = appcan.libuser.getAddress(); var address=JSON.parse(getaddress ...