Submission #780705

#TimeUsernameProblemLanguageResultExecution timeMemory
780705JoenPoenManDetecting Molecules (IOI16_molecules)C++17
19 / 100
1 ms340 KiB

#include "molecules.h"
#include <bits/stdc++.h>
 
using namespace std;
#define ALL(arr) begin(arr), end(arr)

typedef pair<int, int> ii;

std::vector<int> find_subset(int l, int u, std::vector<int> w) {
    vector<bool> subset(w.size(), false);
    vector<ii> sw;
    long int sum = 0;
    for (int i = 0; i < w.size(); i++) {
        sw.push_back({w[i], i});
    }
    sort(ALL(sw));
    for (int i = 0; i < sw.size() && sum < l; i++) {
        sum += sw[i].first;
        subset[sw[i].second] = true;
    }
    int prevInd = -1;
    int prevRemInd = -1;
    for (int attempts = 1.5*w.size(); attempts >= 0; attempts--) {
        if (sum < l) {
            prevRemInd = -1;
            bool suc = false;
            if (sw.back().first + sum >= l) for (auto [weight, ind] : sw) if (!subset[ind]) {
                if (sum + weight >= l) {
                    subset[ind] = true;
                    sum += weight;
                    suc = true;
                    break;
                }
            }
            if (!suc) {
                for (int i = max(0, prevInd); i < sw.size(); i++) if (!subset[sw[i].second]) {
                    auto [weight, ind] = sw[i];
                    subset[ind] = true;
                    sum += weight;
                    suc = true;
                    prevInd = i;
                    break;
                }
            }
            if (!suc) break;
        } else if (sum > u) {
            bool suc = false;
            prevInd = -1;
            if (sum - sw.back().first <= u) for (int i = 0; i < sw.size(); i++) if (subset[sw[i].second]) {
                if (sum - sw[i].first <= u) {
                    auto [weight, ind] = sw[i];
                    sw.erase(sw.begin()+i);
                    subset[ind] = false;
                    sum -= weight;
                    suc = true;
                    break;
                }
            }
            if (!suc) {
                for (int i = max(0, prevRemInd); i < sw.size(); i++) if (subset[sw[i].second]) {
                    auto [weight, ind] = sw[i];
                    sw.erase(sw.begin()+i);
                    subset[ind] = false;
                    sum -= weight;
                    prevRemInd = i;
                    break;
                }
            }
        } else {
            vector<int> res;
            for (int n = 0; n < subset.size(); n++) if (subset[n]) res.push_back(n);
            return res;
        }
    }
    
 
    return std::vector<int>(0);
}

Compilation message (stderr)

molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for (int i = 0; i < w.size(); i++) {
      |                     ~~^~~~~~~~~~
molecules.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for (int i = 0; i < sw.size() && sum < l; i++) {
      |                     ~~^~~~~~~~~~~
molecules.cpp:37:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |                 for (int i = max(0, prevInd); i < sw.size(); i++) if (!subset[sw[i].second]) {
      |                                               ~~^~~~~~~~~~~
molecules.cpp:50:63: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |             if (sum - sw.back().first <= u) for (int i = 0; i < sw.size(); i++) if (subset[sw[i].second]) {
      |                                                             ~~^~~~~~~~~~~
molecules.cpp:61:52: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |                 for (int i = max(0, prevRemInd); i < sw.size(); i++) if (subset[sw[i].second]) {
      |                                                  ~~^~~~~~~~~~~
molecules.cpp:72:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bool>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |             for (int n = 0; n < subset.size(); n++) if (subset[n]) res.push_back(n);
      |                             ~~^~~~~~~~~~~~~~~
#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...