第一题题意在一个长度为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;
}
第二题的意思是在这个矩阵中找到层次然后分开
我们对图进行变化 可以知道为对称的正方形那么就要看落在第几个位置
通过对四条边界的判断可以明显得出答案 在第几个维度
- 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));
// 数学公式要变形 // 莫急莫急先读题 #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] 然后开始从后往前遍历最小值,这样倒退回去就是答案
-
// 数学公式要变形 // 莫急莫急先读题 #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; }