蓝桥杯2023省赛刷题2

蓝桥杯2023省赛刷题

by lqx

数组分割

请添加图片描述

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e3+9;
const int p=1e9+7;
int n,T,a[N],dp[N][2];
signed main(){
    cin>>T;
    while(T--){
        int sum=0;
        cin>>n;
        for(int i=1;i<=n;i++){
          cin>>a[i];
          sum+=a[i];
        }
        if(sum%2==1)cout<<0<<'\n';
        else{
          memset(dp,0,sizeof(dp));
          dp[0][0]=1;
          for(int i=1;i<=n;i++){
              if(a[i]%2==1){//当前是个奇数
                dp[i][0]=dp[i-1][0]+dp[i-1][1];
                dp[i][1]=dp[i-1][0]+dp[i-1][1];
              }
              else{//当前是个偶数
                dp[i][0]=dp[i-1][0]+dp[i-1][0];
                dp[i][1]=dp[i-1][1]+dp[i-1][1];
              }
              dp[i][0]%=p;
              dp[i][1]%=p;
          }
          cout<<dp[n][0]<<'\n';
        }
    }
}

矩形总面积

请添加图片描述

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int x1, y1, x2, y2;
    int x3, y3, x4, y4;
    int x,y;
    scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
    int s1=(x2-x1)*(y2-y1);
    scanf("%lld%lld%lld%lld", &x3, &y3, &x4, &y4);
    int s2=(x4-x3)*(y4-y3);
    x=max(0LL,min(x2,x4)-max(x1,x3));
    y=max(0LL,min(y2,y4)-max(y1,y3));
    int s=x*y;
    cout<<s1+s2-s;   
}

买瓜

请添加图片描述

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,a[50],ans=50;
//x当前瓜的序号,s当前已选瓜的重量,s当前劈的数量
void dfs(int x,int s,int cnt){
    if(s==m){
      ans=cnt;
      return;
    }
    if(x>=n||s>m||cnt>n)return;
    dfs(x+1,s,cnt);//不选
    dfs(x+1,s+a[x]/2,cnt+1);//选+劈
    dfs(x+1,s+a[x],cnt);//选+不劈
}
signed main(){
    cin>>n>>m,m*=2;
    for(int i=0;i<n;i++)cin>>a[i],a[i]*=2;
    dfs(0,0,0);
    cout<<(ans==50?-1:ans);
}
  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值