P操作所求逆序对
I操作根据逆序对来求原数组
算是道水题。
#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn = 55;
int P[maxn], ans[maxn], n;
int main(){
while(scanf("%d", &n) && n){
char str[2];
memset(ans, 0, sizeof(ans));
scanf("%s", str);
for(int i = 1; i <= n; ++i){
scanf("%d", &P[i]);
}
if(str[0] == 'P'){
for(int i = 1; i <= n; ++i){
int tmp = 0;
for(int j = 1; j < i; ++j){
if(P[i] < P[j])tmp++;
}
ans[P[i]] = tmp;
}
}else{
int tmpn = 0;
for(int i = n; i >= 1; --i){
int tmp = P[i], j;
for(j = 1; j <= tmpn && tmp; ++j){
if(ans[j] > i)tmp--;
}
for(int k = tmpn + 1; k > j; --k){
ans[k] = ans[k - 1];
}
ans[j] = i;
tmpn++;
}
}
for(int i = 1; i <= n; ++i){
if(i > 1)printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}