A. Creating Words
swapping the first character of a with the first character of b.
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++){
string a, b;
cin >> a >> b;
swap(a[0], b[0]);
cout << a << " " << b << endl;
}
return 0;
}
B. Maximum Multiple Sum
x+2x+3x+⋯+kx where kx≤n is maximized over all possible values of x.
#include<iostream>
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 x;
cin >> x;
int j = 2;
int res = x / j;
while((x / j) >= res){
res = x / j;
j ++;
}
cout << j - 1 << endl;
}
return 0;
}
C. Good Prefixes
array is good if there exists some element that can be represented as the sum of all other elements,the array [0] is also good,count the number of integers 𝑖 (1≤𝑖≤𝑛) such that the length 𝑖 prefix (i.e. 𝑎1,𝑎2,…,𝑎𝑖) is good.
find max if
res ++;
#include<iostream>
#include<vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
int T;
cin >> T;
for (int i = 0; i < T; i++) {
int n;
cin >> n;
int cnt = 0;
int M = 0;
long long sum = 0;
for (int j = 1; j <= n; j++) {
int x;
cin >> x;
M = max(M, x);
sum += x;
if(sum - M == M)
cnt ++;
}
cout << cnt << "\n";
}
return 0;
}
D. Manhattan Circle
Point (𝑎,𝑏) belongs to the manhattan circle centered at (ℎ,𝑘)
a =
b =
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int r1[201000];
int r2[201000];
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
for(int i = 0; i < n; i++) {
int a, b;
memset(r1, 0, sizeof(r1));
memset(r2, 0, sizeof(r2));
cin >> a >> b;
for(int j = 0; j < a; j++) {
string s;
cin >> s;
for(int k = 0; k < b; k++) {
// cout << ch ;
if(s[k] == '#'){
r1[j] ++;
r2[k] ++;
}
}
// cout << endl;
}
int ma = 0, mb = 0, rmb, rma;
for(int j = 0; j < a; j++) {
if(r1[j] > ma){
ma = r1[j];
rma = j + 1;
}
}
for(int j = 0; j < b; j++) {
if(r2[j] > mb){
mb = r2[j];
rmb = j + 1;
}
}
cout << rma << " " << rmb << endl;
}
return 0;
}
E. Secret Box
determine the maximum number of distinct locations he can choose to place his secret box S inside B
not sum of all distinct locations, but is the max number of distinct locations of B...
#include<iostream>
// #include<fstream>
using namespace std;
int main() {
int n;
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
// ofstream outputFile("res.out");
for(int i = 1; i <= n; i ++){
long long a, b, c, v;
cin >> a >> b >> c >> v;
long long res = 0;
for(long long j = 1; j <= a; j ++){
for(long long k = 1; k <= b; k ++){
long long t = v % (j * k);
if(t == 0 && v / (j * k) <= c){
res = max<long long>(res, (a-j+1)*(b-k+1)*(c- (v / (j * k)) +1));
}
}
}
cout << res << endl;
// outputFile << res << endl;
}
// outputFile.close();
return 0;
}
F. Final Boss
How many turns will you take to beat the boss?
Find k satisfy constraints
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
void solve(){
ll h, n;
cin >> h >> n ;
vector<ll> b(n + 1);
vector<ll> c(n + 1);
for(ll i = 0; i < n; i++)
cin >> b[i];
for(ll i = 0; i < n; i++)
cin >> c[i];
// find thr first val satify condition
ll l = 1;
ll r = 2000001000000;
while(l < r){
ll mid = (l + r) / 2;
ll sum = 0;
for(ll i = 0; i < n; i++)
sum += (b[i] * ((mid - 1)/ (c[i]) + 1));
if(sum >= h) r = mid;
else l = mid + 1;
}
cout << r << endl;
}
int main() {
// Read the number of test cases
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
for(int i = 0; i < T; i++)
solve();
return 0;
}
G. D-Function
For how many integers 𝑛 where 10𝑙≤𝑛<10𝑟 satisfy 𝐷(𝑘⋅𝑛)=𝑘⋅𝐷(𝑛)? Let 𝐷(𝑛) represent the sum of digits of 𝑛.
#include<iostream>
using namespace std;
#define mod 1000000007
long long Pow(long long base, int exp){
long long result = 1;
while(exp > 0){
if(exp % 2 == 1)
result = (result * base) % mod;
base = (long long )base * base % mod;
exp /= 2;
}
return result % mod;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
for(int i = 0; i < T; i ++){
int l, r, k;
cin >> l >> r >> k;
long long h = 9 / k + 1;
long long res = (Pow(h, r) - Pow(h, l) + mod) % mod;
cout << res << endl;
}
return 0;
}
H1. Maximize the Largest Component (Easy Version)
find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.
当我用cstdio的时候,TLE了, 时间复杂度(o(3 * n * m) )
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;
// 寻找连通块的最大的值
int dfs(int &x, int &y, vector<vector<int>> &st, int sign, int &n, int &m, vector<string> &grap){
vector<int> xx{-1, 1, 0, 0};
vector<int> yy{0, 0, -1, 1};
st[x][y] = sign;
int res = 1;
for(int i = 0; i < 4; i ++){
int a = x + xx[i];
int b = y + yy[i];
if(a >= 0 && a < n && b >= 0 && b < m && !st[a][b] && grap[a][b] == grap[x][y]){
st[a][b] = sign;
res += dfs(a, b, st, sign, n, m, grap);
}
}
return res;
}
int check1(int i, int m, int n, vector<vector<int>> &st, vector<int> &q, int len){
int res = 0;
vector<bool> sst(len + 2);
for(int j = 0; j < m; j ++){
if(st[i][j] == -1){
res ++;
if(i - 1 >= 0 && st[i - 1][j] != -1){
if(sst[st[i - 1][j]] == 0){
sst[st[i - 1][j]] = 1;
res += q[st[i - 1][j] - 1];
}
}
if(i + 1 < n && st[i + 1][j] != -1){
if(sst[st[i + 1][j]] == 0){
sst[st[i + 1][j]] = 1;
res += q[st[i + 1][j] - 1];
}
}
}
else{
if(sst[st[i][j]] == 0){
res += q[st[i][j] - 1];
sst[st[i][j]] = 1;
}
}
}
return res;
}
int check2(int i, int n, int m, vector<vector<int>> &st, vector<int> &q, int len){
int res = 0;
vector<bool> sst(len + 2, 0);
for(int j = 0; j < n; j ++){
if(st[j][i] == -1){
res ++;
if(i - 1 >= 0 && st[j][i - 1] != -1){
if(sst[st[j][i - 1]] == 0){
sst[st[j][i - 1]] = 1;
res += q[st[j][i - 1] - 1];
}
}
if(i + 1 < m && st[j][i + 1] != -1){
if(sst[st[j][i + 1]] == 0){
sst[st[j][i + 1]] = 1;
res += q[st[j][i + 1] - 1];
}
}
}
else{
if(sst[st[j][i]] == 0){
res += q[st[j][i] - 1];
sst[st[j][i]] = 1;
}
}
}
return res;
}
void solve(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<string> grap(n + 3);
for(int i = 0; i < n; i ++)
cin >> grap[i];
vector<int> q;
int cnt = 0;
vector<vector<int>> grapSt(n + 10, vector<int> (m + 10));
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
if(grapSt[i][j])
continue;
if(grap[i][j] == '.')
grapSt[i][j] = -1;
else{
cnt++;
int t = dfs(i, j, grapSt, cnt, n, m, grap);
q.push_back(t);
}
}
}
// for(int i = 0; i < n; i ++){
// cout << grap[i] << endl;
// }
int res = 0;
for(int i = 0;i < n; i ++)
res = max(res, check1(i, m, n, grapSt, q, cnt));
for(int i = 0;i < m; i ++)
res = max(res, check2(i, n, m, grapSt, q, cnt));
cout << res << endl;
}
int main(){
//若采取暴力 枚举每一行/列变成 # 所构成的新图的最大值
//再遍历图,找到最大的#sets, 2*10^3*10^6*10^4
//遍历图中的所有联通分量,搜索新添加的节点为'.'+和他相邻的‘#’
//避免重复添加一个块,需要另外开辟一个数组进行标记
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 0; i < n; i ++)
solve();
return 0;
}