Description

有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslant 2 \times 10^5\)

Sol

DP+二分.

发现这个东西有后效性,就是前面选不选会影响后面的决策,并且权值太大无法记录.

但是我们可以倒着做,因为后面的决策无法影响前面的决策.

\(f[i][j]\) 表示到 \(i\) 删掉 \(j\) 个至少需要的初始权值.

因为初始权值非负,所以不可能在中途中出现负数,要处理掉,然后就两个决策,删或不删,转移就很好写了.

统计答案的时候他有单调性,可以二分.

Code

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; typedef long long LL;
const int N = 755; int n,m;
LL a[N],f[N][N],w[N]; inline LL in(LL x=0,char ch=getchar()){ while(ch>'9'||ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
void work(LL x){
int l=0,r=n,mid;
while(l<=r){
mid=(l+r)>>1;
if(f[1][mid] <= x) r=mid-1;
else l=mid+1;
}printf("%d\n",l);
}
int main(){ scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) cin>>a[i]; memset(f,0x3f,sizeof(f));
f[n+1][0]=0LL;
for(int i=n;i;i--) for(int j=0;j<n-i+1;j++){
f[i][j]=min(f[i][j],max(0LL,f[i+1][j]-a[i]));
f[i][j+1]=min(f[i][j+1],f[i+1][j]);
} for(;m--;) work(in());
return 0;
}

  

Codeforces 727 F. Polycarp's problems的更多相关文章

  1. codeforces 659F F. Polycarp and Hay(并查集+bfs)

    题目链接: F. Polycarp and Hay time limit per test 4 seconds memory limit per test 512 megabytes input st ...

  2. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  3. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  4. Polycarp's problems

    Polycarp's problems time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT

    [题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...

  6. AtcoderGrandContest 005 F. Many Easy Problems

    $ >AtcoderGrandContest \space 005 F.  Many Easy Problems<$ 题目大意 : 有一棵大小为 \(n\) 的树,对于每一个 \(k \i ...

  7. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  8. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  9. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

随机推荐

  1. css让图片作为按钮的背景并且大小合适

    最近在做ASP大作业,在做html页面的时候想把一个图片作为按钮的背景,搞了好久终于在csdn上找到了满意的答案: background-size: cover; 只需要这一句就ok了,就是这么简答. ...

  2. Java引用机制——reference

    所谓引用传递就是指将堆内存空间的使用权交给多个栈内存空间. 例子<1> public class Aliasing { int temp = 30; public static void ...

  3. Android学习笔记——SQLite

    该工程的功能是实现关于数据库的操作,即creat.update.insert.query.delete 调试的时候请用模拟器,用真机调试的时候进入cmd-adb shell,再进入cd data/da ...

  4. 以全局监听的方式处理img的error事件

    http://www.ovaldi.org/2015/09/11/%E4%BB%A5%E5%85%A8%E5%B1%80%E7%9B%91%E5%90%AC%E7%9A%84%E6%96%B9%E5% ...

  5. Tomcat编码问题及访问软链接文件设置

    Tomcat编码问题及访问软链接文件设置 一.编码问题:让其支持UTF-8格式 修改tomcat中server.xml Connector port=" protocol="org ...

  6. Oracle VM Virtual

    版本5.xx不能导入 要用旧版的4.3.10

  7. target 事件属性

    定义和用法 target 事件属性可返回事件的目标节点(触发该事件的节点),如生成事件的元素.文档或窗口. 语法 event.target 实例 下面的例子可获得触发事件的元素: <html&g ...

  8. javascript模块简单写法

    写法1: (function (wd, doc) { var mw = {}; mw.noConflict = noConflict; var _$ = wd.$; wd.$ = mw; functi ...

  9. PHP本地通过映射,实现多域名访问

    PHP本地通过映射,实现多域名访问 第一步:先在C盘下的windows/system32/drivers/etc/hosts文件加上 127.0.0.1       localhost    127. ...

  10. JavaScriptSerializer中日期序列化问题

    js请求的json数据返回前台的DateTime 类型被替换成了:\/Date(1404098342309)\/. 这个1404098342309数值,是1970年1月1日(DateTime的最小值) ...