原题题意

给出长度为n的有序数组,m次询问,每次给出一个正整数x。你要删除数组中最少的元素,使得数组中的前缀和+x都为非负整数。允许离线,n≤750,m≤200,000。


原题思路

首先注意到,x能成功通过测试当且仅当前缀和中最小的数≥x。

将询问从大到小排个序,对于一个新的询问,每次尝试从数组中删除最优的一个数,使得成功的机会更大。

何为最优?我们注意到,ai只会对后面的数造成影响。设当前前缀和最小为now,fi为前i个前缀和中最小的数,则答案会增加 max { min { now-ai , fi-1 } } (请注意后面的f囊括了now-ai顾及不到的情况)。

每次判断最小的数在哪,暴力更新,O(n3+mlogm)。


代码

 #pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll inf=;
const ll maxn=1E6+;
template<typename T> void read(T &x){
x=;char ch=getchar();int fh=;
while (ch<''||ch>''){if (ch=='-')fh=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
x=x*fh;
}
void write(ll x)
{
if(x==){putchar('');putchar('\n');return;}
if(x<){putchar('-');x=-x;}
ll a[],size=;
while(x){a[++size]=x%;x/=;}
for(int i=size;i>=;--i)putchar(a[i]+'');
putchar('\n');
}
ll min(ll x,ll y){return x<y?x:y;}
ll max(ll x,ll y){return x>y?x:y;}
ll n,m,a[maxn],tot,sum[maxn],ans[maxn],f[maxn];
struct query{ll pos,x;}Q[maxn];
bool cmp(query a,query b){return a.x>b.x;}
ll get()
{
ll ans=inf;
for(int i=;i<=n;++i)
{
sum[i]=sum[i-]+a[i];
ans=min(ans,sum[i]);
f[i]=min(f[i-],sum[i]);
}
return ans;
}
int main()
{
read(n);read(m);
for(int i=;i<=n;++i)
{
read(a[i]);
if(a[i]<)++tot;
}
for(int i=;i<=m;++i)
{
read(Q[i].x);
Q[i].pos=i;
}
sort(Q+,Q+m+,cmp);
int pos=;
for(int i=;i<=tot;++i)
{
ll g=get();
while(Q[pos].x+g>=&&pos<=m){ans[Q[pos].pos]=i-;++pos;}
if(g>=)break;
ll ans=-inf,pos=;
for(int j=;j<=n;++j)
{
if(min(g-a[j],f[j-])>ans)
{
ans=min(g-a[j],f[j-]);
pos=j;
}
}
a[pos]=;
}
ll g=get();
while(Q[pos].x+g>=&&pos<=m){ans[Q[pos].pos]=tot;++pos;}
for(int i=;i<=m;++i)write(ans[i]);
return ;
}

EX版本:

m,n≤1,000,000,强制在线。


思路

考虑从后往前贪心。我们先只考虑两种情况(0显然没必要考虑)。

第一种,所有的数均为负数。则每次答案必然会从最小的数删起,直到删的数的绝对值第一次大于等于询问。

第二种,只有一个数为正,其余均为负。两种情况:

第一种,加起来为仍为正,那么就不会删数。并且这一位不会对以后造成任何影响(既然都加上正数了,能到达这一位,以后肯定不会小于零)。

  第二种,加起来为负。那么会贪心地选择最小的负数删,直到加起来为正。换个角度,会贪心地选择最大的负数加到正数上,直到正数为负。这样,化归为第一情况。

再将负数加入大根堆,正数不要的原因同第一种。

图画画,手算算至少能够理解。

最后得到的答案要么剩下正数,要么全是负数。这些负数取反后,从大到小排序的第i位代表了取到第i种答案,要删去1~i个数。

二分查找即可。


代码

 #pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll inf=;
const ll maxn=1E6+;
ll min(ll x,ll y){return x<y?x:y;}
ll max(ll x,ll y){return x>y?x:y;}
template<typename T> void read(T &x){
x=;char ch=getchar();int fh=;
while (ch<''||ch>''){if (ch=='-')fh=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
x=x*fh;
}
void write(ll x)
{
if(x==){putchar('');putchar('\n');return;}
if(x<){putchar('-');x=-x;}
ll a[],size=;
while(x){a[++size]=x%;x/=;}
for(int i=size;i>=;--i)putchar(a[i]+'');
putchar('\n');
}
ll n,m,a[maxn],wait[maxn],size,x;
priority_queue<ll>Q;
int main()
{
read(n);read(m);
for(int i=;i<=n;++i)read(a[i]);
for(int i=n;i>=;--i)
{
while(a[i]>=&&!Q.empty())
{
a[i]+=Q.top();
Q.pop();
}
if(a[i]<)Q.push(a[i]);
}
while(!Q.empty())
{
wait[++size]=Q.top();
Q.pop();
}
for(int i=;i<=size/;++i)swap(wait[i],wait[size-i+]);
for(int i=;i<=size;++i)wait[i]+=wait[i-];
for(int i=;i<=size;++i)wait[i]=-wait[i];
while(m--)
{
read(x);
write(lower_bound(wait,wait+size+,wait[size]-x)-wait);
}
return ;
}

CF727F [Polycarp's problems] & [EX_Polycarp's problems]的更多相关文章

  1. Taxonomy of class loader problems encountered when using Jakarta Commons Logging(转)

    Acknowledgments I would like to thank Jacob Kjome for reviewing early drafts of this document. His c ...

  2. Educational Codeforces Round 42 (Rated for Div. 2) A

    A. Equator time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  3. Quality 是什么?

    Quality 是什么? 通常,我们谈及 Quality(质量)时,最常见的问题就是:Quality 是什么? 有很多业界先驱和研究人员已经回答了这个问题,我在这里并不会再给出一个新的答案.在学习总结 ...

  4. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  5. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  6. 超强语感训练文章(Provided by Rocky teacher Prince)

    Content: Class1 My name is Prince Class2 Welcome to our hotel Class3 We’re not afraid of problems Cl ...

  7. Common Pitfalls In Machine Learning Projects

    Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...

  8. 软件工程课程作业(三)--四则运算3(C++)

    伙伴链接:http://www.cnblogs.com/haoying1994/ 一.设计思路 在此前程序拥有的功能:加减有无负数,除法有无余数以及算式可定制的功能的基础上,此次程序又添加了算式结果的 ...

  9. 单元测试--四则运算2程序(c++)

    源代码: //2016 3.6 Cheng Qiqin //四则运算改进 #include <iostream> #include<ctime> #include<cst ...

随机推荐

  1. selenium.common.exceptions.WebDriverException: Message: unknown Error: cannot find Chrome binary

    使用Chrome浏览器时,经常会遇到以下报错:浏览器没有调用起来 selenium.common.exceptions.WebDriverException: Message: unknown Err ...

  2. input光标大小

    最近在做项目忘记密码页面时,input光标大小需要统一.同时也需要兼容ie8浏览器. 总结如下: IE:不管该行有没有文字,光标高度与font-size一致. FF:该行有文字时,光标高度与font- ...

  3. ajax return 的问题

    平时都是在AJAX里执行逻辑,实然想到能不能return返回数据呢? ajax 是异步请求,return拿值得时候 ajax并没有取到值,所以是undefind. 需要把ajax的请求方式改为同步 v ...

  4. too many open files问题

    linux环境下,程序运行时,出现了too many open files的错误. 通过名字就能看出来,是打开了太多的文件,超过了系统限制. ulimit -a 通过这个命令可以查看当前系统设置的最大 ...

  5. Uncertainty

    I did'nt know where i was supposed to be, until i'd actually arrived.

  6. Html Link 标签

    Html Link 标签 Link 是 HTML Head 内部标签 <html> <head> <!-- link标签:rel="shortcut icon& ...

  7. 【Python64-tkinter_label组件】

    一.label组件用于在屏幕上显示文本活图像,Label组件仅能显示单一字体的文本,单文本可以跨越多行.另外还可以为其中的个别字符加上下划线 二.如何使用label组件 使用label组件,你可以指定 ...

  8. freeswitch 显示主叫名称和主叫号码

    1.指定主叫号码 origination_caller_id_number 参数来指定显示的主叫号码 2.指定主叫名称 origination_caller_id_name 参数来指定显示的主叫名称 ...

  9. tomcat的Server.xml详解和Host的配置

    基于以下说法的领悟: 若只配appBase,不配Context 的docBase(appBase和docBase二选一就可以了),则appBase的每个文件夹里都代表一个应用,每个应用都必须放ROOT ...

  10. Mysql中 in or exists not exists not in区别 (网络整理)

    in 和or区别: 如果in和or所在列有索引或者主键的话,or和in没啥差别,执行计划和执行时间都几乎一样. 如果in和or所在列没有 索引的话,性能差别就很大了.在没有索引的情况下,随着in或者o ...