답안 #675046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
675046 2022-12-26T20:48:48 Z Ahmed57 San (COCI17_san) C++14
48 / 120
137 ms 25064 KB
#include<bits/stdc++.h>
using namespace std;
//BIT Fenwick tree
int n ;
int bit[10001]={0};
void add(int e,int v){
    while(e<=n){
        bit[e]+=v;
        e+=e&-e;
    }
}
long long sum(int e){
    long long res = 0;
    while(e>=1){
        res+=bit[e];
        e -= e&-e;
    }
    return res;
}
//end BIT
long long k;
vector<pair<long long,long long>> arr(42);
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>k;
    set<long long> s;
    for(int i = 0;i<n;i++){
        long long a,b;cin>>a>>b;
        s.insert(a);
        arr[i] = {a,b};
    }
    map<long long,long long> compress;
    int z =0;
    for(auto i:s){
        compress[i] = ++z;
    }
    for(int i = 0;i<n;i++){
        arr[i].first = compress[arr[i].first];
    }
    arr[40]={0,0};
    long long ans = 0;
    if(n<=20){
        for(long long mask=1;mask<(1<<n);mask++){
            long long s=0,p=40;
            for(long long i=0;i<n;i++){
                if((1<<i)&mask){
                    if(arr[i].first>=arr[p].first){
                        s+=arr[i].second;
                    }
                    else{
                        s=-1;
                        break;
                    }
                    p=i;
                }
            }
            if(s>=k)ans++;
        }
    }else{
        vector<pair<long long,long long>> v,w;
        for(long long mask=1;mask<(1<<20);mask++){
            long long s=0,p=40;
            for(long long i=0;i<20;i++){
                if((1<<i)&mask){
                    if(arr[i].first>=arr[p].first){
                        s+=arr[i].second;
                    }
                    else{
                        s=-1;
                        break;
                    }
                    p=i;
                }
            }
            if(s>=k)ans++;
            if(s!=-1)v.push_back({s,arr[p].first});
        }
        vector<pair<long long,long long>> cd;
        for(int i = 20;i<n;i++){
            cd.push_back({arr[i].first,arr[i].second});
        }
        n-=20;
        for(long long mask=1;mask<(1<<n);mask++){
            long long s=0,p=40;
            for(long long i=0;i<n;i++){
                if((1<<i)&mask){
                    if(cd[i].first>=cd[p].first){
                        s+=cd[i].second;
                    }
                    else{
                        s=-1;
                        break;
                    }
                    p=i;
                }
            }
            if(s>=k)ans++;
            w.push_back({s,arr[p].first});
        }
        sort(w.begin(),w.end());
        int e = v.size()-1;
        for(int i = 0;i<w.size();i++){
            while(e>=0&&v[e].first+w[i].first>=k){add(v[e].second,1);e--;}
            ans+=sum(w[i].second);
        }
    }
    cout<<ans;
	return 0;
}

Compilation message

san.cpp: In function 'int main()':
san.cpp:102:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for(int i = 0;i<w.size();i++){
      |                       ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 324 KB Output is correct
2 Correct 15 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 75 ms 16824 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 43 ms 4672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 137 ms 25064 KB Output isn't correct
2 Halted 0 ms 0 KB -