#include <bits/stdc++.h>
#define ll long long
using namespace std;
constexpr int N = 2e5 + 5;
struct DSU { //并查集
int fa[N]; //父节点数组
int find(int x) { //查找根节点,路径压缩
return x == fa[x] ? x : fa[x] = find(fa[x]); //递归查找并路径压缩
}
void init(int l, int r) { //初始化并查集,将l到r的父节点设为自己
for (int i = l; i <= r; ++i) { //遍历区间
fa[i] = i; //每个点的父节点指向自己
}
}
bool check(int x, int y) { //判断x和y是否在同一集合
return find(x) == find(y); //比较根节点是否相同
}
void merge(int x, int y) { //合并x和y所在的集合
if (find(x) == find(y)) { //已在同一集合则直接返回
return; //无需合并
}
fa[find(y)] = find(x); //将y的根节点指向x的根节点
}
} DL, DR; //左并查集和右并查集
int T, n; //T组数据,n为序列长度
int a[N], ord[N]; //a为原数组,ord为排序后的下标
void Solve() { //每组数据的求解函数
cin >> n; //读入
for (int i = 1; i <= n; ++i) { //读入
cin >> a[i]; //读入
}
iota(ord + 1, ord + n + 1, 1); //将ord初始化为1到n
sort(ord + 1, ord + n + 1, [](int i, int j) { //按a排序
return a[i] > a[j]; //比较函数
});
DL.init(0, n + 1); //初始化左并查集,范围0到n+1
DR.init(0, n + 1); //初始化右并查集,范围0到n+1
int ans = 0, mn = n; //ans为答案,mn为当前中位数所在区间的最小值
for (int i = 1; i <= n; ++i) { //按a值从大到小遍历每个位置
for (int j = 1; j <= 2; ++j) { //每个位置需要向左右各扩展两次
int p1 = DL.find(ord[i]); //找到ord[i]在左并查集中的根节点
int p2 = DL.find(p1 - 1); //找到p1-1的根节点
if (p1) { //如果p1不为0
DL.merge(p2, p1); //合并p2和p1所在的集合
mn = min(mn, a[p1]); //更新区间最小值
}
p1 = DR.find(ord[i]); //找到ord[i]在右并查集中的根节点
p2 = DR.find(p1 + 1); //找到p1+1的根节点
if (p1 <= n) { //如果p1不超过n
DR.merge(p2, p1); //合并p2和p1所在的集合
mn = min(mn, a[p1]); //更新区间最小值
}
}
ans = max(ans, a[ord[i]] - mn); //更新答案,当前中位数减去区间最小值
}
cout << ans << '\n'; //输出
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> T; //读入
while (T--) { //多组数据测试
Solve(); //求解
}
return 0;
}
6. AT_agc050a
#include <bits/stdc++.h>
using namespace std;
int n; //页面数量
int f(int x){ //将编号映射到1~n范围内
x %= n; //取模
return x == 0 ? n : x; //若结果为0则映射为n,否则返回x
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n; //读入n
for(int i = 1; i <= n; i++){ //枚举每个页面
cout << f(i + 1) << ' ' << f(i + 2) << '\n'; //输出第i个页面连向的两个页面
}
return 0;
}
7.P12336
#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
int a; //输入的正整数a
signed main() {
cin >> a; //读入
if(a == 1) { //特判a = 1的情况
cout << "4 28 40"; //输出a = 1解
return 0;
}
int b = 0, c, d; //b,c,d为待构造的三个数
while((1 << b) <= a) b++; //找到最小的b使得2^b > a
b = 1 << b; //b = 2^k,其中k是满足2^k > a的最小整数
c = b ^ (a ^ 1); //根据构造公式计算c
d = (a * a + b * b + c * c - 1) / 2; //根据等式计算d
if((int)d >= (int)(1ull << 63) || c >= d || b >= c || a >= b) //检查是否满足大小关系及溢出
cout << -1; //不满足则无解
else
cout << b << " " << c << " " << d; //输出
return 0;
}