码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷 P2524 Uim的情人节礼物·其之弐 题解

时间:2017-10-14 21:16:39      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:成功   www   ble   顺序   .com   math   题目   html   char   

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

题目链接:https://www.luogu.org/problem/show?pid=2524

题目描述

前传:详见洛谷P2525

Uim成功地按照顺序将礼物送到了N个妹子的手里并维持她们的和谐。

Uim现在想知道,他最终选择的顺序是所有给N个妹子送礼顺序中、字典序第几小的。

输入输出格式

输入格式:

第一行一个整数N,表示有N个数。

第二行一个整数X,表示给出的排列。

输出格式:

一个整数,表示是第几小的字典序。

输入输出样例

输入样例#1:
3
231
输出样例#1:
4

说明

1<=N<=9

输入的排列没有空格

 

分析:

康托展开裸题。推荐博客

 

AC代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 
 6 int fac[15] = {1};
 7 char a[15];
 8 int n,num[15],vis[15];
 9 
10 long long solCT()
11 {
12     long long ans = 1;
13     int cnt;
14     for(int i = 1;i <= n;++ i)
15     {
16         cnt = 0;
17         for(int j = i+1;j <= n;++ j)
18             if(num[j] < num[i]) ++ cnt;
19         ans += 1LL*cnt*fac[n-i];
20     }
21     
22     return ans;
23 }
24 
25 int main()
26 {
27     scanf("%d",&n);
28     for(int i = 1;i <= n;++ i)
29         fac[i] = fac[i-1]*i;
30     scanf("%s",a);
31     for(int i = 0;i < n;++ i)
32         num[i+1] = a[i]-0;
33     printf("%lld\n",solCT());
34     return 0;
35 }

 

 

洛谷 P2524 Uim的情人节礼物·其之弐 题解

标签:成功   www   ble   顺序   .com   math   题目   html   char   

原文地址:http://www.cnblogs.com/shingen/p/7668252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!