A. X Axis
Let 𝑓(𝑎) be the total distance from the given points to the point a𝑎. Find the smallest value of 𝑓(𝑎).
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0; i < n; i++){
int a, b, c;
cin >> a >> b >> c;
int l = min(min(a, b), c);
int r = max(max(a, b), c);
if(l == r)
cout << "0" << endl;
else
cout << abs(r - l) << endl;
}
return 0;
}
B. Matrix Stabilization
If there are multiple such cells, choose the cell with the smallest value of 𝑖, and if there are still multiple cells, choose the one with the smallest value of 𝑗.
#include<bits/stdc++.h>
using namespace std;
void solve(){
vector<vector<int>> a(110, vector<int>(110, 0));
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
cin >> a[i][j];
vector<int> xx = {1, -1, 0, 0};
vector<int> yy = {0, 0, -1, 1};
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
int h = 0;
for(int k = 0; k < 4; k ++){
int x = i + xx[k];
int y = j + yy[k];
h = max(a[x][y], h);
}
if(a[i][j] > h)
a[i][j] = h;
}
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
cout << a[i][j] << " ";
}
cout << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=0;i<n;i++)
solve();
return 0;
}
C. Update Queries
Find the lexicographically smallest string 𝑠 that can be obtained after 𝑚 update operations, if you can rearrange the indices in the array 𝑖𝑛𝑑 and the letters in the string 𝑐 as you like.
重排字符串和idx
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n, m;
cin >> n >> m;
string a, s;
cin >> a;
unordered_map<int, int> idx;
for(int i = 0; i < m; i ++){
int x;
cin >> x;
x --;
idx[x] ++;
}
cin >> s;
vector<int> ss;
for(int i = 0; i < m; i ++){
int t = s[i] - 'a';
ss.push_back(t);
}
sort(ss.begin(), ss.end());
vector<pair<int, int>> sorted_map(idx.begin(), idx.end());
sort(sorted_map.begin(), sorted_map.end(),
[](const auto& left, const auto & right){
return left.first < right.first;
}) ;
int k = 0;
for(auto t : sorted_map){
a[t.first] = ss[k] + 'a';
k ++;
}
cout << a << endl;
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
for(int i = 0; i < n; i++)
solve();
return 0;
}
D. Mathematical Problem
You must insert exactly n−2symbols + (addition) or ×(multiplication) into this string to form a valid arithmetic expression.
have n - 1 kinds od two numbers prossibles, We can‘t determine a kind of number so that we should cite all prossible kinds and have some special conditions need to judge.
if n == 2 return a[0] * 10 + a[1];
if a[0] == 0 || a[n - 1] == 0 || (n > 3 ans have a number is zero) return 0;
if we meet 1, res += 0, but we must confrim the sequence have a number if not equal 1, if not res euqal 1
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void solve(){
int n;
cin >> n;
string num;
cin >> num;
vector<int> a(n + 2, 0);
vector<int> f(n + 10);
vector<int> s(n + 10);
for(int i = 0; i < n; i ++){
a[i] = num[i] - '0';
if(a[i] == 0 && n > 3){
cout << 0 << endl;
return ;
}
}
if(n == 2){
cout << a[0] * 10 + a[1] << endl;
return ;
}
if(a[0] == 0 || a[n - 1] == 0 ){
cout << 0 << endl;
return ;
}
for(int i = 0; i < n - 1; i ++)
f[i] = a[i] * 10 + a[i + 1];
int resend = 1000000000;
for(int i = 0; i < n - 1; i ++){
int res = 0;
if(f[i] != 1)
res = f[i];
int t = 0;
for(int j = 0; j < n; j ++){
if(j == i || j == i + 1) continue;
if(a[j] == 1) continue;
res += a[j];
t ++;
}
if(t == 0 && res == 0)
res = 1;
resend = min(resend, res);
}
cout << resend << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0; i < n; i ++)
solve();
return 0;
}