#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int jiecheng(int x)
{
int ans = 1;
for (int i = 1; i <= x; i++)
{
ans *= i;
}
return ans;
}
int a[20];
void fun()
{
a[0] = 1;
for (int i = 1; i <= 15; i++)
{
a[i] = jiecheng(i);
}
}
int main()
{
int n;
while (cin >> n)
{
int tt = n;
int num[20];
fun();
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
int ans = 0;
for (int i = 0; i < n; i++)
{
int cnt = 0;
for (int j = i + 1; j < n; j++)
{
if (num[j] < num[i])
{
cnt++;
}
}
ans += a[--tt] * cnt;
}
cout << ans << endl;
next_permutation(num, num + n);//下一个字典序
for (int i = 0; i < n; i++)
{
cout << num[i] << ' ';
}
cout << endl;
}
return 0;
}
swustoj(0541)
最新推荐文章于 2022-10-26 19:42:58 发布