# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
934996 | dimashhh | Aliens (IOI16_aliens) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 3, MOD = 1e9 + 7;
typedef long long ll;
int n,m,k;
vector<pair<ll,ll>> a,x;
ll res;
ll dp[N],L[N],R[N];
int col[N];
ll f(ll pen){
dp[0] = 0;
col[0] = 0;
for(int i = 1;i <= n;i++){
dp[i] = 1e15;
for(int j = i;j >= 1;j--){
if(dp[j - 1] + (R[i] - L[j] + 1) * (R[i] - L[j] + 1) -
max(0ll, R[j - 1] - L[j] + 1) * max(0ll, R[j - 1] - L[j] + 1) + pen < dp[i]){
dp[i] = min(dp[i], dp[j - 1] + (R[i] - L[j] + 1) * (R[i] - L[j] + 1) -
max(0ll, R[j - 1] - L[j] + 1) * max(0ll, R[j - 1] - L[j] + 1) + pen);
col[i] = col[j - 1] + 1;
}
}
}
res = dp[n];
return col[n];
}
void test(){
cin >> n >> m >> k;
for(int i = 1;i <= n;i++){
int l,r;
cin >> l >> r;
++l;++r;
if(l > r){
swap(l,r);
}
a.push_back({l,-r});
}
sort(a.begin(),a.end());
int mx = 0;
x.push_back({0,0});
for(auto [l,r]:a){
r = -r;
if(r > mx){
mx = r;
x.push_back({l,r});
}
}
n = (int)x.size() - 1;
for (int i = 1; i <= n; i++) {
L[i] = x[i].first;
R[i] = x[i].second;
// cout << L[i] << ' ' << R[i] << '\n';
}
ll l = -1e11,r = 1e11;
while(r - l > 1) {
ll mid = (l + r) >> 1;
// cout << l << ' ' << r << ' ' << mid << ' ' << f(mid) << '\n';
if (f(mid) >= k) {
l = mid;
} else {
r = mid;
}
}
f(l);
cout << res - k * l << '\n';
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
test();
}