- BC20260025's blog
信息学期末考试代码
- 2023-12-26 16:42:26 @
T1 最大偶数和
# include<iostream>
# include<cstdio>
using namespace std;
# define ll long long
const int N=1e6+5,inf=1e9+5;
int n;
ll a[N],sum=0,m=inf;
int main(){
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lld",a+i);
sum+=a[i];
if(a[i]%2==1&&m>a[i]) m=a[i];
}
if(sum%2==1) printf("%lld",sum-m);
else printf("%lld",sum);
return 0;
}
T2 分弹珠
# include<iostream>
# include<cstdio>
using namespace std;
# define ll long long
const int M=3e5+5;
ll m,n;
ll l,r,mid,ans;
ll x[M],sum=0,ma=-1;
bool check(int k){
ll s=0;
for(int i=0;i<m;++i){
if(x[i]%k==0) s+=x[i]/k;
else s+=x[i]/k+1;
}
return s<=n;
}
int main(){
scanf("%lld %lld",&n,&m);
for(int i=0;i<m;++i){
scanf("%lld",x+i);
sum+=x[i];
ma=max(ma,x[i]);
}
l=sum/n,r=ma;
while(l<=r){
mid=(r-l)/2+l;
if(check(mid)){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%lld",ans);
return 0;
}
T3 调味
# include<iostream>
# include<cstdio>
# include<cmath>
using namespace std;
# define ll long long
const ll inf=1e18+5;
int n;
ll s[20],b[20],m=inf,ans=inf,t;
void dfs(int k,ll s1,ll b1){
if(k==n){
if(ans==0) return;
if(s1==1&&b1==0) return;
t=abs(s1-b1);
if(t<ans) ans=t;
return;
}
dfs(k+1,s1*s[k],b1+b[k]);
dfs(k+1,s1,b1);
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lld %lld",s+i,b+i);
t=abs(s[i]-b[i]);
if(t<m) m=t;
}
dfs(0,1,0);
if(ans!=inf) printf("%lld",ans);
else printf("%lld",m);
return 0;
}
T4 序列问题
# include<iostream>
# include<cstdio>
# include<algorithm>
using namespace std;
# define ll long long
const int N=1e6+5;
int n;
ll a[N],sum=0;
int main(){
scanf("%d",&n);
for(int i=0;i<n;++i) scanf("%lld",a+i);
for(int i=1;i<n-1;++i){
if(a[i]>a[i-1]) sum+=a[i];
if(a[i]>a[i+1]) sum+=a[i];
}
if(a[0]>a[1]) sum+=a[0];
if(a[n-1]>a[n-2]) sum+=a[n-1];
printf("%lld",sum);
return 0;
}