CF727F [Polycarp's problems] & [EX_Polycarp's problems]
原题题意
给出长度为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]的更多相关文章
- 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 ...
- 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 ...
- Quality 是什么?
Quality 是什么? 通常,我们谈及 Quality(质量)时,最常见的问题就是:Quality 是什么? 有很多业界先驱和研究人员已经回答了这个问题,我在这里并不会再给出一个新的答案.在学习总结 ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- 超强语感训练文章(Provided by Rocky teacher Prince)
Content: Class1 My name is Prince Class2 Welcome to our hotel Class3 We’re not afraid of problems Cl ...
- Common Pitfalls In Machine Learning Projects
Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...
- 软件工程课程作业(三)--四则运算3(C++)
伙伴链接:http://www.cnblogs.com/haoying1994/ 一.设计思路 在此前程序拥有的功能:加减有无负数,除法有无余数以及算式可定制的功能的基础上,此次程序又添加了算式结果的 ...
- 单元测试--四则运算2程序(c++)
源代码: //2016 3.6 Cheng Qiqin //四则运算改进 #include <iostream> #include<ctime> #include<cst ...
随机推荐
- es6阮一峰读后感
不经意间看了你一眼(阮一峰的es6读后感)我自己常用的 字符串篇:ES6 为字符串添加了遍历器接口(详见<Iterator>一章),使得字符串可以被for...of循环遍历.只要有遍历器接 ...
- iPhoneX快速适配,简单到你想哭。
研究了5个小时的iPhoneX适配. 从catalog,storyboard,safearea等一系列文章中发现.如果我们想完全撑满全屏.那直接建一个storyboard就好了.但撑满全屏后,流海就是 ...
- BN多卡同步进行
为什么不进行多卡同步? BatchNorm的实现都是只考虑了single gpu.也就是说BN使用的均值和标准差是单个gpu算的,相当于缩小了mini-batch size.至于为什么这样实现,1)因 ...
- .Net Core创建Docker镜像
1..Net Core项目[Lails.Server.Demo]发布到目录下Lails.Server.Demo\bin\Release\netcoreapp2.1\publish 2.上面目录下新建文 ...
- Linux安装 centos 7 最小化 安装
我用的是vmware 15 ,我将用图片的形式带领大家安装,没有说到的地方为默认选项 最好把文件放到一个单独的文件夹中 简单密码点击两次DONE 即可,虚拟机密码不必那么复杂,工作环境请设 ...
- Vue基础进阶 之 实例方法--生命周期
在上一篇博客中我们知道生命周期的方法: 生命周期: vm.$mount:手动挂载Vue实例: vm.$destroy:销毁Vue实例,清理数据绑定,移除事件监听: vm.$nextTick:将方法中的 ...
- CSS 页面布局、后台管理示例
CSS 页面布局.后台管理示例 页面布局 1.头部菜单 2.中间内容/中间左侧菜单 3.底部内容 <div class='pg-header'> <div style='width: ...
- Docker Swarm 配置文件存储
Docker Swarm 配置文件存储 config:宿主级容器服务配置文件单独保存到docker中存储调用. 环境: 系统:Centos 7.4 x64 应用版本:Docker 18.09.0 管理 ...
- Python select实现socket并发
Python select Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句柄)何时 ...
- AJAX跨域问题以及解决思路(更新中)
跨域的三大原因(同时满足) 浏览器限制 跨域 XHR请求 解决思路: 让浏览器不做限制,指定参数,让浏览器不做校验,但该方法不太合理,它需要每个人都去做改动. 不要发出XHR请求,这样就算是跨域,浏览 ...