이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <iomanip>
using namespace std;
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\n'
using lint = long long;
using ulint = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using vi = vector<int>;
using vl = vector<lint>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
const int mod = (int)1e9 + 7;
vector<pair<lint,lint>> unq;
vl W(200000);
struct CHT{
struct Line{
lint m,y;
Line(lint a, lint b): m(a), y(b){}
lint eval(lint x){
return x * m + y;
}
lint intercept(Line& b){
//m2x + c2 = m1x + c1
//x * (m2 - m1) = c1 - c2
//x = (c1 - c2) / (m2 - m1)
return (y - b.y + (b.m - m - 1)) / (b.m - m);
}
};
deque<pair<Line,pair<lint,lint>>>dq;
void insert(lint m, lint y, int c){
Line New_Line(m,y);
while(dq.size() > 1){
if (dq[dq.size() - 2].fr.intercept(New_Line) <= dq.back().sc.fr){
dq.pop_back();
}else{
break;
}
}
if (dq.empty()){
dq.push_back({New_Line, {-(lint)1e14,c}});
}else{
dq.push_back({New_Line, {New_Line.intercept(dq.back().fr),c}});
}
}
pair<lint,lint>query(lint x){
while(dq.size() > 1 && dq[1].sc.fr <= x)dq.pop_front();
return {dq[0].fr.eval(x), dq[0].sc.sc + 1};
}
};
pair<lint,lint> Lagrangian_Relaxation(lint lambda){
pair<lint,lint> dp[unq.size()];
CHT cht;
rep(i,(int)unq.size()){
if(i){
cht.insert(-2 * unq[i].fr, unq[i].fr * unq[i].fr - W[i-1] * W[i - 1] - 2 * unq[i].fr + dp[i - 1].fr + lambda, dp[i-1].sc);
dp[i] = cht.query(unq[i].sc);
dp[i].fr += 1 + 2 * unq[i].sc + unq[i].sc * unq[i].sc;
}else{
dp[i].fr = lambda + (unq[i].second - unq[i].first + 1) * (unq[i].second - unq[i].first + 1);
dp[i].sc = 1;
}
}
return dp[unq.size() - 1];
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
vector<pair<lint,lint>> Segments;
rep(i,n){
Segments.push_back(mp(min(r[i], c[i]), max(r[i], c[i])));
}
sort(all(Segments), [&](auto x, auto y){
if (x.fr < y.fr)return true;
else if (x.fr == y.fr)return x.sc > y.sc;
else return false;
});
int most_right = -1;
unq.pb(Segments[0]);
for (int i = 1; i < n; i++) {
if (!(unq.back().fr <= Segments[i].first && Segments[i].second <= unq.back().sc)) {
unq.pb(Segments[i]);
}
}
rep1(i,(int)unq.size()){
W[i - 1] = 0;
if (unq[i - 1].sc - unq[i].fr >= 0)W[i-1] =unq[i - 1].sc - unq[i].fr + 1;
}
W[unq.size() - 1] = 0;
lint L = 0, R = (lint)1e12, a = 0;
while(L <= R){
lint mid = (L+R) / 2;
if (Lagrangian_Relaxation(mid).sc <= k){
a = mid;
R = mid - 1;
}else{
L = mid + 1;
}
}
auto ans = Lagrangian_Relaxation(a);
return ans.fr - (a) * ans.sc;
}
컴파일 시 표준 에러 (stderr) 메시지
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:137:9: warning: unused variable 'most_right' [-Wunused-variable]
137 | int most_right = -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... |