8.3讲了前缀和进阶-4
题目
Three Parts of the Array
题意
给定一个长为的序列,要求将切成连续的三段,并且第一段的和等于最后一段的和,求第一段的最大值。
代码
前缀和:数组(题目中的)前个数之和
遍历所有可能的第一部分结束位置,在数组中查找是否存在一个位置,使得sum(第三部分的和等于第一部分的和)。
再_返回第一个大于等于的位置。检查是否满足条件,如果满足,更新最大值。
#include<iostream>
using namespace std;
#define int long long
void read(int &x){
bool neg=false;x=0;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=getchar();}
if(neg){while(ch>='0'&&ch<='9'){x=x*10+('0'-ch);ch=getchar();}}
else{while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();}}
}
void write(int x){
bool neg=false;
if(x<0){neg=true;putchar('-');}
static int sta[40];int top=0;
do{sta[top++]=x%10;x/=10;}while(x);
if(neg)while(top)putchar('0'-sta[--top]);
else while(top)putchar('0'+sta[--top]);
}
const int N=2e5+5;
int a[N],sum[N],ans,n;
signed main(){
read(n);
for(int i=1,x;i<=n;++i){
read(x);
sum[i]=sum[i-1]+x;
}
for(int i=1;i<=n;++i){
if((lower_bound(sum+1,sum+1+n,sum[n]-sum[i])-sum)>=i&&sum[(lower_bound(sum+1,sum+1+n,sum[n]-sum[i])-sum)]==sum[n]-sum[i])ans=sum[i];
}
write(ans);
return 0;
}Glider
题意
滑翔,可以从任意点开始,从高度h开始下降,每秒向前移动一个单位,下降一个单位。
有个区间,每个区间有左右端点,,经过至时不会下降。保证区间不相交。
给你,和每个区间,让你求能滑翔的最远距离。
代码
在输入时,同时计算区段间的下降时间总和和上升时间总和
遍历所有可能的起始区段,使用二分查找找到最大的pos,使得,更新最大飞行距离
最后输出:上升气流区段内的飞行距离 + 自由下降的飞行距离=总长度
#include<iostream>
using namespace std;
void read(int &x){
bool neg=false;x=0;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=getchar();}
if(neg){while(ch>='0'&&ch<='9'){x=x*10+('0'-ch);ch=getchar();}}
else{while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();}}
}
void write(int x){
bool neg=false;
if(x<0){neg=true;putchar('-');}
static int sta[40];int top=0;
do{sta[top++]=x%10;x/=10;}while(x);
if(neg)while(top)putchar('0'-sta[--top]);
else while(top)putchar('0'+sta[--top]);
}
const int N=2e5+5;
int n,h,s[N],e[N],sum[N],ans[N];
int main(){
read(n);read(h);
for(int i=1;i<=n;++i){
read(s[i]);read(e[i]);
if(i>1)sum[i]=sum[i-1]+(s[i]-e[i-1]);
ans[i]=ans[i-1]+(e[i]-s[i]);
}
int res=0;
for(int i=1;i<=n;++i){
int pos=lower_bound(sum+1+i,sum+1+n,h+sum[i])-sum;
--pos;
res=max(res,ans[pos]-ans[i-1]);
}
write(res+h);
return 0;
}Romantic Glasses
题意
有组数据,对于每组数据,有一个数,接下来个数 ,,……, 。判断有没有一个到的区间里的数下标是奇数的和与下标为偶数的和相等,相等输出 YES,否则输出 NO
代码
定义两个前缀和数组:
:前i个数中所有奇数下标元素的和
:前i个数中所有偶数下标元素的和
枚举终点,计算奇数位和与偶数位和的差值,如果同一个差值出现超过次,说明存在满足条件的区间,输出存在,标记已找到然后跳出循环。如果没找到,输出不存在。
记得清空!
#include<iostream>
#include<map>
using namespace std;
#define int long long
void read(int &x){
bool neg=false;x=0;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-')neg=true;ch=getchar();}
if(neg){while(ch>='0'&&ch<='9'){x=x*10+('0'-ch);ch=getchar();}}
else{while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();}}
}
const int N=2e5+5;
int t,n,a[N],sum1[N],sum2[N];
map<long long,int> cnt;
signed main(){
ios::sync_with_stdio(false);cout.tie(0);
read(t);
while(t--){
read(n);
for(int i=1;i<=n;++i){
read(a[i]);
if(i%2==1){
sum1[i]=sum1[i-1]+a[i];
sum2[i]=sum2[i-1];
}else{
sum1[i]=sum1[i-1];
sum2[i]=sum2[i-1]+a[i];
}
}
bool f=1;
for(int r=0;r<=n;++r){
++cnt[sum1[r]-sum2[r]];
if(cnt[sum1[r]-sum2[r]]>1){
cout<<"YES\n";
f=0;break;
}
}
if(f==1)cout<<"NO\n";
cnt.clear();
}
return 0;
}Eating Queries
题意
有颗糖,吃第颗糖可以得到的糖分。
有次询问,每次询问最少吃多少颗糖可以得到不小于的糖分,无解输出。
注意:在每次询问中,不能多次吃同一颗糖;每次询问互相独立,即不同询问可以吃同一颗糖。
代码
先排序,排序后计算前缀和数组,
前缀和数组:前i个最大的糖果的总糖分值
处理询问
如果所有糖果总和都不够,输出-1
否则,使用_找到第一个的前缀和位置,减去得到需要的糖果数
#include<bits/stdc++.h>
using namespace std;
bool cmp(int x,int y){
return x>y;
}
const int N=2e5+5;
int t,n,q,a[N],sum[N],x;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
cin>>n>>q;
for(int i=1;i<=n;++i)cin>>a[i];
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;++i)sum[i]=sum[i-1]+a[i];
while(q--){
cin>>x;
if(sum[n]<x)cout<<"-1\n";
else cout<<lower_bound(sum+1,sum+1+n,x)-sum<<"\n";
}
}
return 0;
}Find the Different Ones!
题意
你需要在~范围内找到一对不同的元素,或者报告这样的元素对不存在。
代码
:从第一个元素到第个元素中,与前一个元素不同的次数。
处理查询
对于每个查询,如果,说明从到的所有元素都相同(因为没有新增的“不同”情况)
否则,_会找到第一个大于的位置,这个位置对应的就是区间内第一个与不同的元素的下标。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int t,n,a[N],q,l,r,sum[N];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
cin>>n;
sum[0]=0;
for(int i=1;i<=n;++i){
cin>>a[i];
sum[i]=sum[i-1]+(a[i]!=a[i-1]);
}
cin>>q;
while(q--){
cin>>l>>r;
if(sum[l]==sum[r])cout<<"-1 -1\n";
else cout<<l<<" "<<upper_bound(sum+l,sum+r+1,sum[l])-sum<<"\n";
}
cout<<"\n";
}
return 0;
}Zero-Sum Prefixes
题意
给定一个长为的数列,你可以将其中的每个分别改成任意整数。求出你最多能让多少个满足……。
代码
:数组前个数的累加和。我们需要找到所有满足的
我们可以考虑逆向处理:
逆向处理:从右向左扫描数组,这样可以:
动态统计每个位置的前缀和出现频率
在遇到时”分段处理”(因为0可以被修改来切断前面前缀和的影响)
关键操作:
用统计每个前缀和出现的次数
记录当前段内出现最频繁的前缀和次数
遇到时,将加入,然后重置统计(因为可以修改这个来创建新的前缀和平衡点)
最后检查整个数组的前缀和是否为0
#include<iostream>
#include<map>
using namespace std;
#define int long long
const int N=2e5+5;
int t,n,a[N],sum[N],cnt,ans;
map<int,int> mp;
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
cin>>n;
mp.clear();cnt=0;ans=0;
for(int i=1;i<=n;++i)cin>>a[i],sum[i]=sum[i-1]+a[i];
for(int i=n;i>=1;--i){
cnt=max(cnt,++mp[sum[i]]);
if(a[i]==0){
ans+=cnt;
cnt=0;
mp.clear();
}
}
ans+=mp[0];
cout<<ans<<"\n";
}
return 0;
}