#include "triples.h"
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
long long count_triples(vector<int> H) {
int n = H.size();
long long res = 0;
if (n <= 100) {
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
for (int k = j + 1; k < n; ++k) {
vector<int> h = {H[i], H[j], H[k]};
vector<int> d = {j - i, k - i, k - j};
sort(h.begin(), h.end());
sort(d.begin(), d.end());
if (h == d) res++;
}
return res;
}
bool small_value = true;
for (int h : H)
if (h > 10) {
small_value = false;
break;
}
if (small_value) {
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < min(n, i + 11); ++j)
for (int k = j + 1; k < min(n, i + 22); ++k) {
vector<int> h = {H[i], H[j], H[k]};
vector<int> d = {j - i, k - i, k - j};
sort(h.begin(), h.end());
sort(d.begin(), d.end());
if (h == d) res++;
}
return res;
}
if (is_sorted(H.begin(), H.end())) {
for (int j = 1; j < n - 1; ++j) {
int target = 2 * H[j];
int left = 0, right = n - 1;
while (left < j && right > j) {
if (H[left] + H[right] == target) {
vector<int> h = {H[left], H[j], H[right]};
vector<int> d = {j - left, right - left, right - j};
sort(h.begin(), h.end());
sort(d.begin(), d.end());
if (h == d) res++;
int curr_left = left, curr_right = right;
while (left < j && H[left] == H[curr_left]) left++;
while (right > j && H[right] == H[curr_right]) right--;
} else if (H[left] + H[right] < target) {
left++;
} else {
right--;
}
}
}
return res;
}
// --- THÊM XỬ LÝ NÀY ---
if (n <= 2000) {
vector<pair<int, int>> arr;
for (int i = 0; i < n; ++i)
arr.emplace_back(i, H[i]);
sort(arr.begin(), arr.end(), [](auto &a, auto &b) {
return a.second < b.second;
});
for (int x = 0; x < n; ++x) {
int i = arr[x].first;
for (int y = x + 1; y < n; ++y) {
int j = arr[y].first;
if (i >= j) continue;
vector<int> h = {H[i], H[j]};
vector<int> d = {j - i};
for (int k = j + 1; k < n; ++k) {
h.push_back(H[k]);
d = {j - i, k - i, k - j};
vector<int> tmp_h = h;
vector<int> tmp_d = d;
sort(tmp_h.begin(), tmp_h.end());
sort(tmp_d.begin(), tmp_d.end());
if (tmp_h == tmp_d) res++;
h.pop_back();
}
}
}
return res;
}
// fallback cuối
unordered_map<int, vector<int>> height_positions;
for (int i = 0; i < n; i++) {
height_positions[H[i]].push_back(i);
}
for (int i = 0; i < min(n, 1000); ++i) {
for (int j = i + 1; j < min(n, i + 1000); ++j) {
for (int k = j + 1; k < min(n, i + 2000); ++k) {
vector<int> h = {H[i], H[j], H[k]};
vector<int> d = {j - i, k - i, k - j};
sort(h.begin(), h.end());
sort(d.begin(), d.end());
if (h == d) res++;
}
}
}
return res;
}
vector<int> construct_range(int M, int K) {
return {1, 1, 2};
}
# | 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... |