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;
int g;
inline long long get(long long a1,long long b1,long long a2,long long b2){
tmp1 = (a1 - a2) / (b2 - b1) + 1;
while(a1 * tmp1 + b1 > a2 * tmp1 + b2)
tmp1--;
tmp1 = max(tmp1 , 0LL);
tmp1 = min(tmp1 , (long long)m);
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<int,int> tmp;
for(int j = k - 1;j >= 0;j--){
deque < pair<int,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 < 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 = m; else g = 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:45:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(i < n - 1 && arr[i].second >= arr[i + 1].first)
^~
aliens.cpp:47: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);
^
aliens.cpp:79:41: warning: operation on 'g' may be undefined [-Wsequence-point]
if((int)q.size() == 0) g = m; else g = g = get(a[i] , tmpb[i] , a[q.front().second] , tmpb[q.front().second]);
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |