[Luo Gu P1966] matches queue

Topic links:

Match queue

Topic analysis:

I feel more logical to be able to launch it? Seems to be a problem
exchanging words you will probably want to reverse to the above, and subject to that formula is used to scaryWith no eggs
Only use probably tell you consider a wave of greed, it is clear that there are two sequences in each pair of corresponding ranking number on the optimal strategy is the same position on this conclusion
detail, assume \ (a_0 \) is \ (a \) sequence of \ (k \) big, \ (b_0 \) is \ (b \) in the sequence of \ (k \) is large, the \ (a_0 \) and \ (b_0 \) definitely need to exchange to the same position, and then subtracted.
Then we consider how to exchange the least number of
first consider the most simple and crude exchange method: two sequences were ranked a sequence, so the ranking is certainly one to one
so the number of exchanges is number two in the sequence in reverse order of the sum
but this is not the best , say the two sequences are the rankings may 1423. If we direct sequencing, there will be an exchange we would be able to get a pair of numbers, in order to transfer him to a place in line rankings go forward together and changed (for example, two sequences have been 1423, but if the direct sorting will have 4 and 2 then it unnecessary to exchange this case), this is actually not contributing to the answer
and then we consider how to optimize
first discovered the problem and in fact this number itself does not matter, mainly of several ranking relationship, so you can put an array of discrete
then that I do not know how to think of a number of priority ...... each first sequence of first redefine what
for example
1342
We take it each element is defined as priority
. 1 2. 4. 3
. 1. 3. 4 2
(corresponding to the vertical)
and then we use our own priority defined redefine the second sequence, the second assumption is
1423
then we replace it with
1342
and we obtained two new sequences are
. 1. 3. 4 2
1 3 4 2
ensure that the first sequence does not move, this time on the second switching element and to a sequence of ranking match on it (because of the change of the first and second change in fact there is no difference in essence)
switching frequency is the second sequence in the reverse order of the number, or merge sort Fenwick tree

Code:

Is not difficult to achieve, I wrote merge, the code is written in a hurry because it is ugly, focusing on the analysis above

#include <bits/stdc++.h>
#define int long long
#define N (100000 + 20)
using namespace std;
inline int read() {
    int cnt = 0, f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + c - '0'; c = getchar();}
    return cnt * f;
}
int n, a[N], b[N << 1], a_[N], res, cnt, ans;
int New[N];
void Discretization(int a[]) {
    sort(b + 1, b + n + 1);
    int q = unique(b + 1, b + n + 1) - b;
    for (register int i = 1; i <= n; i++) 
        a[i] = lower_bound(b + 1, b + q + 1, a[i]) - b - 1;
}
int merge(int l, int r) {
    int mid = (l + r) >> 1;
    int i = l, j = mid + 1; cnt = 0;
    for (register int k = l; k <= r; k++) 
        if (j > r || (i <= mid && a_[i] < a_[j])) b[k] = a_[i++];
        else b[k] = a_[j++], cnt += (mid - i + 1) % 99999997;
    for (register int k = l; k <= r; k++) a_[k] = b[k];
    return cnt;
}
int solve(int l, int r) {
    if (l < r) {
        int mid = (l + r) >> 1;
        solve(l, mid), solve(mid + 1, r);
        ans += merge(l, r); 
    }
    return ans;
}
signed main() {
    n = read();
    for (register int i = 1; i <= n; i++) a[i] = b[i] = read();
    Discretization(a);
    for (register int i = 1; i <= n; i++) a_[i] = b[i] = read();
    Discretization(a_);
    for (register int i = 1; i <= n; i++) New[a[i]] = i;
    for (register int i = 1; i <= n; i++) a_[i] = New[a_[i]];
//  for (register int i = 1; i <= n; i++) cout<<a_[i]<<" ";
//  cout<<endl; 
    memset(b, 0, sizeof(b)); 
    res = solve(1, n) % 99999997;
    printf("%d", res % 99999997);
    return 0;
}

Guess you like

Origin www.cnblogs.com/kma093/p/11294334.html