I finally gave this question to AC today. Haha...
Question:
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 201
Inversion
Time Limit: 1 second memory limit: 32768 KB
Let {A1, A2 ,..., an} be a permutation of the set {1, 2 ,..., n }. if I <j and AI> AJ then the pair (AI, AJ) is called an "inversion" of the permutation. for example, the permutation {3, 1, 4, 2} has three inversions: (3, 1), (3, 2) and (4, 2 ). the inversion table b1, b2 ,..., bn of the permutation {A1, A2 ,..., an} is obtained by lew.bj be the number of elements to the left of J that are greater than J. (in other words, BJ is the number of inversions whose second component is J .) for example, the permutation: {, 3} has the inversion Table2 3 6 4 0 2 1 0 since there are 2 numbers, 5 and 9, to the left of 1; 3 numbers, 5, 9 and 8, to the left of 2; etc. perhaps the most important fact about inversions is Marshall Hall's observation that an inversion table uniquely determines the corresponding permutation. so your task is to convert a permutation to its inversion table, or vise versa, to convert from an inversion table to the corresponding permutation. input: the input consists of several test cases. each test case contains two lines. the first line contains a single integer N (1 <= n <= 50) which indicates the number of elements in the permutation/invertion table. the second line begins with a single charactor either 'P', meaning that the next n integers form a permutation, or 'I', meaning that the next n integers form an inversion table. following are n integers, separated by spaces. the input is terminated by a line contains n = 0. output: for each case of the input output a line of intergers, seperated by a single space (no space at the end of the line ). if the input is a permutation, your output will be the corresponding inversion table; if the input is an inversion table, your output will be the corresponding permutation. sample input:
9P 5 9 1 8 2 6 4 7 39I 2 3 6 4 0 2 2 1 00
Sample output:
2 3 6 4 0 2 2 1 05 9 1 8 2 6 4 7 3
________________________________________________
#include <iostream>using namespace std;const int maxsize = 51;int getResultI(int p[],int length,int j);int getResultP(int result[],int length,int x);int main(){int n;while((cin>>n)&&n){char ch;cin>>ch;int a[maxsize]={0};int result[maxsize];for(int i=0;i<=n;i++)result[i]=-1;for(int i=1;i<=n;i++)cin>>a[i];switch(ch){case 'P':for(int i=1;i<=n;i++)result[i] = getResultI( a, n, i);break;case 'I':int k;for(int i=1;i<=n;i++){//int temp = a[i];k = getResultP(result,n,a[i]);if(result[k]==-1){result[k] = i;}else {while(result[k]!=-1)k++;result[k] = i; }}//forbreak;default:break;}for(int i=1;i<=n;i++){if(i!=n)cout<<result[i]<<" ";else cout<<result[i];}cout<<endl;}//whilereturn 0;}int getResultI(int p[],int length,int j){int k;for(int i=1;i<=length;i++)if(p[i]==j){k = i;break;}int count=0;for(int i=1;i<k;i++){if(p[i]>j)count++;}return count;}int getResultP(int result[],int length,int x){int count=0;for(int i=1;i<=length;i++){if(count==x)return i;if(result[i]==-1)count++;}}
_______________________________________________________________________________
Experience: In fact, I am only one step away from AC.
for(int i=1;i<=n;i++){if(i!=n)cout<<result[i]<<" ";else cout<<result[i];}cout<<endl;
I have never cared much about the output statements, and the results have hurt me. Today, I wanted to give up again. Next time I come back early, I added a line break to my end and finally saw the red accepted. My heart is really happy. Pay attention to the output in the future.