ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#196973 | #3442. 序列 | Zeardoe | 100 | 10647ms | 28632kb | C++11 | 3.9kb | 2023-11-04 11:44:38 | 2023-11-04 13:04:50 |
answer
/*
[templates]:
duipai
spjdp
compre
addhis
floor_sum
treedfs
matrix
network_flow
polynomial
lca
bitset
valuesgt
fenwick
erbitree
*/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#include<bits/stdc++.h>
using namespace std;
#define int long long
//use ll instead of int.
#define f(i, a, b) for(int i = (a); i <= (b); i++)
#define cl(i, n) i.clear(),i.resize(n);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e18;
//#define cerr if(false)cerr
//#define freopen if(false)freopen
mt19937 rng(time(0));
int rnd(int l, int r) {return rng() % (r-l+1) + l; }
#define watch(x) cerr << (#x) << ' '<<'i'<<'s'<<' ' << x << endl
void pofe(int number, int bitnum) {
string s; f(i, 0, bitnum) {s += char(number & 1) + '0'; number >>= 1; }
reverse(s.begin(), s.end()); cerr << s << endl;
return;
}
template <typename TYP> void cmax(TYP &x, TYP y) {if(x < y) x = y;}
template <typename TYP> void cmin(TYP &x, TYP y) {if(x > y) x = y;}
//调不出来给我对拍!
//use std::array.
const int N = 262144;
int a[N + 10], b[N + 10], c[N + 10]; int s[N + 10];
int ans = 0; vector<int> tyl[N + 10]; int n;
int mxp(int l, int r) {
//[l, r] 内 popcount 最小值.
int res = __builtin_popcount(l);
int tot = 0;
for(int i = 30; i >= 0; i --) {
if(((l >> i) & 1) != ((r >> i) & 1)) {
cmin(res, tot + 1);
break;
}
if((l >> i) & 1) tot ++;
}
return res;
}
void solve(int t) {
// f(i, 0, t) tyl[i].clear();
// f(i, 1, n) tyl[a[i] % t].push_back(i);
int lban = 0, rban = 0, zh = 0, fu = 0, hf = n / 2;
f(i, 1, n) s[i] = c[i] - b[i];
sort(s + 1, s + n + 1);
f(i, 1, hf) lban += s[i];
for(int i = n; i >= n - hf + 1; i --) rban += s[i];
f(i, 1, n) if(s[i] < 0) fu += s[i];
else zh += s[i];
int mid = (n + 2) >> 1;
vector<int> vec;
vec.push_back(0);
f(i, 1, n) vec.push_back(t - a[i] % t);
sort(vec.begin(), vec.end());
int unq = unique(vec.begin(), vec.end()) - vec.begin() - 1;
// cerr << "unq = " << unq << endl;
f(i, 1, n) tyl[lower_bound(vec.begin(), vec.begin() + unq + 1, t - a[i] % t) - vec.begin()].push_back(i);
auto inc = [&](int x) {
int it = upper_bound(s + 1, s + n + 1, x) - s - 1;
s[it] ++;
if(it <= hf) lban ++;
else if(it >= n - hf + 1) rban ++;
if(s[it] <= 0) fu ++;
else zh ++;
};
f(id, 0, unq) {
int x = vec[id];
int cost = (s[mid] >= 0 ? zh - fu : rban - lban);
// cerr << "T = " << t << ", x = " << x << endl;
// f(i, 1, n) cerr << s[i] << " \n"[i == n ];
// cerr << "zh = " << zh << ", fu = " << fu << endl;
// cerr << "cost = " << __lg(t) + __builtin_popcount(x) + cost + max(-s[mid], 0ll) << endl;
cmin(ans, __lg(t) + mxp(x, (id == unq ? t - 1 : vec[id + 1] - 1)) + cost + max(-s[mid], 0ll) );
if(id == unq) break;
for(int i : tyl[id + 1]) {
inc(c[i] - b[i]);
}
}
f(id, 0, unq) tyl[id].clear();
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
//freopen();
//freopen();
//time_t start = clock();
//think twice,code once.
//think once,debug forever.
int T; cin >> T;
while(T--) {
ans = inf;
cin >> n;
f(i, 1, n) cin >> a[i];
f(i, 1, n) cin >> b[i];
int ub = 30;
//除法次数 [0, ub]
f(t, 0, ub) {
f(i, 1, n) c[i] = a[i] / (1 << t);
solve(1 << t);
}
cout << ans << endl;
}
//time_t finish = clock();
//cout << "time used:" << (finish-start) * 1.0 / CLOCKS_PER_SEC <<"s"<< endl;
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 83ms
memory: 7440kb
input:
10000 1 246 1197696 1 5729 12760441 1 0 99844 1 3 2513 1 1077 976660 1 12602599 0 1 3 3 1 535554183 ...
output:
1197450 12754712 99844 2510 975583 24 0 29 1170 10080920 793129 26 217 375687 53 236 21176 515 65915...
result:
ok 10000 lines
Test #2:
score: 10
Accepted
time: 1004ms
memory: 13040kb
input:
999 100 16120 16120 16120 16120 16120 16120 16120 16120 16120 16120 16120 16120 16120 16120 16120 16...
output:
2962746559 5755570926 2754481527 3357057321 1973394810 2794402305 2389444350 3804182229 3735658855 9...
result:
ok 999 lines
Test #3:
score: 10
Accepted
time: 1897ms
memory: 28632kb
input:
999 100 0 6334 48973058 1603 480692 17485534 3217 2365 75 225182 214767 155172 6879 4452 0 309 15175...
output:
31 58498956 72 344 22054 255 1604 32 641136295 87 52874 30 3543120 211801 6553 324837 1806050 790633...
result:
ok 999 lines
Test #4:
score: 10
Accepted
time: 612ms
memory: 13044kb
input:
999 100 887846 887846 887846 887846 887846 887846 887846 887846 887846 887846 887846 887846 887846 8...
output:
93 35 293 5584893 23 21 34389431 134953 241 14350519 143 8 405086136 20 17 7642380 154507 34947562 7...
result:
ok 999 lines
Test #5:
score: 10
Accepted
time: 0ms
memory: 7432kb
input:
4 1 37 41130 10 558683482 88020542 417643 59329 6631 0 35 215447 437226236 96881284 127 0 76050 258 ...
output:
41093 144074562 1294 93962343
result:
ok 4 lines
Test #6:
score: 10
Accepted
time: 11ms
memory: 7596kb
input:
30 32 8 2 8 0 7 0 0 3 12 72 23 75 1 1 89 0 8 1 420 101 125 114 28 0 4 4 30 12 11 0 1 127 1 2 3 26 46...
output:
561 491 448 455 617 317 795 357 443 818 492 517 524 382 450 336 446 272 521 539 526 390 424 411 611 ...
result:
ok 30 lines
Test #7:
score: 10
Accepted
time: 1286ms
memory: 15056kb
input:
500 100 6407 13687833 481732 730107 177509531 54827909 7597308 43147 15931671 1233521 5379963 621169...
output:
12556810 12599655 8872122 7682648 14979822 16600437 18976593 12512484 11540732 9657027 11618038 1219...
result:
ok 500 lines
Test #8:
score: 10
Accepted
time: 1084ms
memory: 15068kb
input:
500 100 3987330 230130764 13588694 625002 13854531 120 8102846 13831 18977879 617868 380282672 28649...
output:
10007494 15185462 13849153 5627357 12460574 11687359 12771195 12196549 7709226 13762519 16706760 102...
result:
ok 500 lines
Test #9:
score: 10
Accepted
time: 2509ms
memory: 23636kb
input:
1000 100 31090340 19381562 209016 497550365 68665 127051 157556680 58287 79583 24349 241536228 11773...
output:
9898674 18926711 12936380 13248260 8796643 13424284 9333637 8122698 8751367 18853321 11602296 814531...
result:
ok 1000 lines
Test #10:
score: 10
Accepted
time: 2161ms
memory: 23636kb
input:
1000 100 266848 32179751 160098 17708 10658751 211438 1756262 1770383 4940815 20715 985376 264984297...
output:
10030575 7193755 9245509 9364472 11558581 18709097 13906947 19808293 10070196 10580189 13187455 9887...
result:
ok 1000 lines
Extra Test:
score: 0
Extra Test Passed