제출 #1249891

#제출 시각아이디문제언어결과실행 시간메모리
1249891kduckp3개의 봉우리 (IOI25_triples)C++20
14.17 / 100
2095 ms20176 KiB
#include "triples.h"
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b)
{
    return a.first > b.first;
}

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 (n <= 2000) {
        unordered_map<int, vector<int>> pos;
        for (int i = 0; i < n; ++i)
            pos[H[i]].push_back(i);

        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                int d1 = j - i;
                for (int h3 = 1; h3 <= 2 * d1; ++h3) {
                    vector<int> h = {H[i], H[j], h3};
                    vector<int> d = {j - i, 0, 0};
                    if (!pos.count(h3)) continue;
                    const auto& indices = pos[h3];
                    auto it = upper_bound(indices.begin(), indices.end(), j);
                    for (; it != indices.end(); ++it) {
                        int k = *it;
                        d[1] = k - i;
                        d[2] = k - j;
                        sort(h.begin(), h.end());
                        sort(d.begin(), d.end());
                        if (h == d)
                            res++;
                    }
                }
            }
        }
        return res;
    }

    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 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...