Submission #631291

# Submission time Handle Problem Language Result Execution time Memory
631291 2022-08-18T02:47:17 Z Soul234 San (COCI17_san) C++14
120 / 120
125 ms 7592 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;

const int MX = 45;
int N, H[MX], G[MX];
ll K;
vi hs;
V<ll> lstL[MX], lstR[MX];

void backtrack1(int ind, int lst, ll cursum) {
    if(ind == N/2) {
        int idx = lower_bound(all(hs), lst) - begin(hs);
        lstL[idx].pb(cursum);
        return;
    }
    backtrack1(ind+1, lst, cursum);
    if(H[ind] >= lst) {
        backtrack1(ind+1, H[ind], cursum + G[ind]);
    }
}

void backtrack2(int ind, int fst, int lst, ll cursum) {
    if(ind == N) {
        int idx = lower_bound(all(hs), fst) - begin(hs);
        lstR[idx].pb(cursum);
        return;
    }
    backtrack2(ind+1, fst, lst, cursum);
    if(H[ind] >= lst) {
        int nfst = fst ? fst : H[ind];
        backtrack2(ind+1, nfst, H[ind], cursum + G[ind]);
    }
}

void solve() {
    cin >> N >> K;
    F0R(i, N) {
        cin >> H[i] >> G[i];
        hs.pb(H[i]);
    }
    hs.pb(0);
    sort(all(hs)); hs.erase(unique(all(hs)), hs.end());
    backtrack1(0, 0, 0);
    backtrack2(N/2, 0, 0, 0);
    F0R(i, MX) sort(all(lstR[i]));
    ll ans = 0;
    F0R(i, MX) {
        each(it, lstL[i]) {
            ans += lstR[0].end() - lower_bound(all(lstR[0]), K - it);
            FOR(j,i,MX) ans += lstR[j].end() - lower_bound(all(lstR[j]), K - it);
        }
    }
    cout << ans << nl;
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

san.cpp: In function 'void setIO(std::string)':
san.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
san.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 1236 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 1936 KB Output is correct
2 Correct 5 ms 560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 7592 KB Output is correct
2 Correct 21 ms 1648 KB Output is correct