Submission #263516

# Submission time Handle Problem Language Result Execution time Memory
263516 2020-08-13T18:29:42 Z fivefourthreeone Bulldozer (JOI17_bulldozer) C++17
25 / 100
555 ms 49708 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];
  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();
}

Compilation message

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
1 Correct 1 ms 704 KB Output is correct
2 Correct 1 ms 704 KB Output is correct
3 Correct 1 ms 704 KB Output is correct
4 Correct 1 ms 704 KB Output is correct
5 Correct 1 ms 704 KB Output is correct
6 Correct 1 ms 704 KB Output is correct
7 Correct 2 ms 704 KB Output is correct
8 Correct 1 ms 704 KB Output is correct
9 Correct 1 ms 704 KB Output is correct
10 Correct 1 ms 704 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 704 KB Output is correct
2 Correct 4 ms 704 KB Output is correct
3 Correct 4 ms 704 KB Output is correct
4 Correct 5 ms 704 KB Output is correct
5 Correct 4 ms 704 KB Output is correct
6 Correct 4 ms 704 KB Output is correct
7 Correct 4 ms 704 KB Output is correct
8 Correct 4 ms 704 KB Output is correct
9 Correct 4 ms 704 KB Output is correct
10 Correct 4 ms 704 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 416 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 4 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 4 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 4 ms 704 KB Output is correct
28 Correct 4 ms 704 KB Output is correct
29 Correct 4 ms 704 KB Output is correct
30 Correct 4 ms 704 KB Output is correct
31 Correct 4 ms 704 KB Output is correct
32 Correct 5 ms 704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 704 KB Output is correct
2 Correct 4 ms 704 KB Output is correct
3 Correct 4 ms 704 KB Output is correct
4 Correct 5 ms 704 KB Output is correct
5 Correct 4 ms 704 KB Output is correct
6 Correct 4 ms 704 KB Output is correct
7 Correct 4 ms 704 KB Output is correct
8 Correct 4 ms 704 KB Output is correct
9 Correct 4 ms 704 KB Output is correct
10 Correct 4 ms 704 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 416 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 4 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 4 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 4 ms 704 KB Output is correct
28 Correct 4 ms 704 KB Output is correct
29 Correct 4 ms 704 KB Output is correct
30 Correct 4 ms 704 KB Output is correct
31 Correct 4 ms 704 KB Output is correct
32 Correct 5 ms 704 KB Output is correct
33 Incorrect 555 ms 49708 KB Output isn't correct
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 704 KB Output is correct
2 Correct 4 ms 704 KB Output is correct
3 Correct 4 ms 704 KB Output is correct
4 Correct 5 ms 704 KB Output is correct
5 Correct 4 ms 704 KB Output is correct
6 Correct 4 ms 704 KB Output is correct
7 Correct 4 ms 704 KB Output is correct
8 Correct 4 ms 704 KB Output is correct
9 Correct 4 ms 704 KB Output is correct
10 Correct 4 ms 704 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 416 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 4 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 4 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 4 ms 704 KB Output is correct
28 Correct 4 ms 704 KB Output is correct
29 Correct 4 ms 704 KB Output is correct
30 Correct 4 ms 704 KB Output is correct
31 Correct 4 ms 704 KB Output is correct
32 Correct 5 ms 704 KB Output is correct
33 Incorrect 555 ms 49708 KB Output isn't correct
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 704 KB Output is correct
2 Correct 1 ms 704 KB Output is correct
3 Correct 1 ms 704 KB Output is correct
4 Correct 1 ms 704 KB Output is correct
5 Correct 1 ms 704 KB Output is correct
6 Correct 1 ms 704 KB Output is correct
7 Correct 2 ms 704 KB Output is correct
8 Correct 1 ms 704 KB Output is correct
9 Correct 1 ms 704 KB Output is correct
10 Correct 1 ms 704 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 416 KB Output is correct
16 Correct 4 ms 704 KB Output is correct
17 Correct 4 ms 704 KB Output is correct
18 Correct 4 ms 704 KB Output is correct
19 Correct 5 ms 704 KB Output is correct
20 Correct 4 ms 704 KB Output is correct
21 Correct 4 ms 704 KB Output is correct
22 Correct 4 ms 704 KB Output is correct
23 Correct 4 ms 704 KB Output is correct
24 Correct 4 ms 704 KB Output is correct
25 Correct 4 ms 704 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 0 ms 384 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 416 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 4 ms 704 KB Output is correct
37 Correct 4 ms 704 KB Output is correct
38 Correct 4 ms 704 KB Output is correct
39 Correct 4 ms 704 KB Output is correct
40 Correct 4 ms 704 KB Output is correct
41 Correct 4 ms 704 KB Output is correct
42 Correct 4 ms 704 KB Output is correct
43 Correct 4 ms 704 KB Output is correct
44 Correct 4 ms 704 KB Output is correct
45 Correct 4 ms 704 KB Output is correct
46 Correct 4 ms 704 KB Output is correct
47 Correct 5 ms 704 KB Output is correct
48 Incorrect 555 ms 49708 KB Output isn't correct
49 Halted 0 ms 0 KB -