답안 #237170

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
237170 2020-06-05T03:57:00 Z xiaowuc1 Bulldozer (JOI17_bulldozer) C++14
100 / 100
1512 ms 49968 KB
#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];
  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++) {
      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);
      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();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 704 KB Output is correct
2 Correct 6 ms 704 KB Output is correct
3 Correct 5 ms 704 KB Output is correct
4 Correct 5 ms 704 KB Output is correct
5 Correct 5 ms 704 KB Output is correct
6 Correct 5 ms 704 KB Output is correct
7 Correct 5 ms 704 KB Output is correct
8 Correct 5 ms 704 KB Output is correct
9 Correct 5 ms 704 KB Output is correct
10 Correct 5 ms 704 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 704 KB Output is correct
2 Correct 11 ms 704 KB Output is correct
3 Correct 8 ms 704 KB Output is correct
4 Correct 10 ms 704 KB Output is correct
5 Correct 8 ms 704 KB Output is correct
6 Correct 8 ms 704 KB Output is correct
7 Correct 8 ms 704 KB Output is correct
8 Correct 8 ms 704 KB Output is correct
9 Correct 10 ms 704 KB Output is correct
10 Correct 8 ms 704 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 8 ms 704 KB Output is correct
22 Correct 8 ms 704 KB Output is correct
23 Correct 8 ms 704 KB Output is correct
24 Correct 8 ms 704 KB Output is correct
25 Correct 8 ms 704 KB Output is correct
26 Correct 8 ms 704 KB Output is correct
27 Correct 8 ms 704 KB Output is correct
28 Correct 8 ms 704 KB Output is correct
29 Correct 8 ms 704 KB Output is correct
30 Correct 8 ms 704 KB Output is correct
31 Correct 8 ms 704 KB Output is correct
32 Correct 8 ms 704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 704 KB Output is correct
2 Correct 11 ms 704 KB Output is correct
3 Correct 8 ms 704 KB Output is correct
4 Correct 10 ms 704 KB Output is correct
5 Correct 8 ms 704 KB Output is correct
6 Correct 8 ms 704 KB Output is correct
7 Correct 8 ms 704 KB Output is correct
8 Correct 8 ms 704 KB Output is correct
9 Correct 10 ms 704 KB Output is correct
10 Correct 8 ms 704 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 8 ms 704 KB Output is correct
22 Correct 8 ms 704 KB Output is correct
23 Correct 8 ms 704 KB Output is correct
24 Correct 8 ms 704 KB Output is correct
25 Correct 8 ms 704 KB Output is correct
26 Correct 8 ms 704 KB Output is correct
27 Correct 8 ms 704 KB Output is correct
28 Correct 8 ms 704 KB Output is correct
29 Correct 8 ms 704 KB Output is correct
30 Correct 8 ms 704 KB Output is correct
31 Correct 8 ms 704 KB Output is correct
32 Correct 8 ms 704 KB Output is correct
33 Correct 1464 ms 49808 KB Output is correct
34 Correct 1451 ms 49836 KB Output is correct
35 Correct 1454 ms 49704 KB Output is correct
36 Correct 1467 ms 49856 KB Output is correct
37 Correct 1470 ms 49840 KB Output is correct
38 Correct 1494 ms 49836 KB Output is correct
39 Correct 1458 ms 49812 KB Output is correct
40 Correct 1448 ms 49708 KB Output is correct
41 Correct 1486 ms 49840 KB Output is correct
42 Correct 1436 ms 49836 KB Output is correct
43 Correct 1410 ms 49708 KB Output is correct
44 Correct 1374 ms 49964 KB Output is correct
45 Correct 1512 ms 49820 KB Output is correct
46 Correct 1412 ms 49964 KB Output is correct
47 Correct 1389 ms 49708 KB Output is correct
48 Correct 1428 ms 49708 KB Output is correct
49 Correct 1433 ms 49708 KB Output is correct
50 Correct 1440 ms 49708 KB Output is correct
51 Correct 1439 ms 49708 KB Output is correct
52 Correct 1382 ms 49740 KB Output is correct
53 Correct 1382 ms 49708 KB Output is correct
54 Correct 1383 ms 49740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 704 KB Output is correct
2 Correct 11 ms 704 KB Output is correct
3 Correct 8 ms 704 KB Output is correct
4 Correct 10 ms 704 KB Output is correct
5 Correct 8 ms 704 KB Output is correct
6 Correct 8 ms 704 KB Output is correct
7 Correct 8 ms 704 KB Output is correct
8 Correct 8 ms 704 KB Output is correct
9 Correct 10 ms 704 KB Output is correct
10 Correct 8 ms 704 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 8 ms 704 KB Output is correct
22 Correct 8 ms 704 KB Output is correct
23 Correct 8 ms 704 KB Output is correct
24 Correct 8 ms 704 KB Output is correct
25 Correct 8 ms 704 KB Output is correct
26 Correct 8 ms 704 KB Output is correct
27 Correct 8 ms 704 KB Output is correct
28 Correct 8 ms 704 KB Output is correct
29 Correct 8 ms 704 KB Output is correct
30 Correct 8 ms 704 KB Output is correct
31 Correct 8 ms 704 KB Output is correct
32 Correct 8 ms 704 KB Output is correct
33 Correct 1464 ms 49808 KB Output is correct
34 Correct 1451 ms 49836 KB Output is correct
35 Correct 1454 ms 49704 KB Output is correct
36 Correct 1467 ms 49856 KB Output is correct
37 Correct 1470 ms 49840 KB Output is correct
38 Correct 1494 ms 49836 KB Output is correct
39 Correct 1458 ms 49812 KB Output is correct
40 Correct 1448 ms 49708 KB Output is correct
41 Correct 1486 ms 49840 KB Output is correct
42 Correct 1436 ms 49836 KB Output is correct
43 Correct 1410 ms 49708 KB Output is correct
44 Correct 1374 ms 49964 KB Output is correct
45 Correct 1512 ms 49820 KB Output is correct
46 Correct 1412 ms 49964 KB Output is correct
47 Correct 1389 ms 49708 KB Output is correct
48 Correct 1428 ms 49708 KB Output is correct
49 Correct 1433 ms 49708 KB Output is correct
50 Correct 1440 ms 49708 KB Output is correct
51 Correct 1439 ms 49708 KB Output is correct
52 Correct 1382 ms 49740 KB Output is correct
53 Correct 1382 ms 49708 KB Output is correct
54 Correct 1383 ms 49740 KB Output is correct
55 Correct 1431 ms 49708 KB Output is correct
56 Correct 1429 ms 49836 KB Output is correct
57 Correct 1435 ms 49708 KB Output is correct
58 Correct 1442 ms 49916 KB Output is correct
59 Correct 1427 ms 49712 KB Output is correct
60 Correct 1430 ms 49708 KB Output is correct
61 Correct 1448 ms 49708 KB Output is correct
62 Correct 1444 ms 49844 KB Output is correct
63 Correct 1440 ms 49836 KB Output is correct
64 Correct 1447 ms 49832 KB Output is correct
65 Correct 1445 ms 49708 KB Output is correct
66 Correct 1435 ms 49836 KB Output is correct
67 Correct 1445 ms 49708 KB Output is correct
68 Correct 1438 ms 49968 KB Output is correct
69 Correct 1438 ms 49852 KB Output is correct
70 Correct 1421 ms 49708 KB Output is correct
71 Correct 1458 ms 49808 KB Output is correct
72 Correct 1455 ms 49836 KB Output is correct
73 Correct 1452 ms 49708 KB Output is correct
74 Correct 1444 ms 49836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 704 KB Output is correct
2 Correct 6 ms 704 KB Output is correct
3 Correct 5 ms 704 KB Output is correct
4 Correct 5 ms 704 KB Output is correct
5 Correct 5 ms 704 KB Output is correct
6 Correct 5 ms 704 KB Output is correct
7 Correct 5 ms 704 KB Output is correct
8 Correct 5 ms 704 KB Output is correct
9 Correct 5 ms 704 KB Output is correct
10 Correct 5 ms 704 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 9 ms 704 KB Output is correct
17 Correct 11 ms 704 KB Output is correct
18 Correct 8 ms 704 KB Output is correct
19 Correct 10 ms 704 KB Output is correct
20 Correct 8 ms 704 KB Output is correct
21 Correct 8 ms 704 KB Output is correct
22 Correct 8 ms 704 KB Output is correct
23 Correct 8 ms 704 KB Output is correct
24 Correct 10 ms 704 KB Output is correct
25 Correct 8 ms 704 KB Output is correct
26 Correct 4 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 4 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 5 ms 384 KB Output is correct
35 Correct 5 ms 384 KB Output is correct
36 Correct 8 ms 704 KB Output is correct
37 Correct 8 ms 704 KB Output is correct
38 Correct 8 ms 704 KB Output is correct
39 Correct 8 ms 704 KB Output is correct
40 Correct 8 ms 704 KB Output is correct
41 Correct 8 ms 704 KB Output is correct
42 Correct 8 ms 704 KB Output is correct
43 Correct 8 ms 704 KB Output is correct
44 Correct 8 ms 704 KB Output is correct
45 Correct 8 ms 704 KB Output is correct
46 Correct 8 ms 704 KB Output is correct
47 Correct 8 ms 704 KB Output is correct
48 Correct 1464 ms 49808 KB Output is correct
49 Correct 1451 ms 49836 KB Output is correct
50 Correct 1454 ms 49704 KB Output is correct
51 Correct 1467 ms 49856 KB Output is correct
52 Correct 1470 ms 49840 KB Output is correct
53 Correct 1494 ms 49836 KB Output is correct
54 Correct 1458 ms 49812 KB Output is correct
55 Correct 1448 ms 49708 KB Output is correct
56 Correct 1486 ms 49840 KB Output is correct
57 Correct 1436 ms 49836 KB Output is correct
58 Correct 1410 ms 49708 KB Output is correct
59 Correct 1374 ms 49964 KB Output is correct
60 Correct 1512 ms 49820 KB Output is correct
61 Correct 1412 ms 49964 KB Output is correct
62 Correct 1389 ms 49708 KB Output is correct
63 Correct 1428 ms 49708 KB Output is correct
64 Correct 1433 ms 49708 KB Output is correct
65 Correct 1440 ms 49708 KB Output is correct
66 Correct 1439 ms 49708 KB Output is correct
67 Correct 1382 ms 49740 KB Output is correct
68 Correct 1382 ms 49708 KB Output is correct
69 Correct 1383 ms 49740 KB Output is correct
70 Correct 1431 ms 49708 KB Output is correct
71 Correct 1429 ms 49836 KB Output is correct
72 Correct 1435 ms 49708 KB Output is correct
73 Correct 1442 ms 49916 KB Output is correct
74 Correct 1427 ms 49712 KB Output is correct
75 Correct 1430 ms 49708 KB Output is correct
76 Correct 1448 ms 49708 KB Output is correct
77 Correct 1444 ms 49844 KB Output is correct
78 Correct 1440 ms 49836 KB Output is correct
79 Correct 1447 ms 49832 KB Output is correct
80 Correct 1445 ms 49708 KB Output is correct
81 Correct 1435 ms 49836 KB Output is correct
82 Correct 1445 ms 49708 KB Output is correct
83 Correct 1438 ms 49968 KB Output is correct
84 Correct 1438 ms 49852 KB Output is correct
85 Correct 1421 ms 49708 KB Output is correct
86 Correct 1458 ms 49808 KB Output is correct
87 Correct 1455 ms 49836 KB Output is correct
88 Correct 1452 ms 49708 KB Output is correct
89 Correct 1444 ms 49836 KB Output is correct
90 Correct 1451 ms 49832 KB Output is correct
91 Correct 1452 ms 49944 KB Output is correct
92 Correct 1427 ms 49800 KB Output is correct
93 Correct 1445 ms 49768 KB Output is correct
94 Correct 1439 ms 49832 KB Output is correct
95 Correct 1448 ms 49708 KB Output is correct
96 Correct 1427 ms 49876 KB Output is correct
97 Correct 1441 ms 49964 KB Output is correct
98 Correct 1446 ms 49836 KB Output is correct
99 Correct 1437 ms 49844 KB Output is correct
100 Correct 751 ms 49732 KB Output is correct
101 Correct 732 ms 49864 KB Output is correct
102 Correct 724 ms 49732 KB Output is correct
103 Correct 730 ms 49836 KB Output is correct
104 Correct 723 ms 49708 KB Output is correct
105 Correct 917 ms 49708 KB Output is correct
106 Correct 939 ms 49708 KB Output is correct
107 Correct 925 ms 49708 KB Output is correct
108 Correct 917 ms 49840 KB Output is correct
109 Correct 917 ms 49708 KB Output is correct
110 Correct 1080 ms 49840 KB Output is correct
111 Correct 1102 ms 49840 KB Output is correct
112 Correct 1097 ms 49780 KB Output is correct
113 Correct 1091 ms 49712 KB Output is correct
114 Correct 1081 ms 49784 KB Output is correct
115 Correct 1088 ms 49708 KB Output is correct
116 Correct 1088 ms 49712 KB Output is correct
117 Correct 1087 ms 49968 KB Output is correct
118 Correct 1094 ms 49708 KB Output is correct
119 Correct 1084 ms 49708 KB Output is correct
120 Correct 5 ms 384 KB Output is correct
121 Correct 4 ms 384 KB Output is correct
122 Correct 1344 ms 49832 KB Output is correct
123 Correct 1340 ms 49836 KB Output is correct
124 Correct 1337 ms 49836 KB Output is correct
125 Correct 1320 ms 49816 KB Output is correct
126 Correct 1350 ms 49840 KB Output is correct
127 Correct 1333 ms 49708 KB Output is correct
128 Correct 1330 ms 49708 KB Output is correct
129 Correct 1332 ms 49712 KB Output is correct
130 Correct 1339 ms 49708 KB Output is correct
131 Correct 1340 ms 49836 KB Output is correct
132 Correct 1313 ms 49836 KB Output is correct
133 Correct 1322 ms 49880 KB Output is correct