This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#define TEST
#ifndef TEST
#include "aliens.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
inline ll sq(ll x){
return x*x;
}
const int len = 1e5+5;
const ll inf = 1e15;
int n, m, k, po;
ii arr[len];
vector<ii> vec;
vector<pair<pll, int> > con;
ll com[len];
pair<ll, int> dp[len];
ll func(pll line, int x){
return x*line.fi + line.se;
}
bool bad(pair<pll, int> a, pair<pll, int> b, pair<pll, int> c){
if ((b.fi.se-a.fi.se)*(b.fi.fi-c.fi.fi) == (c.fi.se-b.fi.se)*(a.fi.fi-b.fi.fi))
return (b.se >= a.se || b.se >= c.se);
return ((b.fi.se-a.fi.se)*(b.fi.fi-c.fi.fi) > (c.fi.se-b.fi.se)*(a.fi.fi-b.fi.fi));
}
void add(ll a, ll b, int sec){
while (con.size() >= 2 && bad(con[con.size()-2], con.back(), mp(mp(a, b), sec)))
con.pop_back();
con.pb(mp(mp(a, b), sec));
}
pair<ll, int> query(int x){
if (po >= con.size())
po = con.size()-1;
while (po+1 < con.size() && mp(func(con[po].fi, x), con[po].se) > mp(func(con[po+1].fi, x), con[po].se))
po++;
return mp(func(con[po].fi, x), con[po].se);
}
int check(ll c){
/*for (int i = 1; i <= n; i++){
pair<ll, int> ans = mp(inf, 0);
for (int j = 0; j < i; j++){
ll temp = (vec[i].se-vec[j+1].fi+1)*1LL*(vec[i].se-vec[j+1].fi+1) - com[j];
ans = min(ans, mp(dp[j].fi+temp, dp[j].se));
}
ans.fi += c;
ans.se += 1;
dp[i] = ans;
//printf("i = %d, ans = (%lld, %d)\n", i, ans.fi, ans.se);
}*/
po = 0;
con.clear();
add(-2*(vec[1].fi-1), sq((ll)vec[1].fi-1), 0);
for (int i = 1; i <= n; i++){
dp[i] = query(vec[i].se);
dp[i].fi += sq((ll)vec[i].se)+c;
dp[i].se += 1;
if (i != n)
add(-2*(vec[i+1].fi-1), dp[i].fi+sq((ll)vec[i+1].fi-1)-com[i], dp[i].se);
}
return dp[n].se;
}
ll bs(){
ll l = 0, r = m*1LL*m, ans;
while (l <= r){
ll mid = (l+r)/2;
int cur = check(mid);
//printf("mid = %lld, cur = %d\n", mid, cur);
if (cur <= k)
r = mid-1, ans = mid;
else
l = mid+1;
}
return ans;
}
ll take_photos(int N, int M, int K, vector<int> R, vector<int> C){
n = N, m = M, k = K;
for (int i = 0; i < n; i++){
arr[i] = mp(R[i]+1, C[i]+1);
if (arr[i].fi > arr[i].se)
swap(arr[i].fi, arr[i].se);
}
sort(arr, arr+n);
vec.pb(mp(1, 0));
for (int i = 0; i < n; i++){
while (vec.size() > 1 && vec.back().fi == arr[i].fi)
vec.pop_back();
if (arr[i].se > vec.back().se)
vec.pb(arr[i]);
}
n = vec.size()-1;
k = min(n, k);
for (int i = 0; i < n; i++){
if (vec[i].se < vec[i+1].fi)
com[i] = 0;
else
com[i] = pow(vec[i].se-vec[i+1].fi+1, 2);
}
//for (int i = 0; i <= n; i++)
// printf("%d %d\n", vec[i].fi, vec[i].se);
ll c = bs(), temp = check(c);
//printf("c = %lld, ans = %lld\n", c, dp[n].fi - k*c);
return dp[n].fi - k*c;
}
#ifdef TEST
int main() {
int N, M, K;
assert(3 == scanf("%d %d %d", &N, &M, &K));
//printf("N = %d, M = %d, K = %d\n", N, M, K);
vector<int> r(N), c(N);
for (int i = 0; i < N; i++) {
assert(2 == scanf("%d %d", &r[i], &c[i]));
}
long long ans = take_photos(N, M, K, r, c);
printf("%lld\n", ans);
return 0;
}
#endif
Compilation message (stderr)
aliens.cpp: In function 'std::pair<long long int, int> query(int)':
aliens.cpp:47:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (po >= con.size())
~~~^~~~~~~~~~~~~
aliens.cpp:50:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (po+1 < con.size() && mp(func(con[po].fi, x), con[po].se) > mp(func(con[po+1].fi, x), con[po].se))
~~~~~^~~~~~~~~~~~
aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:134:18: warning: unused variable 'temp' [-Wunused-variable]
ll c = bs(), temp = check(c);
^~~~
aliens.cpp: In function 'll bs()':
aliens.cpp:100:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
return ans;
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |