Submission #320047

# Submission time Handle Problem Language Result Execution time Memory
320047 2020-11-07T11:57:42 Z gustason San (COCI17_san) C++14
120 / 120
190 ms 7768 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int maxN = 45;
vector<ll> L[maxN];
vector<ll> R[maxN];
ll h[maxN], c[maxN], n, k;
vector<int> hs;

void rec_left(int pos, int last, ll sum) {
    if (pos == n/2) {
        auto idx = lower_bound(hs.begin(), hs.end(), last) - hs.begin();
        L[idx].push_back(sum);
        return;
    }

    rec_left(pos + 1, last, sum);
    if (h[pos] >= last) {
        rec_left(pos + 1, h[pos], sum + c[pos]);
    }
}
void rec_right(int pos, int first, int last, ll sum) {
    if (pos == n) {
        auto idx = lower_bound(hs.begin(), hs.end(), first) - hs.begin();
        R[idx].push_back(sum);
        return;
    }
    rec_right(pos + 1, first, last, sum);
    if (h[pos] >= last) {
        if (first == 0) {
            first = h[pos];
        }
        rec_right(pos + 1, first, h[pos], sum + c[pos]);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> k;
    for(int i = 0; i < n; i++) {
        cin >> h[i] >> c[i];
        hs.push_back(h[i]);
    }
    hs.push_back(0);
    sort(hs.begin(), hs.end());

    rec_left(0, 0, 0);
    rec_right(n/2, 0, 0, 0);

    for(int i = 0; i < (int) hs.size(); i++) {
        sort(L[i].begin(), L[i].end());
        sort(R[i].begin(), R[i].end());
    }

    ll ans = 0;
    for(int i = 0; i < (int) hs.size(); i++) {
        for(auto it : L[i]) {
            ans += R[0].end() - lower_bound(R[0].begin(), R[0].end(), k - it);
            for(int j = i; j < (int) hs.size(); j++) {
                ans += R[j].end() - lower_bound(R[j].begin(), R[j].end(), k - it);
            }
        }
    }

    cout << ans << "\n";
    return 0;
}
//~ check for overflows
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1260 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2088 KB Output is correct
2 Correct 6 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 7768 KB Output is correct
2 Correct 29 ms 1704 KB Output is correct