#178. 「2019冬令营提高组」全连

显然我们可以得出一个$O(n^2)$的dp方程

记$f(i)$为取到第$i$个音符时的最大分数,枚举下一个音符的位置$j$进行转移。

蓝后我们就可以用树状数组存下$f(i)$的最大值,每次用$logn$的复杂度每次询问$j=1 \rightarrow i-t[i]$中最大$f(j)$值。

酱紫复杂度就变成了$O(nlogn)$

对于$f(i)$在位置$i+t[i]$之后才能作为转移的一个选择的问题,我们可以打一个延迟标记(ping函数),用类似链式前向星的结构存储(就像存边一样)。

每次到达一个$k$,就把每个从$k$开始起转移作用的$f(i)加入树状数组。

#include<cstdio>
typedef long long ll;
ll max(ll a,ll b){return a>b?a:b;}
void read(ll &x){
char c=getchar();x=;
while(c<''||c>'') c=getchar();
while(''<=c&&c<='') x=x*+(c^),c=getchar();
}
#define N 1000005
ll s[N],a[N],t[N],f[N],ans;
int n,cnt,hd[N],nxt[N],ed[N],poi[N];
void add(int x,ll v){for(;x<=n;x+=x&-x)s[x]=max(s[x],v);}
ll ask(int x){ll re=;for(;x;x-=x&-x)re=max(re,s[x]);return re;}
void ping(int x,int y){
nxt[ed[x]]=++cnt; hd[x]=hd[x]?hd[x]:cnt;
ed[x]=cnt; poi[cnt]=y;
}
int main(){
freopen("fc.in","r",stdin);
freopen("fc.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;++i) read(t[i]);
for(int i=;i<=n;++i) read(a[i]);
for(int i=;i<=n;++i){
for(int j=hd[i];j;j=nxt[j]) add(poi[j],f[poi[j]]);//f[poi[j]]从i开始可以作为一个选择,加入树状数组
f[i]=a[i]*t[i];
if(i>t[i]) f[i]+=ask(i-t[i]);//查询1~i-t[i]的最优选择
if(i+t[i]<=n) ping(i+t[i],i);//把f(i)加入位置i+t[i]的标记中
ans=max(ans,f[i]);
}printf("%lld",ans);
return ;
}

fjwc2019 D1T1 全连(dp+树状数组)的更多相关文章

  1. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  2. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  3. 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...

  4. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  5. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  7. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

  8. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  9. [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...

随机推荐

  1. windows 邮槽mailslot 在服务程序内建立后客户端无权限访问(GetLastError() == 5)的问题

    邮槽创建在服务程序内,可以创建成功, 但外部客户端连接时 m_hMailslot = CreateFile("\\\\.\\mailslot\\zdpMailslot",GENER ...

  2. Java Selenium - 浏览器操作

    浏览器主要操作方法来自接口 org.openqa.selenium.WebDriver , 实现于org.openqa.selenium.remote.RemoteWebDriver这个类,然后不同浏 ...

  3. 一个简单的sel server 函数的自定义

    创建自定义函数:use 数据库名gocreate function 函数名(@pno int)returns intasbegin  declare @a int   if not exists(se ...

  4. iOS 第三方框架-MJExtension

    1.数组转换成模型数组 // 将 "微博字典"数组 转为 "微博模型"数组 NSArray *newStatuses = [HWStatus objectArr ...

  5. python我的tkinter学习,玩玩

    1.开始 #!/usr/bin/env python #coding:utf-8 import Tkinter ############################################ ...

  6. 关于事件循环机制event loop

    setTimeout(()=> { console.log('settimeout') },100) console.log('开始') console.log('结束') new Promis ...

  7. python中安装并使用redis

    数据缓存系统:1:mongodb:是直接持久化,直接存储于硬盘的缓存系统2:redis: 半持久化,存储于内存和硬盘3:memcache:数据只能存储在内存里的缓存系统 redis是一个key-val ...

  8. [13]Windows 内核情景分析 --- 网络通信

    典型的基于tcpip协议套接字方式的网络通信模块层次: 应用程序 socket api WS2_32.dll socket irp Afd.sys tdi irp Tcpip.sys 回调函数接口 各 ...

  9. 06 str() bytes() 编码转换

    x = str() #创建字符串#转换成字符串,字节,编码 m = bytes()#创建字节#转换成字节,字符串,要编程什么编码类型的字节 a = "李露" b1 = bytes( ...

  10. E. Bear and Drawing

         E. Bear and Drawing time limit per test 1 second memory limit per test 256 megabytes input stan ...