Submission #707568

#TimeUsernameProblemLanguageResultExecution timeMemory
707568Error42Detecting Molecules (IOI16_molecules)C++14
100 / 100
53 ms8652 KiB
#include "molecules.h"

#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;
using ll = long long;

vector<int> find_subset(
    int const _l,
    int const _u,
    vector<int> _w
) {
    ll const l = _l;
    ll const u = _u;

    vector<pair<ll, int>> w(_w.size());

    for (int i = 0; i < w.size(); i++) {
        w[i].first = _w[i];
        w[i].second = i;
    }

    sort(w.begin(), w.end());

    vector<ll> prefix(w.size() + 1);

    for (int i = 1; i <= w.size(); i++)
        prefix[i] = prefix[i - 1] + w[i - 1].first;

    for (int mol_count = 1; mol_count <= w.size(); mol_count++) {
        int lo = 0;
        int hi = mol_count;

        while (lo <= hi) {
            int const mi = (lo + hi) / 2;
            int const top = mi;
            int const bottom = mol_count - mi; 

            ll val = prefix[bottom] + prefix[w.size()] - prefix[w.size() - top];

            if (l <= val && val <= u) {
                vector<int> solution;

                for (int i = 0; i < bottom; i++)
                    solution.push_back(w[i].second);

                for (int i = w.size() - top; i < w.size(); i++)
                    solution.push_back(w[i].second);

                return solution;
            }
            
            if (val < l)
                lo = mi + 1;
            else
                hi = mi - 1;
        }
    }

    return {};
}

Compilation message (stderr)

molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 0; i < w.size(); i++) {
      |                     ~~^~~~~~~~~~
molecules.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 1; i <= w.size(); i++)
      |                     ~~^~~~~~~~~~~
molecules.cpp:32:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (int mol_count = 1; mol_count <= w.size(); mol_count++) {
      |                             ~~~~~~~~~~^~~~~~~~~~~
molecules.cpp:49:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |                 for (int i = w.size() - top; 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...