补写一下

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的更多相关文章

  1. 数据结构优化dp

    本以为自己的dp已经成熟了没想到在优化上面还是欠佳 或者是思路方面优化dp还不太行. 赤壁之战 当然 很有意思的题目描述 大体上是苦肉计吧 .盖黄 ... 题意是 求出长度为m的严格上升子序列的个数 ...

  2. HAOI2008 木棍分割 数据结构优化dp+二分答案

    很久之前打的题,现在补篇博客 打滚动数组 #E. 木棍分割 Accepted 100 1712 ms 1512 KiB   2019-05-07 17:01:23 Short 不打滚动数组 #419. ...

  3. HDU 4990 Ordered Subsequence --数据结构优化DP

    题意:给一串数字,问长度为m的严格上升子序列有多少个 解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数, 则有dp[i][j] = S ...

  4. 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 ...

  5. Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp

    比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...

  6. $Poj2376\ Poj3171\ Luogu4644\ Cleaning\ Shifts$ 数据结构优化$DP$

    $Poj$    $AcWing$    $Luogu$ $ps:$洛谷题目与$Poj$略有不同,以下$Description$是$Poj$版.题目的不同之处在于洛谷中雇用奶牛的费用不相同,所以不可以 ...

  7. $HDOJ5542\ The\ Battle\ of\ Chibi$ 数据结构优化$DP$

    $AcWing$ $Description$ $Sol$ 首先显然是是以严格递增子序列的长度为阶段,由于要单调递增,所以还要记录最后一位的数值 $F[i][j]$表示前$i$个数中以$A_i$结尾的长 ...

  8. 洛谷P4644 [USACO2005 Dec]Cleaning Shifts 清理牛棚 [DP,数据结构优化]

    题目传送门 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness ...

  9. bzoj1233 单调队列优化dp

    https://www.lydsy.com/JudgeOnline/problem.php?id=1233 数据结构优化dp的代码总是那么抽象 题意:奶牛们讨厌黑暗. 为了调整牛棚顶的电灯的亮度,Be ...

随机推荐

  1. Spring Boot (9) mybatis全注解化

    ORM对比图 框架对比 Spring JDBC Spring Data Jpa Mybatis 性能 性能最好 性能最差 居中 代码量 多 少 多 学习成本 低 高 居中 推荐指数 ❤❤❤ ❤❤❤❤❤ ...

  2. SQLServer 在存储过程里使用事务控制的简单小例子

    alter proc sp_test(     @name varchar(50))asbegin    --开始事务   begin transaction   --设置一个存储报错代码的变量   ...

  3. 三维重建7:Visual SLAM算法笔记

    VSLAM研究了几十年,新的东西不是很多,三维重建的VSLAM方法可以用一篇文章总结一下. 此文是一个好的视觉SLAM综述,对视觉SLAM总结比较全面,是SLAM那本书的很好的补充.介绍了基于滤波器的 ...

  4. MFC 添加文件路径 遍历文件

    .添加MFC选择文件路径,使用MessageBox显示信息. void CMyCalLawsDlg::OnBnClickedAddfolder() { wchar_t* p; wchar_t szPa ...

  5. 微服务常见安全认证方案Session token cookie跨域

    HTTP 基本认证 HTTP Basic Authentication(HTTP 基本认证)是 HTTP 1.0 提出的一种认证机制,这个想必大家都很熟悉了,我不再赘述.HTTP 基本认证的过程如下: ...

  6. python 各个地方导航(方便查询,持续更新!)

    老男孩python全栈开发教程,武沛齐老师的知识点!:戳这里>>> 老男孩python全栈开发教程,linhaifeng老师的知识点!:戳这里>>> 老男孩pyth ...

  7. Oralce导入数据库出现某一列的值太大

    这是由于导出的文件所运行的Oracle,和导入所运行的Oracle机器字符集不相同导致的,在UTF-8中有的汉字占三个字节, 并不是所有的都占两个字节,

  8. 2019-04-12 SQL 主键约束

    create table dbo.AssetPool( ID bigint not null, poolname nvarchar(50)not null, constraint pk_AssetPo ...

  9. C# DataGridView 使用

    之前咩有做个界面的东西,更没有使用过DataGirdView 这个控件. 现在本来是准备用DeV呢,结果发现我的DEV没有注册,只好暂时用这个DataGridView来替代使用了. 我现在要是设置两列 ...

  10. base64模块 简单了解

    base64,字符串文本编码解码,方便数据进行传输 import base64 '''编码解码''' st = 'ni hao'.encode('utf8') result = base64.b64e ...