Submission #338690

#TimeUsernameProblemLanguageResultExecution timeMemory
338690markussieDetecting Molecules (IOI16_molecules)C++17
0 / 100
1 ms364 KiB
#include "molecules.h"
#include <algorithm>
#include <vector>
#include <numeric>
#include <deque>


using namespace std;

bool intersect(int64_t a, int64_t b, int64_t x, int64_t y)
{
  return a >= x && a <= y || x >= a && x <= b;
}

std::vector<int> find_subset(int d, int u, std::vector<int> w)
{
  int n = w.size();
  vector<int> ind(n);

  iota(ind.begin(), ind.end(), 0);
  sort(ind.begin(), ind.end(), [&](int lhs, int rhs) {return w[lhs] < w[rhs];});
  
  vector<int64_t> sum(n+1);
  for(int i = 1; i <= n; ++i)
    sum[i] = sum[i-1] + w[ind[i-1]];
  
  for(int i = 0; i < w.size(); ++i)
    {
      int l = 1, r = w.size()-i+1;
      while(r - l > 1)
	{
	  int mid = (l + r) / 2;
	  int64_t mn;
	  mn = 1LL * w[ind[i]] * mid + sum[i+mid] - sum[i] - 1LL * mid*w[ind[i]];
	  if(mn > u)
	    r = mid;
	  else
	    l = mid;
	}

      int64_t mn, mx;
      mn = 1LL * w[ind[i]] * l + sum[i+l] - sum[i] - 1LL * l * w[ind[i]];
      mx = 1LL * w[ind[i]] * l + sum[n] - sum[n-l+1] - 1LL * (l-1) * w[ind[i]];
      if(intersect(d, u, mn, mx))
	{
	  int64_t cur_sum = 0;
	  deque<int> tmp;
	 
	  
	  for(int j = 0; j < l; ++j)
	    tmp.push_back(ind[i+j] + 1), cur_sum += w[ind[i+j]];
	  int it = n-1;
	  while(cur_sum < d)
	    {
	      cur_sum -= w[tmp.front()-1];
	      tmp.pop_front();
	      cur_sum += w[ind[it]];
	      tmp.push_back(ind[it] + 1);
	      --it;
	    }
	  vector<int> ans(tmp.begin(), tmp.end());
	  return ans;
	}
    }
  return {0};
}

Compilation message (stderr)

molecules.cpp: In function 'bool intersect(int64_t, int64_t, int64_t, int64_t)':
molecules.cpp:12:17: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   12 |   return a >= x && a <= y || x >= a && x <= b;
      |          ~~~~~~~^~~~~~~~~
molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:27:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |   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...