HDU5125--magic balls(LIS)
题意:求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)的更多相关文章
- ZOJ 1093 Monkey and Banana (LIS)解题报告
ZOJ 1093 Monkey and Banana (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- 浅谈最长上升子序列(LIS)
一.瞎扯的内容 给一个长度为n的序列,求它的最长上升子序列(LIS) 简单的dp n=read(); ;i<=n;i++) a[i]=read(); ;i<=n;i++) ;j<i; ...
- ZOJ 2477 Magic Cube(魔方)
ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds Memory Limit: 65536 KB This is a very popular gam ...
- 最长递增子序列(LIS)(转)
最长递增子序列(LIS) 本博文转自作者:Yx.Ac 文章来源:勇幸|Thinking (http://www.ahathinking.com) --- 最长递增子序列又叫做最长上升子序列 ...
- Poj 2533 Longest Ordered Subsequence(LIS)
一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- Poj 3903 Stock Exchange(LIS)
一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...
- DP——最长上升子序列(LIS)
DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...
- 最长上升子序列(LIS)nlogn模板
参考https://www.cnblogs.com/yuelian/p/8745807.html 注意最长上升子序列用lower_bound,最长不下降子序列用upper_bound 比如123458 ...
- 低价购买 (动态规划,变种最长下降子序列(LIS))
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
随机推荐
- Android ProgressDialog 加载进度
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- C#&Sql获取中文字符拼音首字母的方法
C#获取字符拼音首字母,可以存储在数据库中以备将来按字母搜索的需求. public static string GetAc(string s) { try { string temp = Servic ...
- yii [error] [exception.CHttpException.404] exception 'CHttpException' with message 'Unable to resolve the request "favicon.ico".'
yii使用中,发现runtime文件夹下出现这个错误信息 解决办法:在生成的APP程序根目录下建.htaccess文件(前提是需要开启apache重写,具体如何开,查资料咯) 然后配置如下 <I ...
- MM32 备份域学习(兼容STM32)
MM32 备份域学习(兼容STM32) 内容提要 备份域工作原理 备份域特性 备份域的保护:侵入检测 备份域侵入检测 备份域电源与主要内容 备份域特性 20字节数据后备寄存器(中容量和小容量产品),或 ...
- printf用法之打印2进制,八进制,十进制,十六进制
printf是格式化输出函数,它可以直接打印十进制,八进制,十六进制,输出控制符分别为%d, %o, %x, 但是它不存在二进制,如果输出二进制,可以手写,但是也可以调用stdlib.h里面的itoa ...
- repeater 分页显示数据
表名:ChinaStates 控件:Repeater 查询代码DA: public class ChinaStatesDA { private DataClassesDataContext Conte ...
- 封装函数--->切换,添加,删除class
var obj={}; obj.className='a b c d active'; //切换class function toggle(obj,className) { var str=obj.c ...
- VS2015编译器问题简单记录
问题:VS2015community 无法查找或打开 pdb 文件 解决办法: 1.点击工具->选项, 在选项窗口中展开左侧菜单: 2.展开调试->常规,然后在右边的窗格中勾选“启用服务器 ...
- ORACLE输出详细错误信息错误行数
... COMMIT; --输出成功信息 DBMS_OUTPUT.PUT_LINE('RUN RESULT: SUCCESS'); EXCEPTION WHEN OTHERS THEN BEGIN R ...
- 马士兵SVN.
下载 服务端:VisualSVN Server 和客户端:TortoiseSVN cmd,并cd 到 VisualSVN Server安装目录下的bin目录. 新建库: svnadmin create ...