- BC20260025's blog
Code(7B期末考)
- 2024-6-24 12:21:22 @
T1
# include<iostream>
using namespace std;
string s;
int n,k;
int main(){
cin>>n>>k>>s;
if(n==1){
if(k) cout<<0;
else cout<<s;
return 0;
}
for(int i=0;i<n&&k;++i){
if(i){
if(s[i]>'0'){
s[i]='0';
k--;
}
}
else{
if(s[i]>'1'){
s[i]='1';
k--;
}
}
}
cout<<s;
return 0;
}
T3
# include<iostream>
using namespace std;
# define ll long long
const int N=3e5+5;
int n;
ll c,a[N],ans=0,sum=0,s=0;
int main(){
cin>>n>>c;
for(int i=1;i<=n;++i){
cin>>a[i];
s+=a[i];
}
if(c==1){
cout<<s;
return 0;
}
int i=0,j=0;
while(j<=n){
sum+=a[++j];
if(c>1){
if(sum<0)
while(sum<0&&i<j)
sum-=a[++i];
if(sum>ans) ans=sum;
}
if(c<1){
if(sum>0)
while(sum>0&&i<j)
sum-=a[++i];
if(sum<ans) ans=sum;
}
}
cout<<s-ans+c*ans;
return 0;
}
T4
# include<iostream>
# include<algorithm>
using namespace std;
const int N=103,K=1e5+3;
const long long mod=1e9+7;
int n;
long long dp[K][N],s[K][N],a[N],k;
int main(){
cin>>n>>k;
for(int i=1;i<=n;++i) cin>>a[i];
for(int i=0;i<=k;++i) s[i][0]=1;
for(int i=1;i<=n;++i){
for(int j=0;j<=k;++j){
if(j<a[i]+1)
dp[j][i]=s[j][i-1];
else
dp[j][i]=(s[j][i-1]-s[j-a[i]-1][i-1]+mod)%mod;
if(j==0) s[j][i]=dp[j][i];
else s[j][i]=(s[j-1][i]+dp[j][i])%mod;
}
}
cout<<dp[k][n];
return 0;
}