Polycarp's problems
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp is an experienced participant in Codehorses programming contests. Now he wants to become a problemsetter.

He sent to the coordinator a set of n problems. Each problem has it's quality, the quality of the i-th problem is ai (ai can be positive, negative or equal to zero). The problems are ordered by expected difficulty, but the difficulty is not related to the quality in any way. The easiest problem has index 1, the hardest problem has index n.

The coordinator's mood is equal to q now. After reading a problem, the mood changes by it's quality. It means that after the coordinator reads a problem with quality b, the value b is added to his mood. The coordinator always reads problems one by one from the easiest to the hardest, it's impossible to change the order of the problems.

If after reading some problem the coordinator's mood becomes negative, he immediately stops reading and rejects the problemset.

Polycarp wants to remove the minimum number of problems from his problemset to make the coordinator's mood non-negative at any moment of time. Polycarp is not sure about the current coordinator's mood, but he has m guesses "the current coordinator's moodq = bi".

For each of m guesses, find the minimum number of problems Polycarp needs to remove so that the coordinator's mood will always be greater or equal to 0 while he reads problems from the easiest of the remaining problems to the hardest.

Input

The first line of input contains two integers n and m (1 ≤ n ≤ 750, 1 ≤ m ≤ 200 000) — the number of problems in the problemset and the number of guesses about the current coordinator's mood.

The second line of input contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the qualities of the problems in order of increasing difficulty.

The third line of input contains m integers b1, b2, ..., bm (0 ≤ bi ≤ 1015) — the guesses of the current coordinator's mood q.

Output

Print m lines, in i-th line print single integer — the answer to the problem with q = bi.

Example
input
6 3
8 -5 -4 1 -7 4
0 7 3
output
2
0
1
分析:dp,考虑从后往前转移;
   dp[i][j]表示从后往前i个数去掉了j个数时所需的最小初始b值;
      dp1[i]表示若去掉i个数,则所需最小的初始b值;
   转移方程为:
   dp[i][j]=min(dp[i][j],max(0LL,dp[i-1][j]-a[i]));//当前数保留;
   dp[i][j+1]=min(dp[i][j+1],dp[i-1][j]);//当前数删除;
   最后二分处理询问;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fLL
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn],ans;
ll dp[maxn][maxn],dp1[maxn];
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]);
memset(dp,llinf,sizeof dp);
dp[][]=0LL;
for(i=n;i>=;i--)
{
for(j=;j<n-i+;j++)
{
dp[n-i+][j]=min(dp[n-i+][j],max(0LL,dp[n-i][j]-a[i]));
dp[n-i+][j+]=min(dp[n-i+][j+],dp[n-i][j]);
}
}
dp1[n]=;
for(i=n-;i>=;i--)dp1[i]=max(dp1[i+],dp[n][i]);
rep(i,,m)
{
ll op;
scanf("%lld",&op);
ans=lower_bound(dp1,dp1+n+,op,greater<ll>())-dp1;
printf("%d\n",ans);
}
//system("Pause");
return ;
}

Polycarp's problems的更多相关文章

  1. codeforces 727F. Polycarp's problems

    题目链接:http://codeforces.com/contest/727/problem/F 题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该 ...

  2. Codeforces 727 F. Polycarp's problems

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

  3. CF727F [Polycarp's problems] & [EX_Polycarp's problems]

    原题题意 给出长度为n的有序数组,m次询问,每次给出一个正整数x.你要删除数组中最少的元素,使得数组中的前缀和+x都为非负整数.允许离线,n≤750,m≤200,000. 原题思路 首先注意到,x能成 ...

  4. CF 1006B Polycarp's Practice【贪心】

    Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a ...

  5. Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译

    本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...

  6. MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems

          早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...

  7. Problems about trees

    Problems (1) 给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)再回到起点的最短路程. 答案是显然的:边权之和的两倍. (2)给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)的最 ...

  8. cf723c Polycarp at the Radio

    Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be re ...

  9. Problems with MMM for mysql(译文)

    Problems with mmm for mysql posted in MySQL by shlomi 原文:http://code.openark.org/blog/mysql/problems ...

随机推荐

  1. windows网络编程(1)同步套接字

    1.socket是应用程序与网络驱动程序的桥梁,在应用程序中创建socket,将数据交付给socket即完成数据传输,剩下的任务由socket和网络驱动程序完成: 2.套接字类型:SOCK_STREA ...

  2. HTTP Status 400 - Required request part 'file' is not present

    今天使用Spring MVC做一个文件上传的功能,在提交表单的时候出现了如下错误:

  3. 实现类似QQ自拍头像的功能(demo源码)

    在很多软件系统中,都允许用户设置自己的头像,甚至可以直接使用摄像头照相作为自己的头像,就像QQ的自拍头像功能一样. 这种功能是如何实现的了?最直接的,我们可以使用Windows提供的VFW技术或Dir ...

  4. .Net Core 中使用Session

    1.在 Startup 中 ConfigureServices 添加Session public void ConfigureServices(IServiceCollection services) ...

  5. 新浪 股票 API

    新浪期货数据接口 [例子]http://hq.sinajs.cn/list=M0豆粕连续 M0 返回值如下:var hq_str_M0="豆粕连续,145958,3170,3190,3145 ...

  6. 关于MTK平台CC相关的Log查询

    关于MTK平台CC相关的Log查询 在外场问题中,经常会出现通话相关的故障.这里简单总结一下通话相关log的分析点: 主叫方:主叫方,是指主动发起通话的一方. 初步定位问题, 用户发起通话时,AP端的 ...

  7. ajax传递的数据类型json传递

    $(".del_goods").click(function(){ //删除选中的商品 var clear_data = [];//数组 $("input[name='c ...

  8. 对SNS网站现状和未来的一些想法——以我对人人网的体验为例

    现在对人人网越来越没有兴趣了,上面的照片.状态也越来越少了,反而是朋友圈里大家比较活跃. 我觉得在网上发内容的,至少是希望得到大家关注的,可是为什么人人越来越被大家嫌弃了呢? 人人上的消息越来越被淹没 ...

  9. sql语句的group by与having子句

    准备数据: DROP TABLE IF EXISTS `t_player`; CREATE TABLE `t_player` ( `player_id` int(11) NOT NULL AUTO_I ...

  10. 分析函数 over用法 之row_number() runk_number

    分析函数用于计算基于组的某种聚合值,每个组返回多个行,而聚合函数每个组只返回一个行 表: create table TB_SCORE ( id NUMBER(10), class VARCHAR2(1 ...