제출 #1251147

#제출 시각아이디문제언어결과실행 시간메모리
1251147aryan12Triple Peaks (IOI25_triples)C++20
35 / 100
984 ms4896 KiB
#include "triples.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> h;
vector<bool> vis;

bool check_range(int i, int j, int k) {
  // checks if the range is correct and H[i] is the lowest
  if(vis[i] || vis[j] || vis[k]) return false;
  if(i < 0LL || j < 0LL || k < 0LL || k < j) return false;
  if(i >= h.size() || j >= h.size() || k >= h.size()) return false;
  vector<long long> diff = {abs(i - j), abs(i - k), abs(j - k)};
  vector<long long> diff2 = {h[i], h[j], h[k]};
  sort(diff.begin(), diff.end());
  sort(diff2.begin(), diff2.end());
  return diff == diff2;
}

bool check_range2(int i, int j, int k) {
  if(vis[i] || vis[j] || vis[k]) return false;
  if(i < 0LL || j < 0LL || k < 0LL) return false;
  if(i >= h.size() || j >= h.size() || k >= h.size()) return false;
  vector<long long> diff = {abs(i - j), abs(i - k), abs(j - k)};
  vector<long long> diff2 = {h[i], h[j], h[k]};
  sort(diff.begin(), diff.end());
  sort(diff2.begin(), diff2.end());
  return diff == diff2;
}

long long count_triples(std::vector<int> H) {
  h = H;
  long long ans = 0;
  int n = h.size();
  vector<pair<int, int> > h_sizes;
  vis.resize(n);
  int maxx = 0;
  for(int i = 0; i < n; i++) {
    h_sizes.push_back({h[i], i});
    vis[i] = false;
    maxx = max(maxx, h[i]);
  }
  sort(h_sizes.begin(), h_sizes.end());
  if(n <= 2000) {
    for(int i = 0; i < h_sizes.size(); i++) {
      int idx = h_sizes[i].second;
      for(int j = 0; j < n; j++) {
        int this_diff = abs(idx - j);
        int actual_diff = (h_sizes[i].first == this_diff) ? h[j] : h_sizes[i].first;
        ans += check_range(idx, j, j - actual_diff);
        // if(check_range(idx, j, j - actual_diff)) cout << "1: " << idx << " " << j << " " << j - actual_diff << "\n";
        ans += check_range(idx, j, j + actual_diff);
        // if(check_range(idx, j, j + actual_diff)) cout << "2: " << idx << " " << j << " " << j + actual_diff << "\n";
        ans += check_range(idx, j, idx - actual_diff);
        // if(check_range(idx, j, idx - actual_diff)) cout << "3: " << idx << " " << j << " " << i - actual_diff << "\n";
        ans += check_range(idx, j, idx + actual_diff);
        // if(check_range(idx, j, idx + actual_diff)) cout << "4: " << idx << " " << j << " " << i + actual_diff << "\n";
      }
      vis[idx] = true;
    }
    return ans;
  }
  if(maxx <= 10) {
    for(int i = 0; i < n; i++) {
      for(int j = i + 1; j <= i + 10; j++) {
        for(int k = j + 1; k <= j + 10; k++) {
          ans += check_range2(i, j, k);
        }
      }
    }
    return ans;
  }
  reverse(h.begin(), h.end());
  for(int i = 0; i < n; i++) {
    int k = i + h[i];
    ans += (check_range2(i, k - h[k], k) && (k - h[k] > i));
    ans += (check_range2(i, i + h[k], k) && (i + h[k] < k) && (i + h[k] != k - h[k]));
    // cout << "i = " << i << ", k = " << k << ", ans = " << ans << "\n";
  }
  return ans;
}

std::vector<int> construct_range(int M, int K) {
  // std::mt19937_64 RNG(std::chrono::steady_clock::now().time_since_epoch().count());
  // std::vector<int> peak_range;
  // for(int i = 0; i < M; i++) {
  //   peak_range.push_back(RNG() % 10 + 5);
  // }
  // return peak_range;
  return {};
}
#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...
#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...