#include "triples.h"
#include <algorithm>
#include <array>
#include <iterator>
#include <cassert>
#include <iostream>
long long count_triples(std::vector<int> H) {
int N = H.size();
long long ans = 0;
// remember we have j-i, k-j, and (j-i) + (k-j)
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
// we have three cases: is H[i], H[j], or H[k] biggest?
// CASE 1
// if H[k] is biggest, then either H[i] = j-i and H[j] = k-j
// or H[i] = k-j and H[j] = j-i
// if H[i] = j-i and H[j] = k-j, then H[k] = H[i] + H[j]
// k = (k-j) + (j-i) + i = H[j] + H[i] + i
// if H[j] = j-i and H[i] = k-j, then still H[k] = H[i] + H[j]
// and k = H[i] + H[j] + i
// both sub-cases are the same equation
int k = H[i] + H[j] + i;
if (0 <= k && k < N && k != i && k != j && H[k] == H[i] + H[j] && (H[i] == j-i || H[j] == j-i)) {
// std::cout << i << ' ' << j << ' ' << k << ' ' << "(H[k] is biggest)" << '\n';
++ans;
}
// CASE 2
// if H[i] is biggest, H[i] = k-i
// either H[j] = j-i, or H[j] = k-j
// if H[i] = k-i, H[j] = j-i, H[k] = k-j
// then H[k] = H[i] - H[j], and k = H[i] - H[j] + j
// if H[i] = k-i, H[j] = k-j, H[k] = j-i
// H[k] = H[i] - H[j], k = H[i] + i = H[j] + j
int k1 = H[i] - H[j] + j;
int k2 = H[i] + i;
if (0 <= k1 && k1 < N && k1 != i && k1 != j && H[k1] == H[i] - H[j] && H[j] == j-i) {
// std::cout << i << ' ' << j << ' ' << k1 << ' ' << "(H[i] is biggest)" << '\n';
++ans;
}
if (k2 != k1 && 0 <= k2 && k2 < N && k2 == H[j] + j && k2 != i && k2 != j && H[k2] == H[i] - H[j] && H[i] == k2-i) {
// std::cout << i << ' ' << j << ' ' << k2 << ' ' << "(H[i] is biggest)" << '\n';
++ans;
}
// CASE 3
// if H[j] is biggest, then we got two cases (again!)
// H[i] = j-i, H[j] = k-i, H[k] = k-j
// H[k] = H[j] - H[i], k = H[j] + i
// H[i] = k-j, H[j] = k-i, H[k] = j-i
// H[k] = H[j] - H[i], k = H[i] + j = H[j] + i
// note that this means if k exists in the latter case, it should alr satisfy the former case
k = H[j] + i;
if (0 <= k && k < N && k != i && k != j && H[k] == H[j] - H[i] && (H[i] == j-i || H[i] == k-j)) {
// std::cout << i << ' ' << j << ' ' << k << ' ' << "(H[j] is biggest)" << '\n';
++ans;
}
}
}
return ans;
}
std::vector<int> construct_range(int M, int K) {
std::vector<int> ans;
ans.push_back(1);
ans.push_back(1);
for (int i = 2; i < M; i++) {
ans.push_back(i);
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |