// Black lives matter
#include <bits/stdc++.h>
#include "aliens.h"
/// 500 485 462 A4
using namespace std;
typedef long long int ll;
typedef complex<double> point;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
//#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
const int N=5e4+10,M=1e6+100,inf=(ll)1e15;
ll dp[N];
ll T[N];
ll A[M];
vector <pii> B;
/*
void solve(ll l,ll r,ll L,ll R,ll k){
if (r<l) return;
ll mid=(r+l)/2;
ll id=0,mx=inf;
for (int i=L;i<=min(R,mid);i++){
ll z=0;
if (i>1){
if (B[i-1].F<B[i-2].S){
z=(B[i-2].S-B[i-1].F)*((B[i-2].S-B[i-1].F));
}
}
ll x=dp[i-1][(k-1)%2]+(B[mid-1].S-B[i-1].F)*(B[mid-1].S-B[i-1].F)-z;
if (mx>x){
mx=x;
id=i;
}
}
dp[mid][k%2]=mx;
// cout << l << " " << r << " " << mid << " " << k << " " << mx << endl;
solve(l,mid-1,L,id,k);
solve(mid+1,r,id,R,k);
}
*/
vector <pair <int,pair <pii,int> > > line;
ll intersect(pii x,pii y){
if (x.F==y.F){
if (x.S<=y.S) return -inf;
else return inf;
}
return (y.S-x.S)/(x.F-y.F)+((y.S-x.S)%(x.F-y.F)>0);
}
void add(pair <pii,int> X){
pii x=X.F;
while(line.size()){
pii y=line.back().S.F;
ll z=intersect(x,y);
if (z<=line.back().F){
line.pop_back();
}
else{
// line.pb({1,{{1,1},1}});
line.pb({z,{x,X.S}});
return ;
}
}
line.pb({-inf,X});
}
pii get(ll x){
auto t=lower_bound(line.begin(),line.end(),pair <int,pair <pii,int> > ({x,{{inf,inf},inf}}))-line.begin();
t--;
pii p=line[t].S.F;
return {x*p.F+p.S,line[t].S.S};
}
long long take_photos(int32_t n, int32_t m, int32_t k, std::vector<int32_t> r, std::vector<int32_t> c){
for (int i=0;i<M;i++) A[i]=i;
for (int i=0;i<n;i++){
ll x=r[i],y=c[i];
A[max(x,y)]=min(A[max(x,y)],min(x,y)-1);
}
for (int i=0;i<m;i++){
if (A[i]<i){
while(B.size() && B.back().F>=A[i]) B.pop_back();
B.pb({A[i],i});
}
}
// for (auto u : B){
// cout << u.F << " " << u.S << endl;
//}
ll t=B.size();
ll l=-1,rr=(ll)1e12;
ll ans=inf;
while(rr-l>1){
line.clear();
ll mid=(rr+l)/2;
for (int i=1;i<=t;i++){
dp[i]=(B[i-1].S-B[0].F)*(B[i-1].S-B[0].F);
}
memset(T,0,sizeof T);
for (int j=2;j<=t;j++){
ll z=0;
if (B[j-1].F<B[j-2].S){
z=(B[j-2].S-B[j-1].F)*((B[j-2].S-B[j-1].F));
}
add({{2*B[j-1].F,-(dp[j-1]-z+B[j-1].F*B[j-1].F+mid)},T[j-1]});
pii p=get(B[j-1].S);
ll x=-p.F+B[j-1].S*B[j-1].S;
if (x<dp[j]){
dp[j]=x;
T[j]=p.S+1;
}
}
if (T[t]<k){
// cout << mid << endl;
ans=min(ans,dp[t]-T[t]*mid);
rr=mid;
}
else l=mid;
}
return ans;
}
/*
int32_t main() {
int n, m, k;
assert(3 == scanf("%d %d %d", &n, &m, &k));
std::vector<int32_t> 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;
}
*/
/*
5 7 2
0 3
4 4
4 6
4 5
4 6
*/
//take_photos(5, 7, 2, [0, 4, 4, 4, 4], [3, 4, 6, 5, 6])
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
2 |
Correct |
5 ms |
8496 KB |
Correct answer: answer = 4 |
3 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
4 |
Correct |
5 ms |
8440 KB |
Correct answer: answer = 12 |
5 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 52 |
6 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 210 |
7 |
Correct |
5 ms |
8512 KB |
Correct answer: answer = 88 |
8 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 7696 |
9 |
Correct |
5 ms |
8408 KB |
Correct answer: answer = 1 |
10 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 2374 |
11 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 9502 |
12 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 49 |
13 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 151 |
14 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
15 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7220 |
16 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
17 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
18 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
19 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 624 |
20 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 1 |
2 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
3 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 1 |
4 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 5 |
5 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 41 |
6 |
Correct |
5 ms |
8516 KB |
Correct answer: answer = 71923 |
7 |
Correct |
5 ms |
8464 KB |
Correct answer: answer = 77137 |
8 |
Incorrect |
6 ms |
8524 KB |
Wrong answer: output = 933, expected = 764 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
2 |
Correct |
5 ms |
8496 KB |
Correct answer: answer = 4 |
3 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
4 |
Correct |
5 ms |
8440 KB |
Correct answer: answer = 12 |
5 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 52 |
6 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 210 |
7 |
Correct |
5 ms |
8512 KB |
Correct answer: answer = 88 |
8 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 7696 |
9 |
Correct |
5 ms |
8408 KB |
Correct answer: answer = 1 |
10 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 2374 |
11 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 9502 |
12 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 49 |
13 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 151 |
14 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
15 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7220 |
16 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
17 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
18 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
19 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 624 |
20 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
21 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 1 |
22 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
23 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 1 |
24 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 5 |
25 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 41 |
26 |
Correct |
5 ms |
8516 KB |
Correct answer: answer = 71923 |
27 |
Correct |
5 ms |
8464 KB |
Correct answer: answer = 77137 |
28 |
Incorrect |
6 ms |
8524 KB |
Wrong answer: output = 933, expected = 764 |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
2 |
Correct |
5 ms |
8496 KB |
Correct answer: answer = 4 |
3 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
4 |
Correct |
5 ms |
8440 KB |
Correct answer: answer = 12 |
5 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 52 |
6 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 210 |
7 |
Correct |
5 ms |
8512 KB |
Correct answer: answer = 88 |
8 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 7696 |
9 |
Correct |
5 ms |
8408 KB |
Correct answer: answer = 1 |
10 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 2374 |
11 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 9502 |
12 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 49 |
13 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 151 |
14 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
15 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7220 |
16 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
17 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
18 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
19 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 624 |
20 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
21 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 1 |
22 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
23 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 1 |
24 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 5 |
25 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 41 |
26 |
Correct |
5 ms |
8516 KB |
Correct answer: answer = 71923 |
27 |
Correct |
5 ms |
8464 KB |
Correct answer: answer = 77137 |
28 |
Incorrect |
6 ms |
8524 KB |
Wrong answer: output = 933, expected = 764 |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
2 |
Correct |
5 ms |
8496 KB |
Correct answer: answer = 4 |
3 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
4 |
Correct |
5 ms |
8440 KB |
Correct answer: answer = 12 |
5 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 52 |
6 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 210 |
7 |
Correct |
5 ms |
8512 KB |
Correct answer: answer = 88 |
8 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 7696 |
9 |
Correct |
5 ms |
8408 KB |
Correct answer: answer = 1 |
10 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 2374 |
11 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 9502 |
12 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 49 |
13 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 151 |
14 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
15 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7220 |
16 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
17 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
18 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
19 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 624 |
20 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
21 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 1 |
22 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
23 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 1 |
24 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 5 |
25 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 41 |
26 |
Correct |
5 ms |
8516 KB |
Correct answer: answer = 71923 |
27 |
Correct |
5 ms |
8464 KB |
Correct answer: answer = 77137 |
28 |
Incorrect |
6 ms |
8524 KB |
Wrong answer: output = 933, expected = 764 |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
2 |
Correct |
5 ms |
8496 KB |
Correct answer: answer = 4 |
3 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
4 |
Correct |
5 ms |
8440 KB |
Correct answer: answer = 12 |
5 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 52 |
6 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 210 |
7 |
Correct |
5 ms |
8512 KB |
Correct answer: answer = 88 |
8 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 7696 |
9 |
Correct |
5 ms |
8408 KB |
Correct answer: answer = 1 |
10 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 2374 |
11 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 9502 |
12 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 49 |
13 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 151 |
14 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
15 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7220 |
16 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 7550 |
17 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
18 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
19 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 624 |
20 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 10000 |
21 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 1 |
22 |
Correct |
5 ms |
8524 KB |
Correct answer: answer = 4 |
23 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 1 |
24 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 5 |
25 |
Correct |
5 ms |
8396 KB |
Correct answer: answer = 41 |
26 |
Correct |
5 ms |
8516 KB |
Correct answer: answer = 71923 |
27 |
Correct |
5 ms |
8464 KB |
Correct answer: answer = 77137 |
28 |
Incorrect |
6 ms |
8524 KB |
Wrong answer: output = 933, expected = 764 |
29 |
Halted |
0 ms |
0 KB |
- |