Submission #1172619

#TimeUsernameProblemLanguageResultExecution timeMemory
1172619_ncng.nyr나일강 (IOI24_nile)C++20
47 / 100
2096 ms8888 KiB
#include<bits/stdc++.h>

#define tp tuple<int, int, int>

using namespace std;

const int N = 2e5 + 5;
const long long oo = 1e16;

int n, q, chk1 = 1, chk2 = 1, chk3 = 1;
long long a[N], b[N], w[N], e[N];

namespace sub1 {
  vector<long long> solve() {
    vector<long long> ans;

    for(int i = 1; i <= q; ++i) {
      int D = e[i];
      long long res = 0;
      for(int i = 1; i <= n; ++i) res += b[i];

      if(n & 1) {
        long long sub = oo;
        for(int i = 1; i <= n; ++i) sub = min(sub, a[i] - b[i]);
        res += sub;
      }

      ans.push_back(res);
    }

    return ans;
  }
}

namespace sub2 {
  long long pref[N];
  vector<long long> solve() {
    vector<long long> ans;
    for(int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + b[i];

    for(int i = 1; i <= q; ++i) {
      int D = e[i];
      long long res = oo;

      if(n & 1) {
        if(D > 1) for(int j = 1; j <= n; ++j) res = min(res, pref[n] + a[j] - b[j]);
        else {
          for(int j = 1; j <= n; ++j)
            if(j & 1) res = min(res, pref[n] + a[j] - b[j]);
        }
      }

      else res = pref[n];
      ans.push_back(res);
    }

    return ans;
  }
}

namespace sub3 {
  tp item[N];

  vector<long long> solve() {
    vector<long long> ans;

    for(int i = 1; i <= n; ++i) item[i] = {w[i], a[i], b[i]};
    sort(item + 1, item + n + 1);

    for(int idx = 1; idx <= q; ++idx) {
      int D = e[idx];
      vector<long long> f(n + 1);
      f[1] = 2;

      for(int i = 2; i <= n; ++i) {
        auto [nw, na, nb] = item[i - 1];
        auto [w, a, b] = item[i];
        f[i] = f[i - 1] + 2;
        if(w - nw <= D) f[i] = min(f[i], f[i - 2] + 2);
      }

      ans.push_back(f[n]);
    }

    return ans;
  }
}

namespace sub4 {
  tp item[N];

  vector<long long> solve() {
    vector<long long> ans;

    for(int i = 1; i <= n; ++i) item[i] = {w[i], a[i], b[i]};
    sort(item + 1, item + n + 1);

    vector<long long> pref(n + 1);
    for(int i = 1; i <= n; ++i) {
      auto [w, a, b] = item[i];
      pref[i] = pref[i - 1] + a;
    }

    for(int idx = 1; idx <= q; ++idx) {
      int D = e[idx];
      vector<long long> f(n + 1);

      for(int i = 1; i <= n; ++i) {
        auto [w, a, b] = item[i];
        f[i] = f[i - 1] + a;
        for(int j = i - 1; j; --j) {
          auto [nw, na, nb] = item[j];
          if(w - nw > D) break;

          f[i] = min(f[i], f[j - 1] + b + nb + pref[i - 1] - pref[j]);
        }
      }

      ans.push_back(f[n]);
    }

    return ans;
  }
}

vector<long long> calculate_costs(
    vector<int> W, vector<int> A,
    vector<int> B, vector<int> E) {

  n = W.size(); q = E.size();

  for(int i = 1; i <= n; ++i) {
    w[i] = W[i - 1], a[i] = A[i - 1], b[i] = B[i - 1];
    if(w[i] != 1) chk1 = 0;
    if(w[i] != i) chk2 = 0;
    if(a[i] != 2 || b[i] != 1) chk3 = 0;
  }

  for(int i = 1; i <= q; ++i) e[i] = E[i - 1];

  if(chk1) return sub1 :: solve();
  if(chk2) return sub2 :: solve();
  if(chk3) return sub3 :: solve();
  if(n <= 2'000 && q <= 5) return sub4 :: solve();
}

//#define ntc
#ifdef ntc
int32_t main() {
  cin.tie(0)->sync_with_stdio(0);

  if(fopen("task.inp", "r")) {
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
  }

  if(fopen("NILE.inp", "r")) {
    freopen("NILE.inp", "r", stdin);
    freopen("NILE.out", "w", stdout);
  }

  vector<int> W, A, B, E;

  cin >> n;
  for(int i = 1; i <= n; ++i) {
    int w, a, b; cin >> w >> a >> b;
    W.push_back(w); A.push_back(a); B.push_back(b);
  }

  cin >> q;
  for(int i = 1; i <= q; ++i) {
    int D; cin >> D;
    E.push_back(D);
  }

  vector<long long> ans = calculate_costs(W, A, B, E);
  for(auto x : ans) cout << x << '\n';
}
#endif

Compilation message (stderr)

nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:145:1: warning: control reaches end of non-void function [-Wreturn-type]
  145 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...