Submission #281967

#TimeUsernameProblemLanguageResultExecution timeMemory
281967Atill83Detecting Molecules (IOI16_molecules)C++14
100 / 100
77 ms25696 KiB
#include "molecules.h"
#include <bits/stdc++.h> 
using namespace std;
#define ff first
#define ss second
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
int n;
int cnt = 0;
vector<int> ans;
map<pii, int> done;
vector<ll> pre;

int do_it(vector<pll> &vc, int idx, ll l, ll r, int k){
    if(l <= 0 && r >= 0){
        return 1;
    }
    ll len = n - idx;
    ll l1 = pre[idx + k - 1] - (idx != 0 ? pre[idx - 1] : 0), r1 = pre[n - 1] - (k == n ? 0 : pre[n - 1 - k]);
    ll mxL = max(l1, l), mxR = min(r, r1);
    if(mxL <= mxR){
        if(do_it(vc, idx + 1, l - vc[idx].ff, r - vc[idx].ff, k - 1)){
            ans.push_back(vc[idx].ss);
            return 1;
        }else if(do_it(vc, idx + 1, l, r, k)){
            return 1;
        }else{
            assert(0);
            return 0;
        }
    }else{
        return 0;
    }
}

vector<int> find_subset(int l, int u, vector<int> w) {
    ans.clear();
    vector<pll> vc;
    ll top = 0;
    n = w.size();
    for(ll i: w)
        top += i;
    
    if(top < l) return ans;
    for(int i = 0; i < w.size(); i++){
        vc.push_back({w[i], i});
    }
    sort(vc.begin(), vc.end());

    for(int i = 0; i < w.size(); i++){
        pre.push_back(vc[i].ff);
        if(i) pre[i] += pre[i - 1];
    }

    for(int i = 1; i <= n; i++){
        if(do_it(vc, 0, l, u, i)) break;
    }

    return ans;
}

Compilation message (stderr)

molecules.cpp: In function 'int do_it(std::vector<std::pair<long long int, long long int> >&, int, ll, ll, int)':
molecules.cpp:19:8: warning: unused variable 'len' [-Wunused-variable]
   19 |     ll len = n - idx;
      |        ^~~
molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for(int i = 0; i < w.size(); i++){
      |                    ~~^~~~~~~~~~
molecules.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i = 0; i < w.size(); i++){
      |                    ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...