Codeforces Round #285 Div.1 B Misha and permutations summation

Source: Internet
Author: User
Tags printf

Test instructions: Give two permutations, find out the rank of each arrangement in the whole arrangement, add, modulo n! (the total number of rows) to obtain a number k, the arrangement of the row behavior K.

Solution: First of all to obtain a positioning method, that is, know that an arrangement is the first permutation. For example (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).

Take the Arrangement (1,2,0), the first is 1, the front has cnt=1 less than 1 of the unused number (0), so its ranking to add (cnt=1) *2!, the second is 2, because 1 has been put, so less than 2 is only 0, that is, cnt=1, so, the ranking also added (cnt=1) *1!, so the rank is 3.

Introduction of general Conclusions:

Pre[i] represents the number of numbers that are less than I and are not occupied. We can use a tree-like array to update one side of the query to find out the two permutations of all pre[] values, save to the P array: p1[i] = pre1[b1[i]],p2[i] = Pre2[b2[i]], that is, Cantor unfolded

Then rank and for (P1[i]+p2[i]) * (n-1)! + ... + (P1[n]+p2[n]) *0! = p3[1]* (n-1)! + ... + p3[n]*0! , but the resulting expression may not be a structured form, which we need to detect on one side, from backward to forward, if P3[i] >= (n-i+1), stating that item I has exceeded (n-i+1) * (n-i), then it should be rounded up to (n-i+1)!, that is p3[i-1]+= 1, and so on, the rounding of the 1th bit is no longer considered.

Finally get the correct p3[] sequence, and then through the tree array + two points in the complexity of the NLOGNLOGN will p3 each bit corresponds to the results of each number of permutations, that is, the above for Rank (p) of the inverse operation, that is, the inverse Cantor expansion.


whn6325689//Mr.phoebe//http://blog.csdn.net/u013007900 #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include < fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include & lt;deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map&
Gt #include <cmath> #include <functional> #include <numeric> #pragma comment (linker, "/stack

: 1024000000,1024000000 ") using namespace std;
typedef long Long LL;
typedef long double LD;
typedef pair<ll, ll> PLL;
typedef complex<ld> Point;
typedef pair<int, int> PII;
typedef PAIR&LT;PII, int> PIII;

typedef vector<int> VI; #define CLR (x, y) memset (x,y,sizeof) #define MP (Make_pair) #define PB (x) push_back (x) #define LOWBIT (x) (X&amp ;(-X) #define MID (x, y) (x+ (y-x) >>1)) #define EPS 1e-9 #define PI acos ( -1.0) #define INF 0x3f3f3f3f #define Llinf 1ll<<62 template<class t&
Gt
    inline bool Read (t &n) {T x = 0, tmp = 1;
    char C = GetChar ();
    while ((C < ' 0 ' | | c > ' 9 ') && c! = '-' && c! = EOF) c = GetChar ();
    if (c = = EOF) return false;
    if (c = = '-') c = GetChar (), tmp =-1;
    while (c >= ' 0 ' && C <= ' 9 ') x *=, X + = (c-' 0 '), C = GetChar ();
    n = x*tmp;
return true;
        } template <class t> inline void write (T n) {if (n < 0) {Putchar ('-');
    n =-N;
    } int len = 0,data[20];
        while (n) {data[len++] = n%10;
    n/= 10;
    } if (!len) data[len++] = 0;
while (len--) Putchar (data[len]+48);

}//-----------------------------------const int maxn=200010;
int n;
int A[MAXN], B[MAXN];

int F[MAXN], C[MAXN];
        void Add (int x, int y) {while (x <= N) {c[x] + = y;
    x + = Lowbit (x); }} int sum (iNT x) {int res = 0;
        while (x) {res + = c[x];
    X-= Lowbit (x);
} return res;
    } void Init () {CLR (c, 0);
for (int i=1; i<=n; i++) Add (i, 1);
    } int main () {read (n);
        for (int i=1; i<=n; i++) {read (a[i]);
    a[i]++;
        } for (int i=1; i<=n; i++) {read (b[i]);
    b[i]++;
    } init ();
        for (int i=1; i<n; i++) {int les=sum (a[i]-1);
        F[n-i]+=les;
    Add (A[i],-1);
    } init ();
        for (int i=1; i<n; i++) {int les=sum (b[i]-1);
        F[n-i]+=les;
    Add (B[i],-1);
        } for (int i=1; i<n; i++)//¼æëã³öäæðòêý{f[i+1]+=f[i]/(i+1);
    f[i]=f[i]% (i+1);
    } init ();
    int sml=1;
        for (int i=n-1; i>=1; i--) {int l=1, r=n, Mid, TMP, ANS=-1;
            while (L <= R) {mid=mid (l,r);
            Tmp=sum (mid-1);
      if (tmp <= F[i]) {          l=mid+1;
            Ans=mid;
            } else {r=mid-1;
        }} if (ans = =-1) ans=sml;
        printf ("%d", ans-1);
        Add (ans,-1);
    while (!C[SML]) sml++;
            } for (int i=1; i<=n; i++) {if (C[i]) {printf ("%d\n", i-1);
        Break
}} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.