Submission #473242

# Submission time Handle Problem Language Result Execution time Memory
473242 2021-09-15T10:46:19 Z HossamHero7 San (COCI17_san) C++14
48 / 120
1000 ms 47344 KB
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef vector <ll> row;
typedef vector <row> matrix;
#define endl '\n'
#define fast ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define all(v) ((v).begin()), ((v).end())
#define allR(v) ((v).rbegin()), ((v).rend())
#define For(i,n) for(int i=0;i<(int)(n);i++)
#define sz(v) v.size()
#define swapS(a,b) if(a>b) swap(a,b)
const ll mod = 1e9+7;
const ll OO = 1e18;
const ll invalid = -1e18;
int dx[] = {0,0,-1,1,1,1,-1,-1};
int dy[] = {1,-1,0,0,1,-1,-1,1};
ll fixMod(ll x){
    return ((x%mod) + mod) % mod;
}
ll lcm(ll a,ll b){
    return max(a,b)/__gcd(a,b)*min(a,b);
}
ll power(ll a,ll b){
    if(b==0) return 1;
    if(b==1) return a;
    if(b&1){
        ll y=power(a,b/2);
        return (y*y*a);
    }
    else {
        ll y=power(a,b/2);
        return (y*y);
    }
}
ll inv(ll n){
    return power(n,(mod-2));
}
matrix zero(int n,int m){
    return matrix(n,row(m,0));
}
matrix identity(int n){
    matrix ret = zero(n,n);
    for(int i=0;i<n;i++){
        ret[i][i] = 1;
    }
    return ret;
}
matrix multiply(const matrix &a, const matrix &b){
    matrix ret = zero(a.size(),b[0].size());
    for(int i=0;i<a.size();i++){
        for(int k=0;k<a[0].size();k++){
            for(int j=0;j<b[0].size();j++){
                ret[i][j] += a[i][k] * b[k][j];
                ret[i][j] = ret[i][j];
            }
        }
    }
    return ret;
}
matrix matrixPower(matrix &a,ll b){
    if(b == 0) return identity(a.size());
    if(b == 1) return a;
    matrix r = matrixPower(a,b/2);
    if(b&1) return multiply(multiply(r,r),a);
    return multiply(r,r);
}
void solve(){
    ll n,k;
    cin>>n>>k;
    vector <pair<ll,ll>> v(n);
    ll ans =0 ;
    for(auto &i:v) cin>>i.first>>i.second;
    map <vector<int>,bool> mp;
    ll n1 = 0;
    ll n2 = 0;
    vector <pair<ll,ll>> v1;
    vector <pair<ll,ll>> v2;
    for(int i=0;i<n/2;i++){
        v1.push_back(v[i]);
    }
    for(int i=n/2;i<n;i++){
        v2.push_back(v[i]);
    }
    n1 = v1.size();
    n2 = v2.size();
    vector <pair<ll,ll>> mask1;
    vector <pair<ll,ll>> mask2;
    for(int mask=0;mask<(1<<n1);mask++){
        ll sum = 0;
        ll prev = 0;
        vector <int> temp;
        for(int i=0;i<n1;i++){
            int take = (1<<i) & mask;
            if(take){
                if(v1[i].first >= prev){
                    temp.push_back(i);
                    sum += v1[i].second;
                    prev = v1[i].first;
                }
            }
        }
        if(!mp[temp]){
            mask1.push_back({prev,sum});
        }
        mp[temp] = 1;
    }
    mp.clear();
    for(int mask=0;mask<(1<<n2);mask++){
        ll sum = 0;
        ll prev = 0;
        vector <int> temp;
        for(int i=0;i<n2;i++){
            int take = (1<<i) & mask;
            if(take){
                if(v2[i].first >= prev){
                    temp.push_back(i);
                    sum += v2[i].second;
                    prev = v2[i].first;
                }
            }
        }
        if(!mp[temp]){
            if(temp.size()) mask2.push_back({v2[temp[0]].first,sum});
            else mask2.push_back({1e18,sum});
        }
        mp[temp] = 1;
    }
    for(auto i : mask1){
        for(auto j : mask2){
            if(j.first>=i.first && j.second+i.second>=k) ans ++;
        }
    }
    cout<<ans<<endl;
}
int main()
{
    //fast
    ll t=1;
    // cin>>t;
    while(t--){
        //Some important Notes:
        //  1- Any minus with mod fixed mod
        //  2- Any divide with mod inverse
        //  3- Any mod problem make mod in every where
        //  4- Any dp problem don't forget to return the saved value :)
        solve();
    }
    return 0;
}

Compilation message

san.cpp: In function 'matrix multiply(const matrix&, const matrix&)':
san.cpp:52:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for(int i=0;i<a.size();i++){
      |                 ~^~~~~~~~~
san.cpp:53:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for(int k=0;k<a[0].size();k++){
      |                     ~^~~~~~~~~~~~
san.cpp:54:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |             for(int j=0;j<b[0].size();j++){
      |                         ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 9876 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1094 ms 21556 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1076 ms 47344 KB Time limit exceeded
2 Halted 0 ms 0 KB -