Codeforces 727 F. Polycarp's problems
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Polycarp's problems
Polycarp's problems time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT
[题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...
- AtcoderGrandContest 005 F. Many Easy Problems
$ >AtcoderGrandContest \space 005 F. Many Easy Problems<$ 题目大意 : 有一棵大小为 \(n\) 的树,对于每一个 \(k \i ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
随机推荐
- Spring MVC学习笔记——给Controller和视图传值
一.给Controller传值,值将显示在控制台 1.第一种:使用@RequestParam,改HelloController.java //RequestMapping表示用哪一个url来对应 @R ...
- 端口扫描(TCP)
还待优化... #include <string.h> #include <WinSock.h> #include <stdio.h> #pragma commen ...
- PyCharm光标变粗的解决办法
pycharm中光标变粗,如下: 此时变成了改写模式,只需要按下键盘的insert键即可
- ie6下absolute:fixed问题,完美兼容
普通css代码 .fix_foot{height: 30px; background: #ff0000; position: %; z-index:;} ie6兼容代码 * html,* html b ...
- CSS样式优先级
关于CSS样式优先级 一般情况下: [1位重要标志位] > [4位特殊性标志] > 声明先后顺序 !important > [ id > class > tag ] 使用 ...
- 快递api网接口快递调用方法
----------------实体类 [DataContract] public class SyncResponseEntity { public SyncResponseEntity() { } ...
- Thread 与 Runnable
在Java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...
- WinForm中新开一个线程操作 窗体上的控件(跨线程操作控件)
最近在做一个winform的小软件(抢票的...).登录窗体要从远程web页面获取一些数据,为了不阻塞登录窗体的显示,开了一个线程去加载数据远程的数据,会报一个错误"线程间操作无效: 从不是 ...
- C#反射机制(转)
一:反射的定义 审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. Sys ...
- python 运行时报错误SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2
File "1.py", line 2SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no ...