题意:求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. apache 配置https(转)

    主要讲述在windows下apache配置SSL以实现http转换为https SSL: SSl是为Http传输提供安全的协议,通过证书认证来确保客户端和网站服务器之间的数据是安全.也就是说在SSL下 ...

  2. 招一位安防软件project师,嵌入式开发project师

    岗位职责 1.负责海思平台IPC产品应用层软件设计及维护 2.私有平台协议对接及为第三方提供技术支持. 任职资格: 1.较强的学习.领悟能力,能够高速熟悉公司现有代码. 2.熟练掌握C.C++开发语言 ...

  3. [转] PostgreSQL学习手册(数据表)

    from: http://www.cnblogs.com/stephen-liu74/archive/2012/04/23/2290803.html 一.表的定义: 对于任何一种关系型数据库而言,表都 ...

  4. Linux查看硬件信息以及驱动设备的命令

    用硬件检测程序kuduz探测新硬件:service kudzu start ( or restart) 查看CPU信息:cat /proc/cpuinfo 查看板卡信息:cat /proc/pci 查 ...

  5. Mounting File Systems

    1.Mounting File Systems Just creating a partition and putting a file system on it is not enough to s ...

  6. SQL 查询字段为值不为空

      方法一sql="select   *   from   table   where   id<>null   "     or   sql="select ...

  7. php锁表

    用PHP实现mysql锁表 mysql锁表,是利用相关的SQL语句 //执行SQL语句 锁掉userinfo表 $sql = "LOCK TABLES userinfo WRITE" ...

  8. Php 常用类

    图表库下面的类库可以让你很简单就能创建复杂的图表和图片.当然,它们需要GD库的支持.pChart - 一个可以创建统计图的库.Libchart - 这也是一个简单的统计图库.JpGraph - 一个面 ...

  9. java事件处理5(窗口,窗口坐监视器

    WindowEvent窗口事件 添加接口 addWindowListener(WindowEvent e) 接口有七个方法 public void windowActivated(WindowEven ...

  10. static和const关键字的作用

    static关键字至少有下列n个作用: (1)函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值: (2)在模块内的stat ...