#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define MP make_pair
#define ft first
#define sd second
#define PB push_back
#define pli pair<ll,int>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
const int N = 45;
const int PW = 22;
const int oo = 2e9;
ordered_set<pli> st;
vector<ll> vls[N];
ll h[N], g[N], k, sum[(1 << PW)], ans = 0;
int n, mid, nm[N];
void calc(int l, int r){
int len = (r - l + 1);
fill(sum, sum + (1 << len), -1);
sum[0] = 0;
for (int msk = 0; msk < (1 << len); msk++){
if (sum[msk] < 0) continue;
if (l == 0 && sum[msk] >= k)
ans++;
int fi = 0, se = len - 1;
while (fi < len && !(msk & (1 << fi)))
fi++;
while (se >= 0 && !(msk & (1 << se)))
se--;
for (int i = 0; i < len; i++)
if (!(msk & (1 << i))){
int id = i + l;
ll nw_sum = sum[msk] + g[id];
int nmsk = (msk | (1 << i));
if (sum[nmsk] > 0) continue;
if (msk == 0){
vls[i + l].PB(nw_sum);
sum[nmsk] = nw_sum;
} else {
if (l == 0){
if (i > se && h[id] >= h[se + l]){
vls[i + l].PB(nw_sum);
sum[nmsk] = nw_sum;
}
} else {
if (i > se && h[id] >= h[se + l]){
vls[i + fi].PB(nw_sum);
sum[nmsk] = nw_sum;
}
}
}
}
}
}
bool cmp(int _x, int _y){
return h[_x] > h[_y];
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> h[i] >> g[i];
nm[i] = i;
}
if (n == 1){
cout << (g[0] >= k ? 1 : 0);
return 0;
}
mid = n / 2;
calc(0, mid - 1);
calc(mid, n - 1);
sort(nm, nm + mid, cmp);
sort(nm + mid + 1, nm + n, cmp);
st.clear();
int ite = 0;
for (int i = 0, ptr = mid; i < mid; i++){
while (ptr < n && h[nm[ptr]] >= h[nm[i]]){
for (ll cr : vls[nm[ptr]])
st.insert(MP(cr, ite++));
ptr++;
}
for (ll cr : vls[nm[i]]){
ll ost = k - cr;
ans += sz(st) - st.order_of_key(*st.lower_bound(MP(ost, -1)));
}
}
ans += sz(st) - st.order_of_key(*st.lower_bound(MP(k, -1)));
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
4500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
372 ms |
37204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |