版本比较
题解:CF2246D diss_quack and Array Game
正文
增加 23 行删除 12 行
56 行未改动5758596061616263646566676468697071727374757677787980818283848586877475767778798088899083919293948695969798999010010110210310410510610710810956 行未改动#define pb push_backusing namespace std;const int N = 200050;const int INF = 1e18;int f[N+10]; int a[N+10]; int b[N+10];
int f[N+50]; int b[20][N+50]; int a[N+50];
void pre(){ f[1] = 1; for (int i = 2; i <= N+5; i++){ for (int i = 2; i < N+20; i++){ if (i&1) f[i] = f[i/2]+2; else f[i] = f[i/2]+1; } for (int i = 0; i <= 18; i++){ int lim = N>>i; for (int j = 1; j <= lim + 5; j++){ b[i][j] = (j<<i)+f[j]; } for (int j = lim+4; j >= 1; j--){ b[i][j] = min(b[i][j],b[i][j+1]); } }}
void solve(){ int n; cin >> n; int ans = INF; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i <= 18; i++){ int cur = i,lim = N>>i; for (int j = 1; j <= lim+5; j++){ b[j] = (j<<i)+f[j]; } for (int j = lim+4; j >= 1;j--){ b[j] = min(b[j],b[j+1]); } int cur = i; for (int j = 1; j <= n; j++){ int need = (a[j]+(1LL<<i)-1)>>i; cur += b[need]-a[j]; cur += b[i][need]-a[j]; } ans = min(ans,cur); } } cout << ans << endl; cout << ans << endl;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); pre(); pre(); int _t; cin >> _t; while (_t--) solve(); return 0; }
```
時間複雜度為 $O(19 \times (N+n))$,在$n \log n$ 級別,可以輕鬆通過。