Codeforces Round 947 (Div. 1 + Div. 2) A - D题_哔哩哔哩_bilibili
解析都在视频里哦
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
int cnt = 0;
for (int i = 0; i < n - 1; i++)
if (a[i] > a[i + 1])
cnt++;
if (a[0] < a[n - 1])
cnt++;
if (cnt > 1)
cout << "NO\n";
else
cout << "YES\n";
}
}
#include<bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--)
{
int n, dig, cnt = 0;
cin >> n;
set<int> S;
for (int i = 0; i < n; i++)
{
cin >> dig;
S.insert(dig);
}
dig = *S.begin();
for (auto i : S)
if (i % dig != 0)
cnt++;
if (cnt > 1)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
vector<int> a(n);
for (auto &i : a)
cin >> i;
int maxNum = INT_MIN;
if (n == 2)
cout << min(a[0], a[1]) << endl;
else
{
for (int i = 0; i < n - 2; i++)
{
vector<int> temp{a[i], a[i + 1], a[i + 2]};
sort(temp.begin(), temp.end());
maxNum = max(maxNum, temp[1]);
}
cout << maxNum << endl;
}
}
}
#include<bits/stdc++.h>
using namespace std;
vector<int> adj[200005];
vector<int> parTo;
vector<int> disTo;
int maxStep = INT_MIN;
void dfs(int cur, int par, int step)
{
maxStep = max(maxStep, step);
parTo[cur] = par;
disTo[cur] = step;
for (auto &i : adj[cur])
if (i != par)
dfs(i, cur, step + 1);
}
int main()
{
int T;
cin >> T;
while (T--)
{
int n, a, b, x, y;
cin >> n >> a >> b;
parTo.assign(n + 5, 0), disTo.assign(n + 5, 0);
for (int i = 1; i <= n; i++)
adj[i].clear();
for (int i = 0; i < n - 1; i++)
{
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(a, a, 0);
int firstBlue = b, times = (disTo[b] + 1) / 2;
int ans = times;
while (times--)
firstBlue = parTo[firstBlue];
maxStep = INT_MIN;
dfs(firstBlue, firstBlue, 0);
ans += (n - 1) * 2 - maxStep;
cout << ans << endl;
}
}