P1966 Matches Queuing Problem Solution

P1966 Matches line up

The meaning of the question : There are two rows of matches, each with its own height, and the heights of each pair are different. You can only exchange adjacent numbers each time. Ask how many times to exchange at least so that ∑ (ai − bi) 2 \sum (a_i-b_i) ^2(aibi)2 minimum.

Solution : Discretization + tree array!
Because the data is different, it can be thought that the optimal situation must be the smallest corresponding to the smallest of the second group, and the second smallest corresponds to the second smallest,..., then it is clear that this question has nothing to do with the size of the number, only the relative size. , Then you can think of discretization. Here are two methods of discretization. When you first understand it, you will have a wrong meaning, thinking that after discretization, you just take the discretized value as p[a[i]]=b[ i], so the second method is used, but after the second method is discretized, the subscript of the original array cannot be accessed. I thought about it carefully, because the number of exchanges is required. While showing the relative size, you also need to know where the first smallest value is and where is the second smallest value in the original array. Then the second type of discrete The chemical method used in this question is wrong (only one point has passed).

Code :

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include  <bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-4
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const LL INF = 1e18;
const int N = 1e5+10;
const LL Mod = 99999997;
const int M = 110;
LL tmp[N], c[N];
int n, cnt;
LL tr[N];
int lowbit(int x) {
    
     return x & -x; }
void upd(int p, LL d) {
    
    
  for( ; p <= n; p += lowbit(p) ) tr[p] = (tr[p] + d) % Mod;
}
LL que(int p) {
    
    
  LL r = 0;
  for( ; p > 0; p -= lowbit(p) ) r = (r + tr[p]) % Mod;
  return r;
}
struct xx {
    
    
  int v, id;
  bool operator < (const xx &c) const {
    
    
    return v < c.v;
  }
}a[N], b[N];
int main() {
    
    
  scanf("%d", &n);
  _rep(1, n, i) {
    
     scanf("%lld", &a[i].v); a[i].id = i; }
  _rep(1, n, i) {
    
     scanf("%lld", &b[i].v); b[i].id = i; }
  sort(a+1, a+n+1); sort(b+1, b+n+1);
  _rep(1, n, i) c[a[i].id] = b[i].id;
  LL ans = 0;
  _rep(1, n, i)  {
    
    
    upd(c[i], 1); ans = (ans + i - que(c[i]))%Mod;
  }
  printf("%lld\n", ans);
  return 0;
}

Guess you like

Origin blog.csdn.net/qq_43408978/article/details/108988078