A
没啥好说的
int n, k, A[N], x;
void solve()
{
cin >> n >> k >> x;
for (int i = 1; i <= n; i++)
cin >> A[i];
for (int i = 1; i <= k; i++)
{
cout << A[i] << " ";
}
cout << x << " ";
for (int i = k + 1; i <= n; i++)
{
cout << A[i] << " ";
}
}
B
没开出来,阴间
int a, b, c, d, e, f;
int g, h, i, j, k, l;
bool check(int a, int b, int c, int d)
{
return max(a, c) < min(b, d);
}
void solve()
{
cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j >> k >> l;
if (check(a, d, g, j) && check(b, e, h, k) && check(c, f, i, l))
{
cout << "Yes";
}
else
cout << "No";
}
C
排序+双指针
LL n, k, A[N];
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> A[i];
sort(A + 1, A + n + 1);
LL ans = 1e18;
for (int i = 1, j = n - k; i <= k+1; i++,j++)
{
// cout << A[i] << " " << A[j] << endl;
ans = min(ans, A[j] - A[i]);
}
cout << ans;
}
D
和八数码一样,bfs+map剪枝
int n;
string s, t;
struct w
{
string a;
int b;
};
void solve()
{
cin >> n;
cin >> s >> t;
map<string, int> D;
D[s] = 1;
string a = "--";
s = s + a;
queue<w> DD;
DD.push({s, 0});
while (!DD.empty())
{
w f = DD.front();
DD.pop();
string qq = f.a.substr(0, n);
// cout << f.a << endl;
if (qq == t)
{
cout << f.b;
return;
}
int st = 0;
for (int i = 0; i < n + 1; i++)
{
if (f.a[i] == '-')
{
st = i;
break;
}
}
for (int i = 0; i < n + 1; i++)
{
if (f.a[i] == '-' || f.a[i + 1] == '-')
continue;
string g = f.a;
g[i] = '-', g[i + 1] = '-';
// cout << g << endl;
g[st] = f.a[i], g[st + 1] = f.a[i + 1];
if (D[g])
continue;
// cout << f.a << " " << g << " " << i << " " << st << endl;
D[g] = 1;
DD.push({g, f.b + 1});
}
}
cout << "-1";
}
E
容易想到就是所有路都走俩边,可以选择一条为主路就走一遍,就是2*路长-树的直径
#define int long long
int n, u, v, ans = -1, w, ru[N];
vector<pair<int, int>> A[N];
int dfs(int x, int fa)
{
int d1 = 0, d2 = 0;
for (auto y : A[x])
{
if (y.first == fa)
continue;
int d = dfs(y.first, x) + y.second;
if (d >= d1)
{
d2 = d1, d1 = d;
}
else if (d > d2)
{
d2 = d;
}
}
ans = max(ans, d1 + d2);
return d1;
}
void solve()
{
cin >> n;
LL num = 0;
for (int i = 1; i < n; i++)
{
cin >> u >> v >> w;
A[u].push_back({v, w});
A[v].push_back({u, w});
num += 2 * w;
ru[u]++;
ru[v]++;
}
if (n == 2)
{
cout << num / 2 << endl;
return;
}
int st = 1;
while (ru[st] == 1)
st++;
int k = dfs(st, -1);
cout << num - ans;
}
F
就是暴力枚举, 三次和以上的, 二次的特殊一下
LL n, k, cnt = 0, x = 0;
map<LL, int> D;
void s(LL n)
{
for (LL i = 2; i * i * i <= n; i++)
{
LL t = i * i * i;
while (t <= n)
{
t *= i;
if (D[t])
continue;
if ((LL)sqrtl(t) * (LL)sqrtl(t) == t)
x++;
D[t] = 1, cnt++;
}
}
}
void solve()
{
cin >> n;
s(n);
cout << (LL)sqrtl(n) + cnt - x;
}