Submission #520264

# Submission time Handle Problem Language Result Execution time Memory
520264 2022-01-29T04:17:20 Z cig32 Road Construction (JOI21_road_construction) C++17
18 / 100
10000 ms 15644 KB
#pragma GCC optimize("Ofast")
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2.5e5 + 10;
const int MOD = 1e9 + 7;
#define int long long

int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}

struct node{
  int val, idx;
} a[4*MAXN];
void init(int l, int r, int idx) {
  if(l == r) {
    a[idx].val = 0;
    a[idx].idx = l;
    return;
  }
  int mid = (l + r) >> 1;
  init(l, mid, 2*idx + 1);
  init(mid+1, r, 2*idx + 2);
}
void u(int l, int r, int tar, int idx, int val) {
  if(l == r) {
    a[idx].val = val;
    return;
  }
  int mid = (l + r) >> 1;
  if(tar <= mid) u(l, mid, tar, 2*idx+1, val);
  else u(mid+1, r, tar, 2*idx+2, val);
  a[idx].val = min(a[2*idx+1].val, a[2*idx+2].val);
  a[idx].idx = (a[2*idx+1].val <= a[2*idx+2].val ? a[2*idx+1].idx : a[2*idx+2].idx);
}
pair<int, int> qu(int l, int r, int constl, int constr, int idx) {
  if(l<=constl && constr<=r) return {a[idx].val, a[idx].idx};
  int mid = (constl+constr) >> 1;
  if(mid<l || r<constl) return qu(l, r, mid+1, constr, 2*idx+2);
  else if(constr<l || r<mid+1) return qu(l, r, constl, mid, 2*idx+1);
  return min(qu(l, r, constl, mid, 2*idx+1), qu(l, r, mid+1, constr, 2*idx+2));
}
int n, k, x[MAXN], y[MAXN];
void update(int i, int v) {
  u(1, n-1, i, 0, v);
}
int nxt[MAXN];
int query(int l, int r) {
  pair<int, int> res = qu(l, r, 1, n-1, 0);
  nxt[res.second]++;
  if(nxt[res.second] > n) {
    update(res.second, (int)1e18);
  }
  else {
    update(res.second, x[nxt[res.second]] - x[res.second]);
  }
  return res.first;
}

bool cmp1(int a, int b) {
  return x[a] - y[a] < x[b] - y[b];
}
bool cmp2(int a, int b) {
  return x[a] + y[a] < x[b] + y[b];
}
void solve(int tc) {
  cin >> n >> k;
  for(int i=1; i<=n; i++) {
    cin >> x[i] >> y[i];
  }
  if(n <= 1000) {
    vector<int> v;
    for(int i=1; i<=n; i++) {
      for(int j=i+1; j<=n; j++) {
        v.push_back(abs(x[i] - x[j]) + abs(y[i] - y[j]));
      }
    }
    sort(v.begin(), v.end());
    for(int i=0; i<k; i++) cout << v[i] << "\n";
    return;
  }
  bool yes = 1;
  for(int i=1; i<=n; i++) yes &= (y[i] == 0);
  if(yes) {
    for(int i=1; i<=n; i++) nxt[i] = i + 1;
    init(1, n-1, 0);
    sort(x+1, x+n+1);
    for(int i=1; i<n; i++) update(i, x[i+1] - x[i]);
    for(int i=0; i<k; i++) cout << query(1, n-1) << "\n";
    return;
  }
  int p[n+1];
  for(int i=1; i<=n; i++) p[i] = i;
  priority_queue<int> pq;
  sort(p+1, p+n+1, cmp1);
  for(int i=1; i<=n; i++) {
    int cnt = 0;
    for(int j=1; cnt < k && i+j <= n; j++) {
      if(x[p[i+j]] >= x[p[i]] && y[p[i]] >= y[p[i+j]]) {
        pq.push(abs(x[p[i+j]] - x[p[i]]) + abs(y[p[i+j]] - y[p[i]]));
        cnt++;
      }
      if(pq.size() > k) pq.pop();
    }
  }
  sort(p+1, p+n+1, cmp2);
  for(int i=1; i<=n; i++) {
    int cnt = 0;
    for(int j=1; cnt < k && i+j <= n; j++) {
      if(x[p[i+j]] >= x[p[i]] && y[p[i+j]] >= y[p[i]]) {
        pq.push(abs(x[p[i+j]] - x[p[i]]) + abs(y[p[i+j]] - y[p[i]]));
        cnt++;
      }
      if(pq.size() > k) pq.pop();
    }
  }
  stack<int> st;
  while(pq.size()) {
    st.push(pq.top()); pq.pop();
  }
  while(st.size()) {
    cout << st.top() << "\n"; st.pop();
  }
}
int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}

Compilation message

road_construction.cpp: In function 'void solve(long long int)':
road_construction.cpp:179:20: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  179 |       if(pq.size() > k) pq.pop();
      |          ~~~~~~~~~~^~~
road_construction.cpp:190:20: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  190 |       if(pq.size() > k) pq.pop();
      |          ~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 60 ms 6960 KB Output is correct
2 Correct 63 ms 6968 KB Output is correct
3 Correct 38 ms 5044 KB Output is correct
4 Correct 39 ms 5112 KB Output is correct
5 Correct 56 ms 5816 KB Output is correct
6 Correct 18 ms 4548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 15644 KB Output is correct
2 Correct 199 ms 15584 KB Output is correct
3 Correct 37 ms 4920 KB Output is correct
4 Correct 132 ms 15288 KB Output is correct
5 Correct 125 ms 15632 KB Output is correct
6 Correct 130 ms 15568 KB Output is correct
7 Correct 128 ms 14788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1041 ms 6208 KB Output is correct
2 Correct 1065 ms 6184 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 94 ms 14384 KB Output is correct
5 Correct 735 ms 6244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1041 ms 6208 KB Output is correct
2 Correct 1065 ms 6184 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 94 ms 14384 KB Output is correct
5 Correct 735 ms 6244 KB Output is correct
6 Correct 3697 ms 6180 KB Output is correct
7 Correct 3609 ms 11272 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Incorrect 3611 ms 11300 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 60 ms 6960 KB Output is correct
2 Correct 63 ms 6968 KB Output is correct
3 Correct 38 ms 5044 KB Output is correct
4 Correct 39 ms 5112 KB Output is correct
5 Correct 56 ms 5816 KB Output is correct
6 Correct 18 ms 4548 KB Output is correct
7 Execution timed out 10015 ms 4768 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 60 ms 6960 KB Output is correct
2 Correct 63 ms 6968 KB Output is correct
3 Correct 38 ms 5044 KB Output is correct
4 Correct 39 ms 5112 KB Output is correct
5 Correct 56 ms 5816 KB Output is correct
6 Correct 18 ms 4548 KB Output is correct
7 Correct 238 ms 15644 KB Output is correct
8 Correct 199 ms 15584 KB Output is correct
9 Correct 37 ms 4920 KB Output is correct
10 Correct 132 ms 15288 KB Output is correct
11 Correct 125 ms 15632 KB Output is correct
12 Correct 130 ms 15568 KB Output is correct
13 Correct 128 ms 14788 KB Output is correct
14 Correct 1041 ms 6208 KB Output is correct
15 Correct 1065 ms 6184 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 94 ms 14384 KB Output is correct
18 Correct 735 ms 6244 KB Output is correct
19 Correct 3697 ms 6180 KB Output is correct
20 Correct 3609 ms 11272 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 316 KB Output is correct
23 Incorrect 3611 ms 11300 KB Output isn't correct
24 Halted 0 ms 0 KB -