补写一下

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. sql 系统函数

    --查看表备注SELECT a.column_id AS No, a.name AS 列名, isnull(g.[value],'-') AS 说明 FROM sys.columns a left j ...

  2. 关于如何将_variant_t型转化为int型和string类型

    1)将_variant_t型转化为int型 关于将_variant_t型转化为int型,网上有好多好多参考,但好多都很复杂并且还不对,其实整个转化过程就只一行代码可以搞定: _variant_t a; ...

  3. Android @Field parameters can only be used with form encoding

    今天在学习Retrofit的时候,当post请求时 public interface NewsDataService { @POST("news/list") Call<Ne ...

  4. php常见报错

    Php常见错误提示 一.Fatal error: Call to undefined function……函数不存在,可能的原因:系统不存在这个函数且你也没自定义 二.syntax error, un ...

  5. Hadoop多节点Cluster

    Hadoop多节点集群规划 服务起名称 内网IP HDFS YARN master 192.168.1.155 NameNode ResourceManager slave1 192.168.1.11 ...

  6. OpenCV : 基于切线方向的边缘增强算法

    使用切线方法,对切线方向上的边缘进行强化: 参考连接:图像锐化和边缘检测 代码: //在种子点方向上寻找合适的梯度,用于寻找边缘 //对low_Gray, high_gray之间的点寻找边缘 void ...

  7. PCL的学习必要性、重要性、意义及最初——持续修改中

    为什么学习PCL:图像描述:各种维度图像的逻辑描述形式  ^-^ 且点云库是机器人学领域的必备基础库,ICRA和IROS的计算机视觉相关一般都使用了PCL库,PCL库也成为ROS的基础部分,与机器人操 ...

  8. sqlitManager

    @interface sqlitManager : NSObject +(instancetype)sharedSqlitManager; -(void)createDB; -(void)create ...

  9. elasticsearch重建索引

    1.重建索引 一个field的设置是不能被修改的,如果要修改一个Field,那么应该重新按照新的mapping,建立一个index,然后将数据批量查询出来,重新用bulk api写入index中 批量 ...

  10. 整理Crontab 定时计划

    一. 什么是crontab? crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和 ...