AtCoder Beginner Contest 465 C - Reverse Permutation 题解
solve
- 反转,直接暴力一个一个反转显然不可以,必须考虑转化
- 盯着三个样例看,我们发现了如下信息:
- 最大的数和第二大的数对应的位置都是 o 这个字母
- 交换有优先级
- 三个样例的第一位都是最大的那个数,而第二大的数在最后,以此类推,发现数基本平均分配在两边
- 三个样例的 1 和 2 基本在中间
- 同时考虑转化,我们就可以想到:按题意正着写不行,我们可以倒着做,一前一后向答案数组中填入答案
- 当上一次在前面填入时,如果反转,就在后面填,反之在头上填,上一次在后面填入的情况同理,上一次的转态可以用一个变量维护
- 填答案可以利用 l 和 r 两个指向头和尾的变量,由于优先级,这样做不会导致答案错误
请忽略那个极大的p
code
#include<bits/stdc++.h>
using namespace std;
constexpr int p=1000000007,maxn=5e5+10;
int read() {int x=0,f=1;char ch=getchar();while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}
int n;
string s;
int as[maxn];
int main()
{
n=read();
cin >> s;s=" "+s;
int l=0,r=n+1,f=1;
for (int i=n;i>=1;i--)
{
if (f)
{
if (s[i] == 'o') as[++l]=i,f=0;
else as[--r]=i;
}
else
{
if (s[i] == 'o') as[--r]=i,f=1;
else as[++l]=i;
}
}
for (int i=1;i<=n;i++) printf("%d ",as[i]);
return 0;
}
附加
LZY的图做法
你甚至可以从LZY那跳回来

浙公网安备 33010602011771号