답안 #381425

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381425 2021-03-25T07:51:57 Z IldarKA San (COCI17_san) C++14
96 / 120
275 ms 65540 KB
#include <bits/stdc++.h>

using namespace std;
struct node{
    int bit[2];
    int cnt;
} t[5000001];
int n, h[41], g[41], sz = 1;
long long k;
vector < pair < int, long long > > v, v2;
void add(long long x){
    int v = 0;
    for(int j = 35; j >= 0; j--){
        int c = ((x >> j) & 1);
        if(!t[v].bit[c]){
            t[v].bit[c] = sz;
            sz++;
        }
        v = t[v].bit[c];
        t[v].cnt++;
    }
}
void del(long long x){
    int v = 0;
    for(int j = 35; j >= 0; j--){
        int c = ((x >> j) & 1);
        v = t[v].bit[c];
        t[v].cnt--;
    }
}
int get(long long x){
    int v = 0;
    int ans = 0;
    for(int i = 35; i >= 0; i--){
        int c = ((x >> i) & 1);
        if(!t[v].bit[c]){
            t[v].bit[c] = sz;
            sz++;
        }
        if(c == 0 && t[v].bit[1]){
            ans += t[t[v].bit[1]].cnt;
        }
        v = t[v].bit[c];
    }
    ans += t[v].cnt;
    return ans;
}
int main(){
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> h[i] >> g[i];
    }
    int q = n / 2;
    for(int i = 0; i < (1 << q); i++){
        long long sum = 0;
        bool ok = 1;
        int last = 0;
        for(int j = 0; j < q; j++){
            if((i >> j) & 1){
                if(last > h[j + 1]){
                    ok = 0;
                    break;
                }
                last = h[j + 1];
                sum += g[j + 1];
            }
        }
        if(ok){
            v.push_back({last, sum});
        }
    }
    sort(v.begin(), v.end());
    int q2 = n - q;
    for(int i = 0; i < (1 << q2); i++){
        long long sum = 0;
        bool ok = 1;
        int last = 0, first = 1e9 + 1;
        for(int j = 0; j < q2; j++){
            if((i >> j) & 1){
                if(first == (int)1e9 + 1){
                    first = h[j + 1 + q];
                }
                if(last > h[j + 1 + q]){
                    ok = 0;
                    break;
                }
                last = h[j + 1 + q];
                sum += g[j + 1 + q];
            }
        }
        if(ok){
            add(sum);
            v2.push_back({first, sum});
        }
    }
    sort(v2.begin(), v2.end());
    int pos = 0;
    long long ans = 0;
    for(int i = 0; i < v.size(); i++){
        while(pos < (int)v2.size() && v[i].first > v2[pos].first){
            del(v2[pos].second);
            pos++;
        }
        ans += get(max(k - v[i].second, 0ll));
    }
    cout << ans;
}

Compilation message

san.cpp: In function 'int main()':
san.cpp:99:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |     for(int i = 0; i < v.size(); i++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15456 KB Output is correct
2 Correct 6 ms 876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 11612 KB Output is correct
2 Correct 28 ms 3172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 275 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -