CF 863 div3 A-C

文章提供了三个C++程序,分别解决不同问题:1)在数字串中插入一个数以最大化结果;2)找到矩阵中特定位置的层次;3)根据条件构造新数组。每个问题都通过遍历和比较策略来解决,并给出了完整的C++代码实现。
摘要由CSDN通过智能技术生成

第一题题意在一个长度为n的数字中插入一个数m使得插入之后的数最大

首先我们可以大概思考一下如果m很大那么应该插入到越前面越好 相等的时候却不是比如

23   2   如果直接插入会变成  223 但是 232 明显更大 所以遍历一下n个数然后找到比本身这个数小的放上去即可

// 数学公式要变形
// 莫急莫急先读题
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&(-x))
#define endl "\n"
#define ios ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
#define LF(x)   fixed<<setprecision(x)// c++ 保留小数
typedef long long LL;
typedef pair<int, int> PII;
const int N=1e6+10,M=1010;
const double pai=acos(-1.0);// pai
map<int,int> q;
int t,n,m;
char a[N];
void solve()
{
    cin>>n>>m>>a+1;
    int f=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]-'0'<m&&!f) 
        {
            cout<<m;
            f=1;
        }
        cout<<a[i];
    }
    if(!f) cout<<m;
    cout<<endl;
}
int main ()
{
    ios// 不能有printf puts scanf
    cin>>t;
    while(t--)
    solve();
   
    return 0;
}

 第二题的意思是在这个矩阵中找到层次然后分开

我们对图进行变化 可以知道为对称的正方形那么就要看落在第几个位置

通过对四条边界的判断可以明显得出答案 在第几个维度

  1. LL s1=min(min(a,n-a+1),min(b,n-b+1));
  2. LL s2=min(min(x,n-x+1),min(y,n-y+1));
    // 数学公式要变形
    // 莫急莫急先读题
    #include <bits/stdc++.h>
    using namespace std;
    #define lowbit(x) (x&(-x))
    #define endl "\n"
    #define ios ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
    #define LF(x)   fixed<<setprecision(x)// c++ 保留小数
    typedef long long LL;
    typedef pair<int, int> PII;
    const int N=1e6+10,M=1010;
    const double pai=acos(-1.0);// pai
    map<int,int> q;
    int a[N],b[N];
    LL n,t;
    void solve()
    {
        LL a,b,x,y; cin>>n>>a>>b>>x>>y;
        LL s1=min(min(a,n-a+1),min(b,n-b+1));
        LL s2=min(min(x,n-x+1),min(y,n-y+1));
        LL res=abs(s2-s1);
        cout<<res<<endl;
            return ;
    }
    int main ()
    {
        ios// 不能有printf puts scanf
        cin>>t;
        while(t--)
        solve();
       
        return 0;
    }
    

    第三题是构造数组使得啊a[i]=max(b[i],b[i+1]);(b[i]表示原来的数组,a[i]表示构造后的数组)那么我们如何反推明显的可以用b[n]=a[n-1] 然后开始从后往前遍历最小值,这样倒退回去就是答案

  3. // 数学公式要变形
    // 莫急莫急先读题
    #include <bits/stdc++.h>
    using namespace std;
    #define lowbit(x) (x&(-x))
    #define endl "\n"
    #define ios ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
    #define LF(x)   fixed<<setprecision(x)// c++ 保留小数
    typedef long long LL;
    typedef pair<int, int> PII;
    const int N=1e6+10,M=1010;
    const double pai=acos(-1.0);// pai
    map<int,int> q;
    int t,n;
    int a[N],b[N];
    void solve()
    {
        cin>>n;
        for(int i=1;i<n;i++) cin>>a[i];
        b[n]=a[n-1];// 表示最后一个数
        for(int i=n-1;i>=2;i--) b[i]=min(a[i-1],a[i]);// 表示两个数中小的数
        //那么构造出来的数就是最大的数了
        b[1]=a[1];
        for(int i=1;i<=n;i++) cout<<b[i]<<" ";
        cout<<endl;
        
            return ;
    }
    int main ()
    {
        ios// 不能有printf puts scanf
        cin>>t;
        while(t--)
        solve();
       
        return 0;
    }
    

 

 

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值