20260203 课后总结
zzyycc2013 · · 个人记录
T1 DBMB and the Array
思路
第一点这个
- 总和大于
s ,因为x \ge 1 所以凑不出s -
- 其他情况也不行
Code:
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int a,S,s,x,n,T;
int main(){
cin>>T;
while(T--){
cin>>n>>s>>x;
S=0;
for(int i=1;i<=n;i++)cin>>a,S+=a;
S=s-S;
if(S<0)cout<<"No\n";
else if(S%x!=0)cout<<"nO\n";
else if(S%x==0)cout<<"yEs\n";
}
return 0;
}
T3 Reverse a Permutation
思路
贪心,要字典序尽可能大就把大的数换到前面,如果最大的已经在第一位了,那就把第二大的换到第二位,如果第二也在那按同样的方法改变第三位
Code:
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int n,a[200010],T;
int main(){
cin>>T;
while(T--){
cin>>n;
int p,x=-1,pp;
bool f=1;
for(int i=1,j=n;i<=n;i++,j--){
cin>>a[i];
if(a[i]!=j&&f)p=i,x=j,f=0;
if(a[i]==x&&!f)pp=i;
}
if(x==-1){
for(int i=1;i<=n;i++)cout<<a[i]<<" ";
}else{
for(int i=1;i<p;i++)cout<<a[i]<<" ";
for(int i=pp;i>=p;i--)cout<<a[i]<<" ";
for(int i=pp+1;i<=n;i++)cout<<a[i]<<" ";
}
cout<<"\n";
}
return 0;
}
T4 Replace and Sum
思路
贪心,让每一位尽可能的大这样区间和就会最大
Code:
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int T,n,q,a[200010],b[200010],s[200010];
int main(){
cin>>T;
while(T--){
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
a[n]=max(a[n],b[n]);
for(int i=n-1;i>=1;i--)a[i]=max({a[i+1],b[i],a[i]});
for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
while(q--){
int l,r;
cin>>l>>r;
cout<<s[r]-s[l-1]<<" ";
}
cout<<"\n";
}
return 0;
}