题解:AT_abc465_c [ABC465C] Reverse Permutation
OIbeginnerWZH · · 题解
比较好的一道思维题。
分析后可以发现项数越大受到的反转影响相对来说更少,因为第 o时只会反转前
因此我们可以从后往前处理,我们先假设处理到第
- 该序列被反转过奇数次,那么第
i 项在位置l ,此时位置l 已经有对应的项了,将l 增加1 。 - 该序列被反转过偶数次,那么第
i 项在位置r ,此时位置r 已经有对应的项了,将r 减少1 。
由于第 o时只能反转前
由于每次会将
::::info[场时代码] 这份代码里有思考和调试过程。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
typedef unsigned long long ull;
ll t=1;
ll n;
string s;
ll ans[500005];
ll l,r;
int now;//反转了几次
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//cin>>t;
while(t--){
/*
(1 2 3 4 5)
5 (4 3 2 1)
5 (1 2 3) 4
5 (1 2) 3 4
5 2 (1) 3 4
5 2 1 3 4
*/
cin>>n>>s;
l=1,r=n;
for(int i=n;i>=1;i--){
//cout<<s[i-1]<<endl;
if(((s[i-1]=='o')&&(now%2==0))||((s[i-1]=='x')&&(now%2==1))){
ans[l]=i;
l++;
}
else{
ans[r]=i;
r--;
}
if(s[i-1]=='o'){
now++;
}
//cout<<"l:"<<l<<" r:"<<r<<" now:"<<now<<endl;
}
rep(i,1,n){
cout<<ans[i]<<' ';
}
}
return 0;
}
:::: ::::info[场后代码] 这份代码更简洁。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
typedef unsigned long long ull;
ll t=1;
ll n;
string s;
ll ans[500005];
ll l,r;
int now;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while(t--){
cin>>n>>s;
l=1,r=n;
for(int i=n;i>=1;i--){
if(s[i-1]=='o'){
now++;
}
if(now%2==1){
ans[l]=i;
l++;
}
else{
ans[r]=i;
r--;
}
}
rep(i,1,n){
cout<<ans[i]<<' ';
}
}
return 0;
}
:::: ::::info[两句闲话] 我没有搞懂题目标签(完全想不到对应解法)。
| 喜欢就点个赞吧。 |
|---|