题意:求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. C primer plus 读书笔记第八章

    本章的标题是字符输入/输出和输入确认.主要内容是讨论用于I/O的标准函数. 1.getchar()和putchar() 这两个函数之前用过,我们通过这两个函数来讨论下缓冲区. #include &qu ...

  2. 使用正则表达式给网址添加a标签

    在内容中存在链接地址的时候,我们在前台显示时一定想自动的将地址添加上a标签,方便用户进入链接.使用正则表达式就能轻松实现. Jsvascript正则替换 //javascript 正则替换 var s ...

  3. codevs 2800 送外卖(状压dp)

    /* f[i][j] 表示走过的点构成i状态 且最后到达的点为j时的最优解 在那最后一个状态就是(1<<n+1)-1 每个点都到达 在由此回到0 */ #include<iostre ...

  4. HBuilder开发app ajax跨域 解决XMLHttpRequest

    <div id="a1" onclick="testXHR()" style="font-size: 5em;">sss1< ...

  5. FFMPEG 视频旋转设置

    fmpeg -i inputfile.mp4 -vf "transpose=1" outputfile.mp4 0=90CounterCLockwise and Vertical ...

  6. python面对对象编程----1:BlackJack(21点)

    昨天读完了<Mastering Object-oriented Python>的第一部分,做一些总结. 首先,第一部分总过八章,名字叫Pythonic Classes via Specia ...

  7. mongodb地理空间计算逻辑

    "1/地球半径"是怎么得出的 参考文档如下: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates http:// ...

  8. .Net使用SSH.NET通过SSH访问Linux主机

    使用了SSH.NET库,添加引用dll至项目,以下代码显示了点击按钮后SSH链接Linux主机执行命令并返回命令执行结果 protected void btnExcute_Click(object s ...

  9. 如何安装Oracle Database 11g数据库

    先选择你适合你的系统版本,32位系统的请选择32位的,64位系统可以使用32位也可以使用64位,建议采用64位的! 适用于 Microsoft Windows(32 位)的 Oracle Databa ...

  10. deep learning in nlp 资料文献

    Deep Learning for Natural Language Processing (without Magic) http://nlp.stanford.edu/courses/NAACL2 ...