A. X Axis
思路:
1~10暴力枚举一下所有可能
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, t, h, k;
ll a, b, c;
ll ans, num, sum, cnt;
ll temp[N], f1[N], f2[N];
bool flag, vis[N];
int main()
{
cin >> t;
while (t--) {
cin >> a >> b >> c;
sum = a + b + c;
ans = 1e9;
for (int i = 0; i <= 10; i++) {
ans = min(ans,abs(a-i)+abs(b-i)+abs(c-i));
}
cout << ans << endl;
}
}
B. Matrix Stabilization
思路:
若一个数比周围四个数都大,那么最终会变成四个数当中最大的哪一个,我们只要将四周的数的最大值找到,然后把这个数变成相邻的最大值.
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, t, h, k;
ll a, b, c;
ll ans, num, sum1,sum,sum2, cnt;
ll temp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
int main()
{
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
}
}
for (int i = 1; i <= n; i++) {
mp[i][0] = 0, mp[i][m + 1] = 0;
}
for (int j = 1; j <= m; j++) {
mp[0][j] = 0, mp[n + 1][j] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
sum1 = max(mp[i - 1][j], mp[i + 1][j]);
sum2 = max(mp[i][j + 1], mp[i][j - 1]);
sum = max(sum1, sum2);
if (mp[i][j] > sum) {
mp[i][j] = sum;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << mp[i][j] << " ";
}
cout << endl;
}
}
return 0;
}
C. Update Queries
思路:
对字符串c以及ind数组进行排序,通过贪心可以知道,我们需要按照索引从小到大的修改字符串,同时一个位置只会有一个字母与之对应,因此只需要同时按照字符串c从小到大修改即可.
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, t, h, k;
ll a, b, c;
ll ans, num, sum1,sum,sum2, cnt;
ll temp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s, ss;
int main()
{
cin >> t;
while (t--) {
map<ll, ll>mp;
cin >> n >> m;
cin >> s;
for (int i = 1; i <= m; i++) {
cin >> temp[i];
mp[temp[i]] = 1;
}
cin >> ss;
s = " " + s;
ss = " " + ss;
sort(ss.begin(), ss.end());
//cout << ss << endl;
num = 0;
for (int i = 1; i <= n; i++) {
if (mp[i]) {
s[i] = ss[++num];
}
}
for (int i = 1; i <= n; i++)
cout << s[i];
cout << endl;
}
return 0;
}
D. Mathematical Problem
思路:
一定有一个两位数,因此我们可以枚举这个两位数,然后取最小值。接下来考虑如何取最小值:若存在数字0,那么可以通过都用乘法来将最终结果变成0,此外,若存在数字1,可以通过乘法将1消掉。除此之外的数,都是加法更加小。
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, t, h, k;
ll a, b, c;
ll ans, num, sum1,sum,sum2, cnt;
ll temp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s, ss;
void solve()
{
for (int i = 1; i < n; i++) {
vector<ll>v;
sum = 0;
for (int j = 0; j < n; j++) {
if (j + 1 == i) {
v.push_back(temp[j] * 10 + temp[j + 1]);
j++;
}
else {
v.push_back(temp[j]);
}
}
for (auto kk : v) {
if (kk == 0) {
cout << 0 << endl;
return;
}
if (kk == 1)
continue;
else
sum += kk;
}
if (sum == 0)
sum++;
ans = min(ans, sum);
}
cout << ans << endl;
}
int main()
{
cin >> t;
while (t--) {
cin >> n;
cin >> s;
ans = 1e9;
for (int i = 0; i < n; i++)
temp[i] = s[i] - '0';
solve();
}
return 0;
}