题意:求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. Project Euler problem 61

    题意很明了. 然后我大概的做法就是暴搜了 先把每个几边形数中四位数的处理出来. 然后我就DFS回溯着找就行了. 比较简单吧. #include <cstdio> #include < ...

  2. VS2008 动态库和静态库的生成和加载

    第一:动态库和静态库的生成: 1) 新建一个生成dll工程: 文件->新建->项目->Win32->Win32控制台应用程序 输入项目名称:dllTest ,项目路径:D:\V ...

  3. Windows系统下安装Python的SSH模块教程

    Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是 ...

  4. C++输出hello world 详细注释

    /* #include<iostream> #:预处理标志,后面跟预处理指令,include:预处理指令,包含 <iostream>:c++头文件,输入输出流 这行的作用就是在 ...

  5. mwc config.h 中文注释

    #ifndef CONFIG_H_ #define CONFIG_H_ /*************************************************************** ...

  6. ZOJ3161

    朴素动态规划 ZOJ3161 题意:(严重标题党)老板不想让客人走,客人不想留,客人按顺序排好,老板抽8g(书上翻译成八卦,神翻译),抽到的 如果相邻,其中一个人由客人决定离开,求最后黑心的老板最多能 ...

  7. ruby 知识点

    $LOAD_PATH 执行 require 读取文件时搜索的目录名数组,也可以写作 $: 创建 URI 的时候可以直接这样 URI("http://www.dy2018.com/i/9751 ...

  8. xcode 删除 Provisioning Profile

    provisioning profile path: ~/Library/MobileDevice/Provisioning Profiles 打开并日期排序,删除老的 provisioning pr ...

  9. php中bindValue的批量提交sql语句

    php预编译sql语句,可以批量提交sql,也可以实现防注入 <?php $dsn='mysql:host=127.0.0.1;port=3306;dbname=bisai'; $usernam ...

  10. 跟我学android-搭建Android开发环境(一)

    Android官网地址:http://developer.android.com/,下载和安装 AndroidSDK请按如下步骤进行: 下载ADT 和SDK:http://developer.andr ...