Submission #804872

#TimeUsernameProblemLanguageResultExecution timeMemory
804872BoasDetecting Molecules (IOI16_molecules)C++17
0 / 100
1 ms340 KiB
#include "molecules.h"

using namespace std;
#include <bits/stdc++.h>

#define MAX_N 200001
#define ALL(x) x.begin(), x.end()
#define loop(x) for (int i = 0; i < (x); i++)
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef set<int> si;

#define int uint64_t

int diff(int i, int l, int u)
{
    if (i < l)
        return l - i;
    if (i > u)
        return i - u;
    return 0;
}
#undef int
vi find_subset(int l, int u, vi w)
{
#define int uint64_t
    int alles = accumulate(ALL(w), 0);
    if (alles < l)
        return {};
    if (alles <= u)
    {
        vi ret;
        loop(w.size()) ret.push_back(i);
        return ret;
    }
    vii sortedMolecules; // first weight, second real index
    loop(w.size())
    {
        if (w[i] > u)
            continue;
        sortedMolecules.push_back({w[i], i});
    }
    sort(ALL(sortedMolecules));
    si used;
    bitset<MAX_N> usin;
    int totalW = 0;
    while (totalW < l)
    {
        bool added = false;
        for (int i = sortedMolecules.size() - 1; i >= 0; i--)
        {
            if (usin[i])
                continue;
            int W = sortedMolecules[i].first; // weight, real index
            if (totalW + W > u)
            {
                continue;
            }
            added = true;
            used.insert(i);
            usin[i] = true;
            totalW += W;
        }
        if (!added)
            break;
    }
    if (totalW >= l)
    {
        vi indexesOfUsedMols;
        for (int i : used)
        {
            indexesOfUsedMols.push_back(sortedMolecules[i].second);
        }
        return indexesOfUsedMols;
    }
    if (sortedMolecules.size() < 1)
        return {};
    int smallestW = sortedMolecules[0].first;
    used.insert(0);
    totalW += smallestW;
    usin[0] = true;
    si oldUsed(used);            // sorted indexes
    bitset<MAX_N> oldUsin(usin); // sorted indexes
    int oldW = totalW;
    for (int i1 = 0; i1 < sortedMolecules.size(); i1++)
    {
        used = oldUsed;
        usin = oldUsin;
        if (usin[i1])
            continue;
        ii p1 = sortedMolecules[i1];
        for (int i2 : oldUsed)
        {
            used = oldUsed;
            usin = oldUsin;
            ii p2 = sortedMolecules[i2];
            used = oldUsed;
            used.erase(i2);
            usin[i2] = false;
            used.insert(i1);
            usin[i1] = true;
            totalW = oldW - p2.first + p1.first;
            if (diff(totalW, l, u) > diff(oldW, l, u))
                continue;
            while (totalW < l)
            {
                bool added = false;
                for (int i = sortedMolecules.size() - 1; i >= 0; i--)
                {
                    if (usin[i])
                        continue;
                    int W = sortedMolecules[i].first;
                    if (totalW + W > u)
                    {
                        continue;
                    }
                    added = true;
                    used.insert(i);
                    usin[i] = true;
                    totalW += W;
                }
                if (!added)
                    break;
            }
            if (totalW >= l && totalW <= u)
            {
                vi indexesOfUsedMols;
                for (int i : used)
                {
                    indexesOfUsedMols.push_back(sortedMolecules[i].second);
                }
                return indexesOfUsedMols;
            }
            if (diff(totalW, l, u) < diff(oldW, l, u))
            {
                oldW = totalW;
                oldUsed = used;
                oldUsin = usin;
                i1 = 0;
                break;
            }
        }
    }
    return {};
}

Compilation message (stderr)

molecules.cpp: In function 'vi find_subset(int, int, vi)':
molecules.cpp:29:15: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |     if (alles < l)
      |         ~~~~~~^~~
molecules.cpp:31:15: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   31 |     if (alles <= u)
      |         ~~~~~~^~~~
molecules.cpp:48:19: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   48 |     while (totalW < l)
      |            ~~~~~~~^~~
molecules.cpp:56:28: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   56 |             if (totalW + W > u)
      |                 ~~~~~~~~~~~^~~
molecules.cpp:68:16: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   68 |     if (totalW >= l)
      |         ~~~~~~~^~~~
molecules.cpp:106:27: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  106 |             while (totalW < l)
      |                    ~~~~~~~^~~
molecules.cpp:114:36: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  114 |                     if (totalW + W > u)
      |                         ~~~~~~~~~~~^~~
molecules.cpp:126:24: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  126 |             if (totalW >= l && totalW <= u)
      |                 ~~~~~~~^~~~
molecules.cpp:126:39: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  126 |             if (totalW >= l && totalW <= u)
      |                                ~~~~~~~^~~~
#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...