题目链接https://www.luogu.com.cn/problem/P1878
首先先读题
介绍一下:静态双向单链表的一般构成
以及双链表的断链重连操作
完整代码
#include <cstdio>
#include <cmath>
#include <queue>
#include <array>
#include <bitset>
#include <iostream>
constexpr int __MAX__ { static_cast<int>( 1e5 ) * 2 };
typedef struct {//定义一对节点
int diff; //每一对的水平差距
int first;//第一个人的编号
int second;//第二个人的编号
} node;
std::priority_queue<node> Q;
//记录每个人是否出队(是否有效)
std::bitset<__MAX__ + 1> label {};
//只有label为假,即还没有出队时访问prev和next才有意义
std::array<int, __MAX__ + 1> prev {}; //动态记录每个人的前驱
std::array<int, __MAX__ + 1> next {}; //动态记录每个人的后继
int N {};
//每个人的水平
std::array<int, __MAX__ + 1> value {};
//一共有多少对
int counter {};
//组成的每一对的第一个和第二个人
std::array<int, (__MAX__ >> 1 & 0)> pfirst {};
std::array<int, (__MAX__ >> 1 & 0)> psecond {};
int main(void){
std::scanf("%d", &N);
std::string s {};
s.push_back(std::cin.peek());
char token {};
while(~std::scanf("%c", &token)) s += token;
for(int i {}; i < N; ++i) {
auto index {i + 1};
std::scanf("%d", &value[index]);
next[index] = index + 1;
prev[index] = index - 1;
if(s[index - 1] == s[index]) continue;
Q.emplace(std::abs(value[index] - value[index - 1]), i - 1, i);
}
while(!Q.empty()) {
auto [diff, first, second] = Q.top();
Q.pop();
if(! first || ! second) continue;
if(label[first] || label[second]) continue;
next[prev[first]] = next[second];
prev[next[second]] = prev[first];
pfirst[counter] = first;
psecond[counter] = second;
++ counter;
if(label[prev[first]] || label[next[second]]) continue;
if(s[prev[first]] == s[next[second]]) continue;
Q.emplace(std::abs(value[prev[first]] - value[next[second]]), prev[first], next[second]);
}
std::printf("%d\n", counter);
for(int i {}; i < counter; ++i)
std::printf("%d %d\n", pfirst[i], psecond[i]);
return 0;
}