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 "aliens.h"
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
//#define iceil(a, b) (((a) + (b) - 1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
int n, m;
struct Line{
ll a, b, t;
ll l, r;
Line(ll a = 0, ll b = 1LL << 60, ll t = 0, ll l = 0, ll r = m - 1): a(a), b(b), t(t), l(l), r(r) {}
pll operator()(ll x){
return mp(a * x + b, t);
}
};
ostream& operator<<(ostream& o, Line ln){
return o << '(' << ln.a << ',' << ln.b << ',' << ln.t << ',' << ln.l << ',' << ln.r << ')';
}
vector<pll> a;
pll operator+(pll a, pll b){
return mp(a.F + b.F, a.S + b.S);
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a >= 0) return a / b;
else return (a - b + 1) / b;
}
ll intersect(Line a, Line b){
return iceil(b.b - a.b, a.a - b.a);
}
pll calc(ll p){
// cerr << "calc " << p << "\n";
vector<pll> dp(n);
deque<Line> dq;
dq.eb(Line(-2 * a[0].F, a[0].F * a[0].F, 0));
// cerr << "add " << -2 * a[0].F << " " << a[0].F * a[0].F << " " << 0 << "\n";
for(int i = 0; i < n; i++){
while(dq.front().r < a[i].S) dq.pof;
pll tmp = dq.front()(a[i].S);
// cerr << i << " " << tmp << "\n";
dp[i] = tmp + mp(a[i].S * a[i].S + p, 1);
if(i == n - 1) continue;
// cerr << "add " << -2 * a[i + 1].F << " " << a[i + 1].F * a[i + 1].F + dp[i].F - max(0LL, a[i].S - a[i + 1].F) * max(0LL, a[i].S - a[i + 1].F) << " " << dp[i].S << "\n";
Line ln = Line(-2 * a[i + 1].F,
a[i + 1].F * a[i + 1].F + dp[i].F - max(0LL, a[i].S - a[i + 1].F) * max(0LL, a[i].S - a[i + 1].F),
dp[i].S);
while(!dq.empty() && dq.back()(dq.back().l) >= ln(dq.back().l)) dq.pob;
if(!dq.empty()){
if(dq.back()(dq.back().r) <= ln(dq.back().r)){
if(dq.back().r < m - 1){
ln.l = dq.back().r + 1;
dq.eb(ln);
}
continue;
}
ll ip = intersect(ln, dq.back());
dq.back().r = ip;
ln.l = ip + 1;
dq.eb(ln);
}
else dq.eb(ln);
// printv(dq, cerr);
}
// printv(dp, cerr);
return dp[n - 1];
}
ll take_photos(int N, int M, int k, vector<int> R, vector<int> C){
n = N;
m = M;
multiset<int> st;
vector<vector<int>> e(m);
for(int i = 0; i < n; i++){
if(R[i] <= C[i]){
e[R[i]].eb(MAX);
e[C[i]].eb(R[i]);
}
else{
e[C[i]].eb(MAX);
e[R[i]].eb(C[i]);
}
}
for(int i = 0; i < m; i++){
gsort(e[i]);
for(int j : e[i]){
if(j == MAX) st.insert(i);
else{
st.erase(st.find(j));
if(!st.empty() && *st.begin() <= j) continue;
a.eb(mp(j - 1, i));
}
}
}
n = a.size();
// printv(a, cerr);
if(calc(0).S <= k){
// cerr << calc(0) << "\n";
return calc(0).F;
}
ll l = 0, r = (ll)m * m;
while(l < r){
ll mid = (l + r) / 2;
if(calc(mid).S <= k) r = mid;
else l = mid + 1;
}
// cerr << l << " " << calc(l) << "\n";
return calc(l).F - k * l;
}
# | 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... |