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 <iostream>
#include <numeric>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define sp ' '
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define forn(YY, yy) for(long long int yy = 0; yy < YY; ++yy)
#define prn(XX) cout << XX << endl
#define prs(XX) cout << XX << " "
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
const int MOD = 1e9 + 7;
const int INF = 2e9 + 13;
const int mINF = -2e9 - 13;
const double PI = 3.14159265358979;
const double EPS = 1e-9;
const int MAXN = 42;
int n, h[MAXN], g[MAXN];
ll k;
vector<int> hs;
vll L[MAXN], R[MAXN];
void rec_left(int pos, int last, ll sum){
if(pos == n/2){
int idx = lower_bound(hs.begin(), hs.end(), last) - hs.begin();
L[idx].pb(sum);
return;
}
rec_left(pos+1, last, sum);
if(h[pos] >= last)
rec_left(pos+1, h[pos], sum + g[pos]);
}
void rec_right(int pos, int first, int last, ll sum){
if(pos == n){
int idx = lower_bound(hs.begin(), hs.end(), first) - hs.begin();
R[idx].pb(sum);
return;
}
rec_right(pos+1, first, last, sum);
if(h[pos] >= last){
int nfirst = (first == 0) ? h[pos] : first;
rec_right(pos+1, nfirst, h[pos], sum + g[pos]);
}
}
int main(int argc, char **argv){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
for(int i = 0; i < n; ++i){
cin >> h[i] >> g[i];
hs.pb(h[i]);
}
hs.pb(0);
sort(hs.begin(), hs.end());
rec_left(0, 0, 0);
rec_right(n/2, 0, 0, 0);
ll ans = 0;
for(int i = 0; i < (int)hs.size(); ++i)
sort(R[i].begin(), R[i].end());
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 << endl;
return 0;
}
//cikisir
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |