0x58 数据结构优化DP
补写一下
poj3171 设f[i]表示覆盖L~i的最小花费,把区间按左端点排序,枚举区间,f[a[i].r]=min{f[a[i].l~(a[top].r-1)]}+a[i].c (当然还要和原值比较的)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; struct node{int l,r,d;}a[];
bool cmp(node n1,node n2){return n1.r<n2.r;} struct trnode
{
int l,r,lc,rc,c;
}tr[];int trlen;
void bt(int l,int r)
{
int now=++trlen;
tr[now].l=l;tr[now].r=r;tr[now].c=(<<);
tr[now].lc=tr[now].rc=-;
if(l<r)
{
int mid=(l+r)/;
tr[now].lc=trlen+;bt(l,mid);
tr[now].rc=trlen+;bt(mid+,r);
}
}
void change(int now,int p,int k)
{
if(tr[now].l==tr[now].r){tr[now].c=k;return ;} int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/;
if(p<=mid)change(lc,p,k);
else change(rc,p,k); tr[now].c=min(tr[lc].c,tr[rc].c);
}
int getmin(int now,int l,int r)
{
if(tr[now].l==l&&tr[now].r==r)return tr[now].c; int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/; if(r<=mid) return getmin(lc,l,r);
else if(mid+<=l)return getmin(rc,l,r);
else return min(getmin(lc,l,mid),getmin(rc,mid+,r));
} int f[];
int main()
{
int n,L,R;
scanf("%d%d%d",&n,&L,&R);L++,R++;
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].d);
a[i].l++,a[i].r++;
if(a[i].r<L||R<a[i].l)i--,n--;
if(a[i].l<L)a[i].l=L;
if(R<a[i].r)a[i].r=R;
}
sort(a+,a+n+,cmp); int top=; trlen=,bt(L,R);
memset(f,,sizeof(f));
for(int i=L;i<=R;i++)
{
while(top<=n&&a[top].r==i)
{
if(a[top].l==L)
{
if(a[top].d<f[i])
f[i]=a[top].d, change(,i,f[i]);
}
else
{
int k=getmin(,a[top].l-,a[top].r-)+a[top].d;
if(k<f[i])
f[i]=k, change(,i,f[i]);
}
top++;
}
}
if(f[R]==f[])printf("-1\n");
else printf("%d\n",f[R]);
return ;
}
poj3171
hdu5542 f[i][j]表示枚举到第几个位置,长度为j的严格上升序列有多少(注意一定包括第i个位置)
f[i][j]=∑(k<j&&a[k]<a[j])f[k][j-1] 这里先离散化,然后开m个树状数组记录,想想cdq分治,时间维有序第一个限制不管,第二个用树状数组解决。实际操作中,是不用定义数组的,直接插入树状数组中相应位置即可。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int mod=1e9+; int lslen;int s[][];
int lowbit(int x){return x&-x;}
void change(int x,int w,int k)
{
while(x<=lslen)
{
s[w][x]=(s[w][x]+k)%mod;
x+=lowbit(x);
}
}
int getsum(int x,int w)
{
int ret=;
while(x>)
{
ret=(ret+s[w][x])%mod;
x-=lowbit(x);
}
return ret;
} //----------------bit--------------------- int a[],ls[];
int main()
{
int T,T_T=;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
lslen=;
for(int i=;i<=n;i++)
scanf("%d",&a[i]), ls[++lslen]=a[i];
sort(ls+,ls+lslen+);
lslen=unique(ls+,ls+lslen+)-ls-;
for(int i=;i<=n;i++)
a[i]=lower_bound(ls+,ls+lslen+,a[i])-ls; memset(s,,sizeof(s));
for(int i=;i<=n;i++)
{
change(a[i],,);
int li=min(i,m);
for(int j=;j<=li;j++)
change(a[i],j,getsum(a[i]-,j-));
} printf("Case #%d: %d\n",++T_T,getsum(lslen,m));
}
return ;
}
hdu5542
0x58 数据结构优化DP的更多相关文章
- 数据结构优化dp
本以为自己的dp已经成熟了没想到在优化上面还是欠佳 或者是思路方面优化dp还不太行. 赤壁之战 当然 很有意思的题目描述 大体上是苦肉计吧 .盖黄 ... 题意是 求出长度为m的严格上升子序列的个数 ...
- HAOI2008 木棍分割 数据结构优化dp+二分答案
很久之前打的题,现在补篇博客 打滚动数组 #E. 木棍分割 Accepted 100 1712 ms 1512 KiB 2019-05-07 17:01:23 Short 不打滚动数组 #419. ...
- HDU 4990 Ordered Subsequence --数据结构优化DP
题意:给一串数字,问长度为m的严格上升子序列有多少个 解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数, 则有dp[i][j] = S ...
- The Battle of Chibi(数据结构优化dp,树状数组)
The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...
- Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp
比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...
- $Poj2376\ Poj3171\ Luogu4644\ Cleaning\ Shifts$ 数据结构优化$DP$
$Poj$ $AcWing$ $Luogu$ $ps:$洛谷题目与$Poj$略有不同,以下$Description$是$Poj$版.题目的不同之处在于洛谷中雇用奶牛的费用不相同,所以不可以 ...
- $HDOJ5542\ The\ Battle\ of\ Chibi$ 数据结构优化$DP$
$AcWing$ $Description$ $Sol$ 首先显然是是以严格递增子序列的长度为阶段,由于要单调递增,所以还要记录最后一位的数值 $F[i][j]$表示前$i$个数中以$A_i$结尾的长 ...
- 洛谷P4644 [USACO2005 Dec]Cleaning Shifts 清理牛棚 [DP,数据结构优化]
题目传送门 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness ...
- bzoj1233 单调队列优化dp
https://www.lydsy.com/JudgeOnline/problem.php?id=1233 数据结构优化dp的代码总是那么抽象 题意:奶牛们讨厌黑暗. 为了调整牛棚顶的电灯的亮度,Be ...
随机推荐
- CSS元素水平垂直居中的方法
1. 元素水平居中 1.1 设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...
- 删除django
1.命令行运行python 2.import django3.print(django.__path__)4.删除django目录即可
- ubuntu16.04安装KDE
由于对KDE界面情有独钟,升级到ubuntu之后,第一件事就是安装kde桌面 命令: add-apt-repository ppa:kubuntu-ppa/backports apt-get upda ...
- 微信小程序播放背景音乐
小程序实现和h5一样的音乐图标一直旋转. 一..js中封装旋转动画方法 添加animation属性 data:{ animation:''" } 改变animation的值(官网提供角度范围 ...
- Jsp页面中常用的EL表达式
首先引入标签 <%@ page language="java" contentType="text/html; charset=utf-8" pageE ...
- PAT_A1144#The Missing Number
Source: PAT A1144 The Missing Number (20 分) Description: Given N integers, you are supposed to find ...
- PAT_A1142#Maximal Clique
Source: PAT A1142 Maximal Clique (25 分) Description: A clique is a subset of vertices of an undirect ...
- 01 c++常见面试题总结
https://www.cnblogs.com/yjd_hycf_space/p/7495640.html C++常见的面试题 http://c.tedu.cn/workplace/217749. ...
- 关于 多个git用户或多个git管理工具切换时出现的问题总结
在这几天遇到了个比较头痛的问题 因为在同时使用多个git工具(gitlab,github.gitee)由于账户的问题和这个仓库指定地址,导致拉代码和推代码不能正常运行 问题解决: 对于多个git直接的 ...
- 0808关于RDS如何恢复到本地教程
转自http://www.cnblogs.com/ilanni/archive/2016/02/25/5218129.html 公司目前使用的数据库是阿里云的RDS,目前RDS的版本为mysql5.6 ...