A. Insert Digit
分析
插入一位数使之尽可能的大,那么插入后所有可能的数的数位相等,就只需要考虑高位的数尽可能大,将插入的数与要插入的数的每位数相比较,大于就在这个数位前插入,如果到最后都没有插入,就将d插入到最后。
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N = 100010,mod = 1e6+7;
typedef long long ll;
typedef pair<int,int> pii;
int n,m,k;
void solve()
{
cin >> n >> m;
string s;
cin >> s;
int f = 0;
for(int i = 0;i < n; i++)
{
if(s[i] - '0' < m && !f)
{
f = 1;
cout << m << s[i];
}
else cout << s[i];
}
if(!f) cout << m;
cout << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
T = 1;
cin >> T;
while(T--) solve();
return 0;
}
B. Conveyor Belts
分析
假设每个点移动到最外层四条边距离最小值为最短距离,因为一个点(x,y)可以在指定的正方形中循环移动,即最短距离min{x,n-x+1,y,n-y+1},两个点的最少操作即两个点的最短距离之差。
代码实现
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 100010,mod = 1e9+7;
int n,m;
void solve()
{
int x1, y1, x2, y2;
cin >> n >> x1 >> y1 >> x2 >> y2;
int a, b;
a = min({x1, n - x1 + 1, y1, n - y1 + 1});
b = min({x2, n - x2 + 1, y2, n - y2 + 1});
cout << abs(a - b) << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while(tt--)
{
solve();
}
return 0;
}
C. Restore the Array
分析
构造题,由题意可知,假设,由后往前推,因为影响和的值,如果比小同时等于,那么将用给复制,保证的值不影响 ,确保的值由 ,其余情况用 复制给,因为不等于确保有 获得。
代码实现
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 200010,mod = 1e9+7;
int n,m;
int a[N],b[N];
void solve()
{
cin >> n;
for(int i = 1;i <= n; i++) a[i] = 0;
for(int i = 1;i <= n - 1; i++) cin >> b[i];
a[1] = b[1], a[n] = b[n - 1];
for(int i = n - 1;i > 1; i--)
{
if(b[i - 1] < b[i] && max(a[i],a[i + 1]) == b[i])
{
a[i] = b[i - 1];
}
else a[i] = b[i];
}
for(int i = 1;i <= n; i++)
cout << a[i] << ' ';
cout << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while(tt--)
{
solve();
}
return 0;
}
D. Umka and a Long Flight
想了好久都没怎么搞明白,只能将自己的想法先记录下来,还是思维太弱了。
分析
有题目和猜测判断一个矩形能否被分割成2个边长为1的正方形、边长为2,3...n的正方形各一个,同时官方的证明:F[0]^2+F[1]^2+...+F[n]^2=F[n]*F[n+1],F[n]*F[n+1]=F[n]*(F[n-1]*F[n])。依次将边长从大到小的正方形方块割出去,如果F[n-1] < y <= F[n]就无法割出当前为F[n]的正方形。当y>F[n],将左边的正方形割去,涂色方格的坐标变为(y-F[n], x).同理可得,y<=F[n-1]时,将右边的正方形割去,涂色方格的坐标变为(y,x),知道割到只剩下最后一个边长为1的正方形。大家可以画一画,下面给出y>F[n]的情况。
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N = 100010,mod = 1e6+7;
typedef long long ll;
typedef pair<int,int> pii;
int n,m,k;
int F[50];
bool check(int n, int x, int y)
{
if(n == 0) return true;
if(y <= F[n-1])
{
}
else if(y > F[n])
{
y -= F[n];
}
else return false;
return check(n-1, y, x);
}
void solve()
{
int n,x,y;
cin >> n >> x >> y;
cout << (check(n, x, y) ? "YES" : "NO") << '\n';
}
void init()
{
F[0] = 1,F[1] = 1;
for(int i = 2;i < 50; i++)
{
F[i] = F[i-1]+F[i-2];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
T = 1;
init();
cin >> T;
while(T--) solve();
return 0;
}
E. Living Sequence
分析
看了题解很妙的思路,将{0,1,2,3,5,6,7,8,9}当作一个字符集的九进制数,给予我们的是一个不出现4的十进制数,将k转换为9进制,大于等于4的数将之转换时加1。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 100010,mod = 1e9+7;
int n,m;
void solve()
{
ll k;
cin >> k;
vector<int> a;
while(k)
{
a.push_back(k % 9);
k /= 9;
}
reverse(a.begin(),a.end());
for(int i = 0;i < a.size(); i++)
{
if(a[i] < 4) cout << a[i];
else cout << a[i]+1;
}
cout << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while(tt--)
{
solve();
}
return 0;
}