2023csp山东小学组复赛试题及其讲解 待完善

1赚钱

题目地址:https://www.luogu.com.cn/problem/U377638

 1 //U377638 赚钱
 2 #include <iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 int n,a[200010];
 8 int main() {
 9     cin>>n;
10     for(int i=1; i<=n; i++) cin>>a[i];
11     int mmin=a[1];//记录到目前为止能够买进的最小值
12     int ans=-2e9-1;
13     for(int i=1; i<n; i++) {
14         mmin=min(mmin,a[i]);
15         ans=max(a[i+1]-mmin,ans);
16     }
17     cout<<ans;
18     return 0;
19 }

 2回文字符串

题目地址:https://www.luogu.com.cn/problem/U377648

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 string s;
 7 int main() {
 8     cin>>s;
 9     int ls=s.length();
10     int ans=0;
11     int l=0,r=ls-1;
12     int f=0;
13     while(l<r) {
14         if(s[l]!=s[r]) {//考虑好几个字母相等的情况
15             string a="",b="";
16             while(l<r) {
17                 a=a+s[l];
18                 b=s[r]+b;
19                 l++;
20                 r--;
21                 if(a==b) {
22                     ans+=2;
23                     break;
24                 }
25             }
26             if(a==b) f=0;//ab ab
27             else f=1;//a code a
28         } else { //目前相等,直接去掉
29             ans+=2;
30             l++;
31             r--;
32             if(l==r) f=1; //a h a
33             else f=0;//a a
34         }
35     }
36     ans+=f;
37     if(ans<=1) cout<<"NO";
38     else cout<<"YES"<<endl<<ans;
39     return 0;
40 }

 3克隆机

题目地址:https://www.luogu.com.cn/problem/U377740

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 long long n,k;
 7 int main() {
 8     cin>>k>>n;
 9     if(n<=k) {
10         cout<<char('A'+n-1);
11         return 0;
12     }
13     long long t=1,tol=k;
14     while(tol<=n) {
15         t=t*2;
16         tol+=k*t;
17     }
18     tol-=t*k;//完成重复t次后有多少个种子
19 
20     long long ans=(n-tol)/t;
21     if((n-tol)%t!=0) ans++;
22     //cout<<t<<" "<<tol<<" "<<ans<<endl;
23     cout<<char('A'+ans-1);
24     return 0;
25 }

4代价

题目地址:https://www.luogu.com.cn/problem/U377774

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 int n,a,b;
 8 long long w[100010];
 9 long long sum[100010];
10 int main() {
11     cin>>n>>a>>b;
12     for(int i=1; i<=n; i++) 
13         cin>>w[i];
14     sort(w+1,w+1+n);
15     for(int i=1; i<=n; i++) {
16         sum[i]=sum[i-1]+w[i];
17     }
18     long long ans=(sum[n]-sum[1]-(n-1)*w[1])*b;
19     for(int i=1; i<=n; i++) {
20         //如果选定wi为最终规格
21         long long t=((i-1)*w[i]-sum[i-1])*a+(sum[n]-sum[i]-(n-i)*w[i])*b;
22         ans=min(ans,t);
23     }
24     cout<<ans<<endl;
25     return 0;
26 }

 

posted @ 2023-11-01 15:03  关于42号星球  阅读(1057)  评论(1编辑  收藏  举报