这里是本题的官方题解,这题比较复杂,还是按照 oolimry 的题解分成 4 parts 叙述,增加了我在验题的时候不同的想法。
另外题面中有一句彩蛋:
Zookeeper is just a duck.
指的是 oolimry 原来的 id 是 Zookeeper 但是现在他是只鸭子了(
如果您想做这题的话,记住 errorgon 说的话:
It’s a waste of time.
part1
首先我们可以把题意中的队列转换成一个圆,把王位进行继承。
假设我们认为王位是顺时针旋转的,那么王位所在的人会和顺时针的下一位进行决斗。
我们将 顺时针的下一位 称作 顺次位,同理我们定义 逆次位 是 逆时针的下一位。
这是有 2 种情况:
- 王 打败了 顺次位,那么交换 王 和 顺次位。
- 王 败了,则不进行变化。
无论是哪种情况 王位 都会向 顺次位 移动。
这里放一张图方便理解,其中 先是打败了 然后输给了 :
在这样的题意下 把 王位 融入了 战斗顺序 中 方便我们进行后续处理以及性质的发现。
part2
如果一个动物 的 大于 顺次位的 那么 我们将 顺次位 标为 红色。
换句话就是 当一个位置是 红色 时 可以帮助 他的逆次位 从第一任期到第二任期过渡。
接下来我要证明的是:在 part1 所转化的题意中 红色 无法被制造出来。
我们假设 的相邻顺序中 是一个非红色位置 是红色位置 我们让 进入第一任期 由于我们竞选不能结束
因为结束了讨论这个没有意义所以假设注意到保证了
考虑 在打败了 之后 并不能成为 红色
考虑 所以 也不能成为红色
所以 红色 无法被制造出,但显然 可以失去红色。
所以在选举的过程中 整体的红色数量是单调不增的。这是这题一个重要的性质。
part3
这部分是这题比较难的地方。
我们注意到 红色位置 的消失的次数有 次,所以我们可以考虑快速计算两次消失之间的轮数。
让我们再关注一些性质:
非红色位置之间的顺序不会变化
那我们把红色点抽出来,将其他点看做不动,红色点开始进行向 逆次位 移动。
还是给出图来帮助理解:
这是好的,减少了我们处理移动的负担 考虑最多 步,每个 红色位置都会向 逆次位 移动一格。
我们将 非红色 的位置 再分成 绿色 和 蓝色 分别 标注能结束竞选 与 不能结束竞选。
蓝色的定义是: 赢了前一个 红色 但输给了后一个动物。
绿色的定义是: 赢了前一个 红色 也赢了后一个动物。
我们再考虑一下几个关键的位置:
- 红色消失当且仅当 而 可以是 绿或蓝。
- 竞选结束只可能当 而 必须是 绿色。
我们想要找到移动次数的话可以直接进行二分 从由非红色位置构成的 传送带 中找到上述两个关键位置中最早发生的进行转移。
最后我们有 步,倒着检查一遍就可以了,如果不行就输出 。
但是这里并没有考虑一些非常特殊的情况,比如不存在红色位置 或者有红色相邻 等。
这些特殊情况都可以在 步内算出结果,所以我们一开始可以暴力跑这部分。
part4
考虑复杂度 我们一共会有 次事件,而每次事件要 的时间寻找下一个。
总复杂度:
这题代码和 **** 一样,给出一份尚有可读性的代码方便调试:
#include <bits/stdc++.h>
using namespace std;
const int RED = 0, BLUE = 1, GREEN = 2;
const long long inf = 1e16;
typedef pair<long long, long long> ii;
typedef pair<long long, ii> iii;
long long n;
struct animal{ int a, b, c, id, pos, colour, redHere; };
vector<animal> belt;
vector<animal> arr;
vector<iii> s; ///strictly increasing stack
vector<ii> reds;
void insert(long long b, long long beltPos, long long pos){
while(!s.empty() && s.back().first >= b) s.pop_back();
s.push_back(iii(b,ii(beltPos, pos)));
}
long long totalMoves = 0;
void brute(){
deque<animal> q;
animal w = arr[0];
for(int i = 1;i < n;i++){
q.push_back(arr[i]);
}
long long count = 1;
for(int x = 1;;x++){
if(x > 2*n){
return;
}
if(count == 3){
cout << w.id << " " << x << "\n"; // << "\n" << (int)((clock() - start));
exit(0);
}
if(count == 1){
if(w.b > q.front().a){
animal y = q.front();
q.push_back(y);
q.pop_front();
count++;
}
else{
q.push_back(w);
w = q.front();
q.pop_front();
count = 1;
}
}
else{
if(w.c > q.front().a){
animal y = q.front();
q.push_back(y);
q.pop_front();
count++;
}
else{
q.push_back(w);
w = q.front();
q.pop_front();
count = 1;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0;i < n;i++){
int a, b, c; cin >> a >> b >> c;
assert(a > b and b < c);
arr.push_back({a,b,c,i,-1,1,-1});
}
if(arr[0].a > arr[1].a) swap(arr[0],arr[1]); ///settle first game
arr.push_back(arr[0]); arr.erase(arr.begin());
brute();
///deciding which are RED
for(int i = 1;i < n;i++){
if(arr[i-1].b > arr[i].a){
arr[i].colour = RED;
}
}
///for non-RED, decide if it's BLUE or GREEN
for(int i = 0;i < n;i++){
arr[i].pos = i;
if(arr[i].colour == RED) continue;
int nxt = i+1; if(nxt == n) nxt = 0;
if(arr[nxt].colour == RED) nxt++; if(nxt == n) nxt = 0;
if(arr[i].c > arr[nxt].a) arr[i].colour = GREEN;
else arr[i].colour = BLUE;
}
///get ready the belt
arr.push_back(arr[0]);
for(int i = 0;i < n;i++){
if(arr[i].colour != RED){
if(arr[i+1].colour == RED){
arr[i].redHere = arr[i+1].pos;
}
belt.push_back(arr[i]);
}
}
while(true){
s.clear(); reds.clear();
///Account for cyclic nature, so every element is inserted once, then later inserted again
for(int i = 0;i < (int) belt.size();i++){
animal A = belt[i];
if(A.colour == BLUE) insert(A.b, A.pos, i);
else insert(-inf, A.pos, i); ///GREEN trigger events regardless of the value of a
}
long long minMoves = inf;
for(int i = 0;i < (int) belt.size();i++){
animal A = belt[i];
if(A.colour == BLUE) insert(A.b, A.pos, i);
else insert(-inf, A.pos, i); ///GREEN trigger events regardless of the value of a
///find the RED at that position, if any
int red = A.redHere; if(red == -1) continue;
reds.push_back(ii(red, i));
///find the earliest event that triggers this RED (rightmost non-Red with nonRED.a < RED.b or rightmost GREEN)
auto early = lower_bound(s.begin(), s.end(), iii(arr[red].a, ii(-1,-1)));
if(early == s.begin()) continue; ///no event in this case
early--;
///Number of rotations before event (rotation = N-1 turns)
long long distance = i - (early->second).second;
if(distance < 0) distance += (belt.size());
minMoves = min(distance, minMoves); ///find the minimum number of rotations
}
///No event can occur, answer last infiinite
if(minMoves == inf){
cout << "-1 -1";
return 0;
}
else if(minMoves != 0){ ///Move the reds CCW along the belt
totalMoves += minMoves * (n-1);
for(ii R : reds) belt[R.second].redHere = -1;
for(ii R : reds){
int pos = R.second;
pos -= minMoves; if(pos < 0) pos += belt.size();
belt[pos].redHere = R.first;
}
}
ii ans = ii(inf, inf);
///Updating the belt
for(int i = 0;i < (int) belt.size();i++){
if(belt[i].redHere == -1) continue;
animal R = arr[belt[i].redHere];
if(belt[i].b < R.a){ ///BLUE type event occured
belt[i].redHere = -1;
belt.insert(belt.begin()+(i+1), R); ///inserting in middle of vector
///updating the colours of the surrounding non-REDs
if(belt[i].c > R.a) belt[i].colour = GREEN;
else belt[i].colour = BLUE;
int nxt = i+2; if(nxt == (int) belt.size()) nxt = 0;
if(belt[i+1].c > belt[nxt].a) belt[i+1].colour = GREEN;
else belt[i+1].colour = BLUE;
}
else if(belt[i].colour == GREEN){ ///GREEN type event occured, game ends
ans = min(ans, ii(totalMoves + R.pos + 2, belt[i].id));
}
}
if(ans.first != inf){
cout << ans.second << " " << ans.first;
return 0;
}
}
}
