Submission #1115074

# Submission time Handle Problem Language Result Execution time Memory
1115074 2024-11-20T02:15:25 Z KK_1729 Nile (IOI24_nile) C++17
6 / 100
2000 ms 20792 KB
#include "nile.h"
#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define all(a) a.begin(), a.end()
// #define endl "\n"

void printVector(vector<int> a){
  for (auto x: a) cout << x << " ";
  cout << endl;
}

int max(int x, int y){
  if (x > y) return x;
  return y;
}

int maxi(vector<int> a){
  int e = 0;
  for (auto x: a) e = max(e, x);
  return e;
}
std::vector<long long> calculate_costs(std::vector<int> W, std::vector<int> A,
                                       std::vector<int> B, std::vector<int> E) {
  int Q = (int)E.size();
  std::vector<long long> R(Q, 0);

  int n = (int) W.size();

  vector<vector<int>> edges; // edge in the form of {diff, val, l, r}
  
  // auto comp = [] (int a, int b) {
  //   return W[a] < W[b];
  // };

  vector<int> o;
  FOR(i,0,n) o.pb(i);
  sort(all(o), [&](int a, int b){
    return W[a] < W[b];
  });

  vector<int> a(n), b(n);
  FOR(i,0,n){
    a[i] = A[o[i]];
    b[i] = B[o[i]];
  }

  A = a;
  B = b;

  // printVector(o);
  FOR(i,0,n-1){
    edges.pb({W[o[i+1]]-W[o[i]], A[i]+A[i+1]-B[i]-B[i+1], i, i+1, 0});
    if (i < n-2) edges.pb({W[o[i+2]]-W[o[i]], i+1, 1});
  }
  sort(all(edges));
  map<long long, long long> answers;
  vector<int> F = E;
  sort(all(F));

  
  long long ans = 0;
  FOR(i,0,n) ans += A[i];

  // int disc = 0;

  int u = edges.size();

  vector<long long> even(n), odd(n);
  FOR(i,0,n){
    odd[i] = A[i]-B[i];
  }
  FOR(i,1,n-1){
    even[i] = A[i]-B[i];//(A[i-1]-B[i-1])+(A[i]-B[i])+(A[i+1]-B[i+1]);
  }
  int curr = 0;
  set<pair<int, int>> intervals;
  for (auto X: F){
    // cout << X << endl;
    while (curr < u){
      if (edges[curr][0] <= X){
        if (edges[curr][(int)edges[curr].size()-1] == 0){
          // cout << "O" << endl;
          pair<int, int> current = {edges[curr][2], edges[curr][3]};
          for (auto x: intervals){
            if (x.second == edges[curr][2]){
              current.first = x.first;
              intervals.erase(x);
              break;
            }
          }
          for (auto x: intervals){
            if (x.first == current.second){
              current.second = x.second;
              intervals.erase(x);
              break;
            }
          }
          // cout << "a" << current.first << current.second << endl;
          intervals.insert(current);
        }else{
          int x = edges[curr][1];
          // even[x] -= (A[x-1]-B[x-1])+(A[x+1]-B[x+1]);
        }
        curr++;
      }else{
        break;
      }
    }
    long long disc = 0;
    for (auto x: intervals){
      long long m = 1e17;
      
      FOR(i,x.first,x.second+1){
        disc += A[i]-B[i];

      }

      if ((x.second-x.first)%2 == 1) break;
      FOR(i,x.first,x.second+1){
        if ((i-x.first)%2 == 0){
          m = min(m, odd[i]);
        }else{
          m = min(m, even[i]);
        }

      }
      disc -= m;
    }
    
    answers[X] = ans-disc;
    // cout << "a" << intervals.size() << endl;
    // for (auto item: intervals){
    //   cout << item.first << item.second << endl;
    // }
  }
  FOR(i,0,Q){
    R[i] = answers[E[i]];

  }
  return R;
}

Compilation message

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:105:15: warning: unused variable 'x' [-Wunused-variable]
  105 |           int x = edges[curr][1];
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 760 KB Output is correct
2 Correct 5 ms 592 KB Output is correct
3 Correct 5 ms 592 KB Output is correct
4 Correct 5 ms 592 KB Output is correct
5 Correct 5 ms 592 KB Output is correct
6 Correct 5 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2035 ms 20792 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 59 ms 18992 KB Output is correct
2 Correct 63 ms 19248 KB Output is correct
3 Execution timed out 2068 ms 19700 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 760 KB Output is correct
2 Correct 5 ms 592 KB Output is correct
3 Correct 5 ms 592 KB Output is correct
4 Correct 5 ms 592 KB Output is correct
5 Correct 5 ms 592 KB Output is correct
6 Correct 5 ms 592 KB Output is correct
7 Incorrect 2 ms 592 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 760 KB Output is correct
2 Correct 5 ms 592 KB Output is correct
3 Correct 5 ms 592 KB Output is correct
4 Correct 5 ms 592 KB Output is correct
5 Correct 5 ms 592 KB Output is correct
6 Correct 5 ms 592 KB Output is correct
7 Execution timed out 2035 ms 20792 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 59 ms 18992 KB Output is correct
2 Correct 63 ms 19248 KB Output is correct
3 Execution timed out 2068 ms 19700 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 5 ms 760 KB Output is correct
3 Correct 5 ms 592 KB Output is correct
4 Correct 5 ms 592 KB Output is correct
5 Correct 5 ms 592 KB Output is correct
6 Correct 5 ms 592 KB Output is correct
7 Correct 5 ms 592 KB Output is correct
8 Execution timed out 2035 ms 20792 KB Time limit exceeded
9 Halted 0 ms 0 KB -