Submission #651835

#TimeUsernameProblemLanguageResultExecution timeMemory
651835alvinpiterDetecting Molecules (IOI16_molecules)C++17
9 / 100
1 ms316 KiB
#include "molecules.h"
#include<bits/stdc++.h>
using namespace std;

std::vector<int> find_subset(int l, int u, std::vector<int> w) {
  vector<pair<int, int> > indexedWeights;
  for (int i = 0; i < w.size(); i++) {
    indexedWeights.push_back({w[i], i});
  }

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

  vector<int> result;

  // Check if the lightest molecule is larger than u
  if (indexedWeights[0].first > u) {
    return result;
  }

  // Check if there is a molecule with weight between l and u
  for (auto [weight, index]: indexedWeights) {
    if (weight >= l && weight <= u) {
      result.push_back(index);
      return result;
    }
  }

  // All molecules has weight < l

  long long int totalWeight = 0;
  for (int i = indexedWeights.size() - 1; i >= 0; i--) {
    auto [weight, index] = indexedWeights[i];

    totalWeight += weight;
    result.push_back(index);

    if (totalWeight >= l && totalWeight <= u) {
      return result;
    }

    if (totalWeight > u) {
      result.clear();
      return result;
    }

    int lo = 0, hi = i - 1, mid;
    while (hi >= lo) {
      mid = (lo + hi)/2;
      if (totalWeight + indexedWeights[mid].first < l) {
        lo = mid + 1;
      } else {
        hi = mid - 1;
      }
    }

    if (lo < i) {
      if (totalWeight + indexedWeights[lo].first <= u) {
        result.push_back(indexedWeights[lo].second);
        return result;
      }
    }
  }

  result.clear();
  return result;
}

Compilation message (stderr)

molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:7:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 |   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...