답안 #237169

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
237169 2020-06-05T03:55:10 Z xiaowuc1 Bulldozer (JOI17_bulldozer) C++14
100 / 100
1710 ms 50060 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 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);
    }
    set<int> seen;
    map<int, vector<int>> dp;
    for(int k = i; k < j; k++) {
      if(!seen.count(v[k].i))  {
        seen.insert(v[k].i);
        dp[find(v[k].i)].push_back(v[k].i);
      }
      if(!seen.count(v[k].j))  {
        seen.insert(v[k].j);
        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 5 ms 704 KB Output is correct
2 Correct 5 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 4 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 256 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 9 ms 704 KB Output is correct
3 Correct 10 ms 704 KB Output is correct
4 Correct 11 ms 832 KB Output is correct
5 Correct 9 ms 832 KB Output is correct
6 Correct 9 ms 704 KB Output is correct
7 Correct 9 ms 704 KB Output is correct
8 Correct 9 ms 704 KB Output is correct
9 Correct 8 ms 704 KB Output is correct
10 Correct 9 ms 704 KB Output is correct
11 Correct 5 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 4 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 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 9 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 9 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 9 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 9 ms 704 KB Output is correct
3 Correct 10 ms 704 KB Output is correct
4 Correct 11 ms 832 KB Output is correct
5 Correct 9 ms 832 KB Output is correct
6 Correct 9 ms 704 KB Output is correct
7 Correct 9 ms 704 KB Output is correct
8 Correct 9 ms 704 KB Output is correct
9 Correct 8 ms 704 KB Output is correct
10 Correct 9 ms 704 KB Output is correct
11 Correct 5 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 4 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 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 9 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 9 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 9 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 1637 ms 50060 KB Output is correct
34 Correct 1634 ms 49836 KB Output is correct
35 Correct 1628 ms 49836 KB Output is correct
36 Correct 1626 ms 49840 KB Output is correct
37 Correct 1626 ms 49844 KB Output is correct
38 Correct 1629 ms 49836 KB Output is correct
39 Correct 1627 ms 49836 KB Output is correct
40 Correct 1641 ms 49800 KB Output is correct
41 Correct 1636 ms 49840 KB Output is correct
42 Correct 1643 ms 49836 KB Output is correct
43 Correct 1596 ms 49836 KB Output is correct
44 Correct 1571 ms 49840 KB Output is correct
45 Correct 1566 ms 49836 KB Output is correct
46 Correct 1577 ms 49836 KB Output is correct
47 Correct 1591 ms 49836 KB Output is correct
48 Correct 1588 ms 49836 KB Output is correct
49 Correct 1594 ms 49836 KB Output is correct
50 Correct 1593 ms 49836 KB Output is correct
51 Correct 1607 ms 49928 KB Output is correct
52 Correct 1592 ms 49968 KB Output is correct
53 Correct 1581 ms 49836 KB Output is correct
54 Correct 1569 ms 49836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 704 KB Output is correct
2 Correct 9 ms 704 KB Output is correct
3 Correct 10 ms 704 KB Output is correct
4 Correct 11 ms 832 KB Output is correct
5 Correct 9 ms 832 KB Output is correct
6 Correct 9 ms 704 KB Output is correct
7 Correct 9 ms 704 KB Output is correct
8 Correct 9 ms 704 KB Output is correct
9 Correct 8 ms 704 KB Output is correct
10 Correct 9 ms 704 KB Output is correct
11 Correct 5 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 4 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 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 9 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 9 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 9 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 1637 ms 50060 KB Output is correct
34 Correct 1634 ms 49836 KB Output is correct
35 Correct 1628 ms 49836 KB Output is correct
36 Correct 1626 ms 49840 KB Output is correct
37 Correct 1626 ms 49844 KB Output is correct
38 Correct 1629 ms 49836 KB Output is correct
39 Correct 1627 ms 49836 KB Output is correct
40 Correct 1641 ms 49800 KB Output is correct
41 Correct 1636 ms 49840 KB Output is correct
42 Correct 1643 ms 49836 KB Output is correct
43 Correct 1596 ms 49836 KB Output is correct
44 Correct 1571 ms 49840 KB Output is correct
45 Correct 1566 ms 49836 KB Output is correct
46 Correct 1577 ms 49836 KB Output is correct
47 Correct 1591 ms 49836 KB Output is correct
48 Correct 1588 ms 49836 KB Output is correct
49 Correct 1594 ms 49836 KB Output is correct
50 Correct 1593 ms 49836 KB Output is correct
51 Correct 1607 ms 49928 KB Output is correct
52 Correct 1592 ms 49968 KB Output is correct
53 Correct 1581 ms 49836 KB Output is correct
54 Correct 1569 ms 49836 KB Output is correct
55 Correct 1638 ms 49812 KB Output is correct
56 Correct 1645 ms 49964 KB Output is correct
57 Correct 1634 ms 49836 KB Output is correct
58 Correct 1646 ms 49836 KB Output is correct
59 Correct 1634 ms 49836 KB Output is correct
60 Correct 1639 ms 49840 KB Output is correct
61 Correct 1640 ms 49836 KB Output is correct
62 Correct 1638 ms 49868 KB Output is correct
63 Correct 1638 ms 49964 KB Output is correct
64 Correct 1710 ms 49836 KB Output is correct
65 Correct 1652 ms 49812 KB Output is correct
66 Correct 1646 ms 49836 KB Output is correct
67 Correct 1636 ms 49836 KB Output is correct
68 Correct 1635 ms 49836 KB Output is correct
69 Correct 1634 ms 49836 KB Output is correct
70 Correct 1644 ms 49836 KB Output is correct
71 Correct 1636 ms 49836 KB Output is correct
72 Correct 1630 ms 50004 KB Output is correct
73 Correct 1650 ms 49836 KB Output is correct
74 Correct 1639 ms 49812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 704 KB Output is correct
2 Correct 5 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 4 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 256 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 9 ms 704 KB Output is correct
17 Correct 9 ms 704 KB Output is correct
18 Correct 10 ms 704 KB Output is correct
19 Correct 11 ms 832 KB Output is correct
20 Correct 9 ms 832 KB Output is correct
21 Correct 9 ms 704 KB Output is correct
22 Correct 9 ms 704 KB Output is correct
23 Correct 9 ms 704 KB Output is correct
24 Correct 8 ms 704 KB Output is correct
25 Correct 9 ms 704 KB Output is correct
26 Correct 5 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 4 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 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 9 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 9 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 9 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 1637 ms 50060 KB Output is correct
49 Correct 1634 ms 49836 KB Output is correct
50 Correct 1628 ms 49836 KB Output is correct
51 Correct 1626 ms 49840 KB Output is correct
52 Correct 1626 ms 49844 KB Output is correct
53 Correct 1629 ms 49836 KB Output is correct
54 Correct 1627 ms 49836 KB Output is correct
55 Correct 1641 ms 49800 KB Output is correct
56 Correct 1636 ms 49840 KB Output is correct
57 Correct 1643 ms 49836 KB Output is correct
58 Correct 1596 ms 49836 KB Output is correct
59 Correct 1571 ms 49840 KB Output is correct
60 Correct 1566 ms 49836 KB Output is correct
61 Correct 1577 ms 49836 KB Output is correct
62 Correct 1591 ms 49836 KB Output is correct
63 Correct 1588 ms 49836 KB Output is correct
64 Correct 1594 ms 49836 KB Output is correct
65 Correct 1593 ms 49836 KB Output is correct
66 Correct 1607 ms 49928 KB Output is correct
67 Correct 1592 ms 49968 KB Output is correct
68 Correct 1581 ms 49836 KB Output is correct
69 Correct 1569 ms 49836 KB Output is correct
70 Correct 1638 ms 49812 KB Output is correct
71 Correct 1645 ms 49964 KB Output is correct
72 Correct 1634 ms 49836 KB Output is correct
73 Correct 1646 ms 49836 KB Output is correct
74 Correct 1634 ms 49836 KB Output is correct
75 Correct 1639 ms 49840 KB Output is correct
76 Correct 1640 ms 49836 KB Output is correct
77 Correct 1638 ms 49868 KB Output is correct
78 Correct 1638 ms 49964 KB Output is correct
79 Correct 1710 ms 49836 KB Output is correct
80 Correct 1652 ms 49812 KB Output is correct
81 Correct 1646 ms 49836 KB Output is correct
82 Correct 1636 ms 49836 KB Output is correct
83 Correct 1635 ms 49836 KB Output is correct
84 Correct 1634 ms 49836 KB Output is correct
85 Correct 1644 ms 49836 KB Output is correct
86 Correct 1636 ms 49836 KB Output is correct
87 Correct 1630 ms 50004 KB Output is correct
88 Correct 1650 ms 49836 KB Output is correct
89 Correct 1639 ms 49812 KB Output is correct
90 Correct 1669 ms 49836 KB Output is correct
91 Correct 1643 ms 49836 KB Output is correct
92 Correct 1650 ms 49836 KB Output is correct
93 Correct 1640 ms 49908 KB Output is correct
94 Correct 1649 ms 49840 KB Output is correct
95 Correct 1638 ms 49840 KB Output is correct
96 Correct 1657 ms 49840 KB Output is correct
97 Correct 1627 ms 49836 KB Output is correct
98 Correct 1654 ms 49964 KB Output is correct
99 Correct 1640 ms 49840 KB Output is correct
100 Correct 896 ms 49836 KB Output is correct
101 Correct 915 ms 49836 KB Output is correct
102 Correct 906 ms 49832 KB Output is correct
103 Correct 906 ms 49836 KB Output is correct
104 Correct 915 ms 49836 KB Output is correct
105 Correct 1127 ms 49836 KB Output is correct
106 Correct 1092 ms 49964 KB Output is correct
107 Correct 1118 ms 49840 KB Output is correct
108 Correct 1098 ms 49836 KB Output is correct
109 Correct 1112 ms 49836 KB Output is correct
110 Correct 1555 ms 49872 KB Output is correct
111 Correct 1579 ms 49812 KB Output is correct
112 Correct 1619 ms 49844 KB Output is correct
113 Correct 1630 ms 49812 KB Output is correct
114 Correct 1604 ms 49752 KB Output is correct
115 Correct 1573 ms 49780 KB Output is correct
116 Correct 1589 ms 49976 KB Output is correct
117 Correct 1573 ms 49836 KB Output is correct
118 Correct 1615 ms 49836 KB Output is correct
119 Correct 1637 ms 49836 KB Output is correct
120 Correct 5 ms 384 KB Output is correct
121 Correct 5 ms 384 KB Output is correct
122 Correct 1569 ms 49836 KB Output is correct
123 Correct 1575 ms 49924 KB Output is correct
124 Correct 1576 ms 49836 KB Output is correct
125 Correct 1570 ms 49836 KB Output is correct
126 Correct 1530 ms 49836 KB Output is correct
127 Correct 1556 ms 49816 KB Output is correct
128 Correct 1704 ms 49836 KB Output is correct
129 Correct 1534 ms 49836 KB Output is correct
130 Correct 1586 ms 49836 KB Output is correct
131 Correct 1559 ms 49836 KB Output is correct
132 Correct 1595 ms 49836 KB Output is correct
133 Correct 1508 ms 49836 KB Output is correct