题解:P12092 [NERC2024] Adrenaline Rush
DecemberFox · · 题解
P12092 [NERC2024] Adrenaline Rush 题解
题目分析
题目要求我们给出一个超车的方案,使得最终到达终点的车辆序列为
解法分析
想要超车的次数最大化,对于每一辆车都要就要尽可能多地超越其余的车。进行一次超车,就相当于调换了两车的相对顺序,那么我们可以比较初始和结束的序列判断途中的超车情况。
对于两辆车
若最终
此外,题目要求构造一组超车的序列。为保证不重不漏,我们从结果中最后一名开始,它一定可以超过原来在它前面的车,且其余所有车都超过它一次。
对于序列中,就相当于将该车移动到序列的第一位,然后再移动至最后面,移动的过程就是超车的过程。
处理完最后一辆车后,依次向前遍历,同样是超越了它前面的车一次,然后又被剩余的车超过(正好排除了最后一名的车)。
依次类推,最终可以处理完整个序列。代码如下。
#include<iostream>
#include<utility>
#include<cstdio>
#include<queue>
namespace SOLVE
{
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef unsigned __int128 ulll;
const int const1=1e3+10;
int t[const1],rank[const1],now[const1],pos[const1];
std::queue<std::pair<int,int> > ans;
void solve()
{
int n,i,j,k;
read(n);
for(i=1;i<=n;++i)
{
read(t[i]);
rank[t[i]]=now[i]=pos[i]=i;
}
for(i=n;i>=1;--i)
{
for(j=pos[t[i]];j>1;--j)
{
ans.push(std::make_pair(now[j],now[j-1]));
std::swap(pos[now[j]],pos[now[j-1]]);
std::swap(now[j],now[j-1]);
}
for(j=1;j<rank[t[i]];++j)
{
ans.push(std::make_pair(now[j+1],now[j]));
std::swap(pos[now[j+1]],pos[now[j]]);
std::swap(now[j+1],now[j]);
}
}
write(ans.size());
while(!ans.empty())
{
write(ans.front().first,' ',ans.front().second);
ans.pop();
}
}
}
int main()
{
SOLVE::solve();
return 0;
}