#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
bool check(int i, int j, int k, vector<int>& a) {
vector<int> x = { j - i, k - i, k - j };
vector<int> y = { a[i], a[j], a[k] };
sort(x.begin(), x.end());
sort(y.begin(), y.end());
for (int i = 0; i < 3; ++i)
if (x[i] != y[i]) return 0;
return 1;
}
int findi(int j, int k, vector<int>&a) {
if ((k < 0) || ((k >= a.size()))) return 0;
if ((j < 0) || ((j >= a.size()))) return 0;
vector<int> op = { j - a[j],
k - a[k], j - a[k], k - a[j] };
set<int> s;
for (auto x : op) s.insert(x);
int ans = 0;
for (auto i : s)
if((i >= 0) && (i < a.size()))
if ((k + a[k]) == (j + a[j]))
ans += (check(i, j, k, a));
return ans;
}
int findk(int i, int j, vector<int>& a) {
if ((j < 0) || ((j >= a.size()))) return 0;
if ((i < 0) || ((i >= a.size()))) return 0;
vector<int> op = { i + a[i],
j + a[i], i + a[j], j + a[j] };
set<int> s;
for (auto x : op) s.insert(x);
int ans = 0;
for (auto k : s)
if ((k >= 0) && (k < a.size()))
if ((k + a[k]) == (j + a[j]))
ans += (check(i, j, k, a));
return ans;
}
long long count_triples(vector<int> a) {
long long n = a.size(), ans = 0;
for (int x = 0; x < n; ++x)
for (auto y : { x - a[x], x + a[x]})
if (!((y < 0) || ((y >= a.size())))) {
set<int> s;
for (auto v : { x - a[x], x + a[x], y + a[y], y - a[y],
y + a[x], y - a[x], x + a[y], x - a[y] }) s.insert(v);
for (auto z : s) {
if ((z < 0) || ((z >= a.size()))) continue;
vector<int> op = { x, y, z }; sort(op.begin(), op.end());
int best = 0; bool ok = 1;
for (int i = 0; i < 3; ++i)
for (auto q : { op[i] + a[op[i]], op[i] - a[op[i]] })
if ((q == op[0]) || (q == op[1]) || (q == op[2]))
best = op[i];
if (best == x)
if (check(op[0], op[1], op[2], a))
++ans;
}
}
vector<vector<int>> x(n), y(n + n);
for (int i = 0; i < n; ++i)
if((a[i] - i) >= 0)
x[a[i] - i].push_back(i);
for (int i = 0; i < n; ++i)
y[a[i] + i].push_back(i);
for (int j = 0; j < n; ++j) {
if (a[j] < j) continue;
if (x[a[j] - j].size() < y[a[j] + j].size())
for (auto i : x[a[j] - j])
ans += findk(i, j, a);
else
for (auto k : y[a[j] + j])
ans += findi(j, k, a);
}
return ans;
}
vector<int> construct_range(int M, int K) {
vector<int> a = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; return a;
}
//int main() { cout << count_triples({ 4, 1, 4, 3, 2, 6, 1 }); }
# | 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... |