Submission #330174

# Submission time Handle Problem Language Result Execution time Memory
330174 2020-11-24T05:53:32 Z saarthak Detecting Molecules (IOI16_molecules) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
#include "molecules.h"

struct weights {
	int wt;
	int idx;
};


bool cmp(weights a, weights b) {
	return (a.wt < b.wt);
}


std::vector<int> find_subset(int l, int u, std::vector<int> w) {
	int n = w.size();
	
	if(l == u)
		if(l%w[0] == 0 && l/w[0] <= n)
			return std::vector<int>(l/w[0], w[0]);			//all weights equal
		
	else if(l == u)
		return std::vector<int>();						//no subarray found
	
	
	int total_weight = 0, i = 0;
	std::queue<int> indices;
	
	vector<struct weights> wts(n);
	
	for(int i = 0; i < n; i++)
		wts[i] = {w[i], i};
	
	std::sort(wts.begin(), wts.end(), cmp);
	
	while(total_weight < l) {
		total_weight += wts[i].wt;
		indices.push(wts[i].idx);
		i++;
	}
	i = 0;
	while(total_weight > u) {
		total_weight -= wts[i].wt;
		indices.pop();
	}
	
	if(total_weight < l || total_weight > u) return std::vector<int>();		//no subarray found
	
	std::vector<int> ans(indices.size());
  	i = 0;
	while(!indices.empty()) {
		ans[i++] = indices.front();
		indices.pop();
	}
	
	return ans;
}

Compilation message

molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:18:4: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   18 |  if(l == u)
      |    ^
molecules.cpp:29:2: error: 'vector' was not declared in this scope; did you mean 'std::vector'?
   29 |  vector<struct weights> wts(n);
      |  ^~~~~~
      |  std::vector
In file included from /usr/include/c++/9/vector:67,
                 from /usr/include/c++/9/queue:61,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:86,
                 from molecules.cpp:1:
/usr/include/c++/9/bits/stl_vector.h:386:11: note: 'std::vector' declared here
  386 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
molecules.cpp:29:9: error: expected primary-expression before 'struct'
   29 |  vector<struct weights> wts(n);
      |         ^~~~~~
molecules.cpp:32:3: error: 'wts' was not declared in this scope
   32 |   wts[i] = {w[i], i};
      |   ^~~
molecules.cpp:34:12: error: 'wts' was not declared in this scope
   34 |  std::sort(wts.begin(), wts.end(), cmp);
      |            ^~~