QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#443697#8522. Waterfall Matrixucup-team087#WA 7180ms4072kbC++143.4kb2024-06-15 16:19:052024-06-15 16:19:05

Judging History

你现在查看的是最新测评结果

  • [2024-06-15 16:19:05]
  • 评测
  • 测评结果:WA
  • 用时:7180ms
  • 内存:4072kb
  • [2024-06-15 16:19:05]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


int N;
vector<Int> A, B, C;

vector<Int> sol;

void rec(Int cL, Int cR, const vector<int> &us) {
// cerr<<"[rec] ["<<cL<<", "<<cR<<"] "<<us<<endl;
  if (cL == cR) {
    for (const int u : us) {
      sol[u] = cL;
    }
  } else {
    const Int cMid = (cL + cR) / 2;
    map<Int, Int> gs;
    vector<pair<Int, Int>> ps;
    for (const int u : us) {
      const Int cost0 = abs(cMid - C[u]);
      const Int cost1 = abs((cMid + 1) - C[u]);
      if (cost0 < cost1) {
        // 0-side = s-side
        Int f = cost1 - cost0;
        for (auto it = gs.lower_bound(B[u]); it != gs.end(); it = gs.erase(it)) {
          if (f >= it->second) {
            f -= it->second;
          } else {
            f = 0;
            it->second -= f;
            break;
          }
        }
// cerr<<"    u = "<<u<<": f = "<<f<<endl;
        if (f > 0) {
          for (; ps.size() && B[u] <= ps.back().second; ps.pop_back()) {}
          ps.emplace_back(A[u], B[u]);
        }
      } else {
        // 1-side = t-side
        const Int g = cost0 - cost1;
        gs[B[u]] += g;
      }
    }
// cerr<<"  "<<cMid<<" "<<cMid+1<<" "<<us<<": "<<ps<<endl;
    int pos = 0;
    vector<int> uss[2];
    for (const int u : us) {
      for (; pos < (int)ps.size() && !(ps[pos].first <= A[u]); ++pos) {}
      uss[(pos < (int)ps.size() && ps[pos].second <= B[u]) ? 0 : 1].push_back(u);
    }
    rec(cL, cMid, uss[0]);
    rec(cMid + 1, cR, uss[1]);
  }
}

int main() {
  for (; ~scanf("%d", &N); ) {
    A.resize(N);
    B.resize(N);
    C.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%lld%lld%lld", &A[u], &B[u], &C[u]);
    }
    
    vector<int> us(N);
    for (int u = 0; u < N; ++u) us[u] = u;
    sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (make_pair(A[u], B[u]) > make_pair(A[v], B[v]));
    });
    sol.assign(N, -1);
    const Int minC = *min_element(C.begin(), C.end());
    const Int maxC = *max_element(C.begin(), C.end());
    rec(minC, maxC, us);
// cerr<<"sol = "<<sol<<endl;
    
    Int ans = 0;
    for (int u = 0; u < N; ++u) {
      ans += abs(sol[u] - C[u]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 4072kb

input:

5
1 3 5
3 2 1
3 3 3
4 4 1
3 5 4

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

1
1 1 58383964

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 5290ms
memory: 3784kb

input:

2
1 1 204909414
2 2 807091086

output:

602181672

result:

ok 1 number(s): "602181672"

Test #4:

score: -100
Wrong Answer
time: 7180ms
memory: 3792kb

input:

4
2 4 107744934
3 2 143843719
4 4 908851567
2 2 307161330

output:

865146178

result:

wrong answer 1st numbers differ - expected: '801106633', found: '865146178'