이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
// END NO SAD
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<ll>> matrix;
typedef pair<ll, ll> pll;
const int KOOSAGA_SZ = 1 << 11;
ll koosagap[2 * KOOSAGA_SZ];
ll koosagas[2 * KOOSAGA_SZ];
ll koosagam[2 * KOOSAGA_SZ];
ll koosagat[2 * KOOSAGA_SZ];
void upd(int idx, ll val) {
idx += KOOSAGA_SZ;
koosagat[idx] = val;
koosagap[idx] = val;
koosagas[idx] = val;
koosagam[idx] = val;
while(idx > 1) {
idx /= 2;
koosagap[idx] = max(koosagap[2*idx], koosagat[2*idx] + koosagap[2*idx+1]);
koosagas[idx] = max(koosagas[2*idx+1], koosagat[2*idx+1] + koosagas[2*idx]);
koosagat[idx] = koosagat[2*idx] + koosagat[2*idx+1];
koosagam[idx] = max(koosagam[2*idx], koosagam[2*idx+1]);
koosagam[idx] = max(koosagam[idx], koosagas[2*idx] + koosagap[2*idx+1]);
}
}
struct state {
int x, y, v, i;
};
bool operator<(state a, state b) {
return pii(a.x, a.y) < pii(b.x, b.y);
}
struct event {
ll dx, dy;
int i, j;
event(){}
event(ll a, ll b, int c, int d) {
ll gcd = __gcd(a, b);
a /= gcd;
b /= gcd;
if(a < 0 || (a == 0 && b < 0)) {
a = -a;
b = -b;
}
dx=a;
dy=b;
i=c;
j=d;
}
};
bool operator<(event a, event b) {
ll ret = a.dx * b.dy - a.dy * b.dx;
return ret > 0;
}
state l[KOOSAGA_SZ];
int loc[KOOSAGA_SZ]; // item i should be at loc[i]
int n;
int par[KOOSAGA_SZ];
int lastid[KOOSAGA_SZ];
int seen[KOOSAGA_SZ];
int currid;
int find(int x) {
if(lastid[x] != currid) {
lastid[x] = currid;
par[x] = x;
}
return par[x] == x ? x : par[x] = find(par[x]);
}
void merge(int x, int y) {
par[find(x)] = find(y);
}
bool locsort(int a, int b) {
return loc[a] < loc[b];
}
void realswap(int a, int b) {
assert(l[loc[a]].i == a);
assert(l[loc[b]].i == b);
swap(l[loc[a]], l[loc[b]]);
swap(loc[a], loc[b]);
upd(loc[a], l[loc[a]].v);
upd(loc[b], l[loc[b]].v);
}
void solve() {
cin >> n;
for(int i = 0; i < n; i++) {
cin >> l[i].x >> l[i].y >> l[i].v;
l[i].i = i;
}
vector<event> v;
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
event e(l[j].x - l[i].x, l[j].y - l[i].y, i, j);
v.push_back(e);
}
}
sort(l, l+n);
sort(all(v));
for(int i = 0; i < n; i++) {
loc[l[i].i] = i;
}
for(int i = 0; i < n; i++) assert(loc[l[i].i] == i);
for(int i = 0; i < n; i++) {
upd(i, l[i].v);
}
ll ret = koosagam[1];
if(n==2000)exit(0);
for(int i = 0; i < sz(v);) {
currid++;
int j = i+1;
while(j < sz(v) && v[i].dx == v[j].dx && v[i].dy == v[j].dy) {
j++;
}
for(int k = i; k < j; k++) {
//cout<<v[k].i<<" "<<v[k].j<<"\n";
merge(v[k].i, v[k].j);
}
map<int, vector<int>> dp;
for(int k = i; k < j; k++) {
if(seen[v[k].i] != currid) {
seen[v[k].i] = currid;
dp[find(v[k].i)].push_back(v[k].i);
}
if(seen[v[k].j] != currid) {
seen[v[k].j] = currid;
dp[find(v[k].j)].push_back(v[k].j);
}
}
for(auto out: dp) {
sort(all(out.second), locsort);
for(int k: out.second) {
//cout<<k<<" ";
}
//cout<<"\n";
int lhs = 0;
int rhs = sz(out.second)-1;
while(lhs < rhs) {
realswap(out.second[lhs++], out.second[rhs--]);
}
}
ret = max(ret, koosagam[1]);
i = j;
}
cout << ret << "\n";
}
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
bulldozer.cpp: In function 'void solve()':
bulldozer.cpp:159:15: warning: unused variable 'k' [-Wunused-variable]
159 | for(int k: out.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... |