This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* made by amunduzbaev */
#include "aliens.h"
//~ #include "grader.cpp"
#include "bits/stdc++.h"
using namespace std;
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template<class T> using oset = tree<T,
//~ null_type, less_equal<T>, rb_tree_tag,
//~ tree_order_statistics_node_update>;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define mem(arr, v) memset(arr, v, sizeof arr)
#define int long long
#define degub(x) cout<<#x<<" : "<<x<<"\n"
#define GG cout<<"here"<<endl;
//~ void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
//~ freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, true : false; }
template<int sz> using tut = array<int, sz>;
//~ for P. Pankov problems
//~ const int mod = 2021;
//~ const int mod = (119 << 23)+1;
const int N = 5e4+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
const int MX = 1e6+5;
#define MULTI 0
int n, m, k, t, q, ans, res, a[N];
int x[N], y[N];
bool cmp(tut<3> a, tut<3> b){
if(a[1] != b[1]) return (a[1] > b[1]);
return (a[0] < b[0]);
}
struct TREE{
int tree[4*MX];
void sett(int i, int v, int lx = 0, int rx = MX, int x = 1){
if(lx == rx) { tree[x] = v; return; }
int m = (lx + rx)>>1;
if(i <= m) sett(i, v, lx, m, x<<1);
else sett(i, v, m+1, rx, x<<1|1);
tree[x] = tree[x<<1] + tree[x<<1|1];
}
int get(int l, int r, int lx = 0, int rx = MX, int x = 1){
if(lx > r || rx < l) return 0;
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx)>>1;
return get(l, r, lx, m, x<<1) + get(l, r, m+1, rx, x<<1|1);
}
}tree;
int dp[N][105];
vpii pp;
int sq(int a) { return a * a; }
//~ void dnc(int l, int r, int lx, int rx){
//~ if(l > r) return;
//~ int m = (l + r)>>1, op = min(rx, m);
//~ dp[m][k] = inf;
//~ for(int i=min(rx, m);i>=lx;i--){
//~ int in = 0;
//~ if(pp[i-1].ss >= pp[i].ff) in = sq(pp[i-1].ss - pp[i].ff + 1);
//~ if(umin(dp[m][k], dp[i-1][k-1] + sq(pp[m].ss - pp[i].ff + 1) - in)) op = i;
//~ }
//~ dnc(l, m-1, lx, op);
//~ dnc(m+1, r, op, rx);
//~ }
#define i32 int32_t
int take_photos(i32 N, i32 M, i32 K, std::vector<i32> r, std::vector<i32> c){
n = N, m = M, k = K;
for(int i=1;i<=n;i++){
x[i] = r[i-1], y[i] = c[i-1];
if(x[i] > y[i]) swap(x[i], y[i]);
}
vpii tt;
for(int i=1;i<=n;i++) tt.pb({x[i], y[i]});
sort(all(tt), [&](pii a, pii b){
if(a.ss != b.ss) return a.ss > b.ss;
return a.ff < b.ff;
});
for(auto x : tt){
if(tree.get(0, x.ff)) continue;
tree.sett(x.ff, 1); pp.pb(x);
}
sort(all(pp));
n = sz(pp);
for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dp[i][j] = (int)(1e12);
for(int i=0;i<n;i++) dp[i][0] = sq(pp[i].ss - pp[0].ff + 1);
umin(k, n);
K = k;
//~ GG
for(k=1;k<K;k++){
for(int j=0;j<n;j++){
for(int l=0;l<j;l++){
int in = 0;
if(pp[l+1].ff <= pp[l].ss) in = sq(pp[l].ss - pp[l+1].ff + 1);
umin(dp[j][k], dp[l][k-1] + sq(pp[j].ss - pp[l+1].ff + 1) - in);
}
}
}
res = inf;
for(int i=0;i<K;i++) umin(res, dp[n-1][i]);
return res;
}
/*
2 6 2
1 4
4 1
4 7 2
0 3
4 6
4 3
4 5
*/
//~ signed main(){
//~ NeedForSpeed
//~ i32 n, m, k; cin>>n>>m>>k;
//~ vv<i32> r, c;
//~ for(int i=0;i<n;i++){
//~ int x, y; cin>>x>>y;
//~ r.pb(x), c.pb(y);
//~ }
//~ cout<<take_photos(n, m, k, r, c)<<"\n";
//~ return 0;
//~ }
# | 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... |