2024-07-10-ABC360
A - Insert
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, x;
cin >> n >> k >> x;
vector<int> a(n);
for (auto& i : a)
cin >> i;
a.insert(a.begin() + k, x);
for (auto i : a)
cout << i << ' ';
cout << '\n';
return 0;
}
B - Intersection of Cuboids
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b, c, d, e, f;
int g, h, i, j, k, l;
cin >> a >> b >> c >> d >> e >> f;
cin >> g >> h >> i >> j >> k >> l;
auto overlap = [](int l1, int r1, int l2, int r2) {
return max(l1, l2) < min(r1, r2);
};
bool x = overlap(a, d, g, j);
bool y = overlap(b, e, h, k);
bool z = overlap(c, f, i, l);
if (x && y && z) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
C - Make Them Narrow
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto& x : a)
cin >> x;
sort(a.begin(), a.end());
int ans = 1e9 + 7;
for (int i = 0; i <= k; i++) {
ans = min(ans, a[n - (k - i) - 1] - a[i]);
}
cout << ans << '\n';
return 0;
}
D - Go Stone Puzzle
BFS
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s, t;
cin >> n >> s >> t;
vector<int> st(n + 2, 2), ed(n + 2, 2);
for (int i = 0; i < n; i++) {
st[i] = s[i] == 'B';
ed[i] = t[i] == 'B';
}
map<vector<int>, int> cnt;
queue<vector<int>> q;
q.push(st);
cnt[st] = 0;
while (!q.empty()) {
auto u = q.front();
q.pop();
int d = cnt[u];
if (u == ed) {
break;
}
int empty = find(u.begin(), u.end(), 2) - u.begin();
for (int i = 0; i < n + 1; ++i) {
auto v = u;
if (v[i] != 2 && v[i + 1] != 2) {
swap(v[i], v[empty]);
swap(v[i + 1], v[empty + 1]);
if (!cnt.count(v)) {
cnt[v] = d + 1;
q.push(v);
}
}
}
}
if (!cnt.count(ed)) {
cnt[ed] = -1;
}
cout << cnt[ed] << '\n';
return 0;
}
E - Tree and Hamilton Path 2
#include <bits/stdc++.h>
#define LL long long
using namespace std;
vector<array<int, 2>> edge[200005];
vector<LL> dis;
void dfs(int u, int fa)
{
for (auto [v, w] : edge[u]) {
if (v == fa)
continue;
dis[v] = dis[u] + w;
dfs(v, u);
}
}
int main() {
int n;
cin >> n;
dis.resize(n);
LL sum = 0;
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
edge[u].push_back({v, w});
edge[v].push_back({u, w});
sum += w;
}
dfs(0, 0);
int l = max_element(dis.begin(), dis.end()) - dis.begin();
dis.assign(n, 0);
dfs(l, l);
LL max_dis = *max_element(dis.begin(), dis.end());
cout << sum * 2 - max_dis << endl;
return 0;
}
F - x = a^b
#include<bits/stdc++.h>
using namespace std;
long long safe_pow(long long a,long long b){
long long res=1;
for(long long i=0;i<b;i++){
double dres=res;
dres*=a;
if(dres>2e18){return 2e18;}
res*=a;
}
return res;
}
long long sqrt_floor(long long x){
long long l=0,r=2e9;
while(l<=r){
long long t=(l+r)/2;
if(t*t>x){r=t-1;}
else{l=t+1;}
}
return r;
}
int main(){
long long n;
cin >> n;
set<long long> st;
for(long long b=3;b<60;b++){
for(long long a=2;;a++){
long long x=safe_pow(a,b);
if(x>n){break;}
long long s=sqrt_floor(x);
if(s*s!=x){st.insert(x);}
}
}
long long res=st.size();
res+=sqrt_floor(n);
cout << res << "\n";
return 0;
}