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>
#include "aliens.h"
using namespace std;
const int N = 4010 , K = 4010;
vector< pair<int,int> > arr , tmp;
long long dp[N][K];
int n , m , k;
long long a[N] , b[N] , c[N] , tmpb[N];
long long tmp1 , g;
inline long long get(long long a1,long long b1,long long a2,long long b2){
assert(a1 > a2);
tmp1 = (b2 - b1) / (a1 - a2) - ((((b2 - b2) < 0) ^ ((a1 - a2) < 0) && (abs(b2 - b1) % abs(b1 - a2) != 0)) ? 1 : 0);
return tmp1;
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c){
for(int i = 0 ;i < n;i++){
arr.push_back(make_pair(min(r[i] , c[i]) , -max(r[i] , c[i])));
}
sort(arr.begin(),arr.end());
int mx = -1;
for(int i = 0 ;i < (int)arr.size();i++){
arr[i].second = -arr[i].second;
if(arr[i].second > mx){
tmp.push_back(arr[i]);
mx = arr[i].second;
}
}
arr = tmp;
n = (int)arr.size();
::n = n;
::m = m;
::k = k;
for(int i = 0 ;i < n;i++){
b[i] = (long long)(arr[i].second + 1) * (long long)(arr[i].second + 1);
if(i < n - 1 && arr[i].second >= arr[i + 1].first)
b[i] -= (long long)(arr[i].second - arr[i + 1].first + 1) * (long long)(arr[i].second - arr[i + 1].first + 1);
a[i] = -2 * (long long)(arr[i].second + 1);
c[i] = (long long)arr[i].first * (long long)arr[i].first;
}
for(int i = 0 ;i < n;i++){
dp[i][k] = (long long)1e18;
}
pair<long long,int> tmp;
for(int j = k - 1;j >= 0;j--){
deque < pair<long long,int> > q;
for(int i = n - 1;i >= 0 ;i--){
tmpb[i] = dp[i + 1][j + 1] + b[i];
dp[i][j] = c[i] + a[i] * (long long)arr[i].first + tmpb[i];
while((int)q.size() > 1){
tmp = q.back();
q.pop_back();
if(q.back().first < (long long)arr[i].first){
q.push_back(tmp);
break;
}
}
if((int)q.size() > 0){
tmp = q.back();
dp[i][j] = min(dp[i][j] , c[i] + a[tmp.second] * (long long)arr[i].first + tmpb[tmp.second]);
}
while((int)q.size() > 0){
g = get(a[i] , tmpb[i] , a[q.front().second] , tmpb[q.front().second]);
if(g >= q.front().first){
q.pop_front();
continue;
}
break;
}
if((int)q.size() == 0) g = (long long)1e18; else g = get(a[i] , tmpb[i] , a[q.front().second] , tmpb[q.front().second]);
q.push_front(make_pair(g , i));
}
}
return dp[0][0];
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:40:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(i < n - 1 && arr[i].second >= arr[i + 1].first)
^~
aliens.cpp:42:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
a[i] = -2 * (long long)(arr[i].second + 1);
^
# | 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... |