# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
886722 | vjudge1 | San (COCI17_san) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define ONLINE_JUDGE
void solve() {
int n;
i64 k;
cin >> n >> k;
vector <int> h(n), g(n);
for(int i = 0; i < n; i++) {
cin >> h[i] >> g[i];
}
vector <i64> idxs[n + 2];
int half = n / 2;
i64 ans = 0;
for(int mask = 1; mask < (1 << half); mask++) {
int last = 0, lst = -1;
i64 sum = 0;
bool ok = true;
for(int i = 0; i < half; i++) {
if(mask & (1 << i)) {
if(h[i] < last) {
ok = false;
break;
}
last = h[i];
sum += g[i];
lst = i;
}
}
if(ok) {
idxs[lst].emplace_back(sum);
ans += (sum >= k);
}
}
for(int mask = 1; mask < (1 << (n - half)); mask++) {
int last = 0, fir = -1;
i64 sum = 0;
bool ok = true;
for(int i = 0; i < (n - half); i++) {
if(mask & (1 << i)) {
if(h[i + half] < last) {
ok = false;
break;
}
last = h[i + half];
sum += g[i + half];
if(fir == -1) {
fir = half + i;
}
}
}
if(ok) {
idxs[fir].emplace_back(sum);
ans += (sum >= k);
}
}
for(int i = 0; i < n; i++) {
sort(idxs[i].begin(), idxs[i].end());
/*
cerr << i << ": ";
for(int &j : idxs[i]) {
cerr << j << " ";
}
cerr << "\n";
*/
}
for(int i = 0; i < half; i++) {
for(int &j : idxs[i]) {
for(int l = half; l < n; l++) {
if(h[i] < h[l]) {
auto it = lower_bound(idxs[l].begin(), idxs[l].end(), k - j);
ans += idxs[l].end() - it;
}
}
}
}
cout << ans;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}