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 ...
随机推荐
- Eclipse出现:An internal error occurred during: "Retrieving archetypes:". GC overhead limit exceeded的问题解决
网上说修改虚拟内存的方式,其实不太可行,最直接的方式就是删除以前的workspace,重新使用一个新的workspace.
- CSS 字体效果
text-shadow还没有出现时,大家在网页设计中阴影一般都是用photoshop做成图片,现在有了css3可以直接使用text-shadow属性来指定阴影.这个属性可以有两个作用,产生阴影和模糊主 ...
- 如何为 SpringMVC 编写单元测试:普通 Controller 测试(转)
前一篇文章我们已经知道如何配置使用了 SpringMVC 测试框架的单元测试. 现在我们就该亲身实践下如何为普通 Controller 编写单元测试了. 接下来一个很明显的问题就是: 什么是普通 Co ...
- [Android] TextView上同时显示图标和文字
需求场景 +----------------------------+ | Icon TEXT | +----------------------------+ 当然,可以使用LineLayout,包 ...
- zabbix 乱码问题
一.乱码原因 查看cpu负载,中文乱码如下 这个问题是由于zabbix的web端没有中文字库,我们最需要把中文字库加上即可 二.解决zabbix乱码方法 2.1 上传字体文件到zabbix中 找到本地 ...
- UI自动化(八)xpath
由于最新版火狐不在支持FireBug等开发工具,可以通过https://ftp.mozilla.org/pub/firefox/releases/下载49版本以下的火狐就可以增加Firebug等扩展了 ...
- rest_framework视图和组件
一.视图 1.基本视图 #基本视图 #抽取基类 from rest_framework.response import Response from rest_framework.views impor ...
- Java基础学习-Java语言概述
一.Java语言发展史 创始人:詹姆斯·高斯林(James Gosling) 公司:SUN——(Stanford University Network斯坦福大学网络公司) 1995年5月23日,Jav ...
- 【java】J2EE、J2SE和J2ME的区别
本文向大家简单介绍一下J2EE.J2SE.J2ME概念及区别,J2EE,J2SE,J2ME是java针对不同的的使用来提供不同的服务,也就是提供不同类型的类库. Java2平台包括:标准版(J2SE) ...
- JavaScript(数组、Date、正则)
数组 创建数组 // 一.自变量创建数组 // 1-通过字面量创建一个空数组 var arr1 = []; console.log(arr1) console.log(typeof arr1); // ...