代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define x first
#define y second
#define lowbit(x) (x&(-x))
const int N = 200;
ll a[N], h[N];
void solve(){
int n;
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
for(int i = 1; i <= n; i ++){
cin >> h[i];
}
ll ans = 0, last = n;
for(int i = n; i >= 1; i --){
if((a[i] - a[i - 1]) >= h[i]){
int dis = a[i] - h[i];
ans += (a[last] - dis + 1) * (a[last] - dis) / 2;
last = i - 1;
}
else{
h[i - 1] = max(h[i - 1], h[i] - (a[i] - a[i - 1]));
}
}
cout << ans << endl;
}
int main(){
int T;
cin >> T;
while(T--)
solve();
}
D
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define x first
#define y second
#define lowbit(x) (x&(-x))
const int N = 2e5 + 10;
int a[N], cnt[N], n;
ll cal(int x, int y){
ll sum1 = 0, sum2 = 0, sum3 = 0, k = 1;
for(; k <= n; k ++){
if(sum1 + cnt[k] > x)break;
sum1 += cnt[k];
}
for(; k <= n; k ++){
if(sum2 + cnt[k] > y)break;
sum2 += cnt[k];
}
sum3 = n - sum1 - sum2;
ll z = 1;
while(sum3 > z) z <<= 1;
return x - sum1 + y - sum2 + z - sum3;
}
void solve(){
cin >> n;
for(int i = 0; i <= n; i ++) cnt[i] = 0;
for(int i = 1; i <= n; i ++){
cin >> a[i];
cnt[a[i]] ++;
}
ll ans = 1e18;
for(int i = 0; i <= 20; i ++){
for(int j = 0; j <= 20; j ++){
ans = min(ans, cal(1 << i, 1 << j));
}
}
cout << ans << endl;
}
int main(){
int T;
cin >> T;
while(T--)
solve();
}