P1966はキューイング問題の解決策と一致します

P1966マッチラインナップ

質問の意味:2行のマッチがあり、それぞれに独自の高さがあり、各ペアの高さが異なります。毎回隣接する番号のみを交換できます。少なくとも∑(ai − bi)2 \ sum(a_i-b_i)^ 2Σ Ab)。最低2つ

解決策:離散化+ツリー配列!
データが異なるため、最適な状況は2番目のグループの最小に対応する最小でなければならないと考えることができます。2番目に小さいものは2番目に小さいものに対応します...そして、この質問には何もする必要がないことは明らかです。数値のサイズで行い、相対的なサイズのみを行います。次に、離散化を考えることができます。離散化の2つの方法があります最初に理解したときは、離散化した後は、離散化された値はp [a [i]] = b [i]であるため、2番目の方法が使用されますが、2番目の方法が離散化された後は、元の配列の添え字にアクセスできません。交換の数が必要なため、慎重に検討しました。相対的なサイズを示しながら、最初の最小値が元の配列のどこにあり、どこが2番目に小さい値であり、次に2番目のタイプの離散値であるかを知る必要もあります。この質問で使用されている化学的方法は間違っています(1つのポイントだけが通過しました)。

コード

#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;
}

おすすめ

転載: blog.csdn.net/qq_43408978/article/details/108988078