Submission #1319142

#TimeUsernameProblemLanguageResultExecution timeMemory
1319142tkm_algorithmsDetecting Molecules (IOI16_molecules)C++20
100 / 100
40 ms11272 KiB
#include <bits/stdc++.h>
#include "molecules.h"

using namespace std;
using ll = long long;
//#define int ll
using P = pair<ll, int>;
#define all(x) x.begin(), x.end()
#define rep(i, l, n) for (int i = l; i < (n); ++i)
#define sz(x) (int)x.size()
const char nl = '\n';
const int mod = 1e9+7;

vector<int> find_subset(int l, int u, vector<int> w) {
	vector<P> nw; int n = sz(w);
	rep(i, 0, n)nw.push_back(P(w[i], i));
	sort(all(nw));
	ll sm = 0;
	
	priority_queue<P> pq;
	for (int i = n-1; i >= 0; --i) {
		sm += nw[i].first;
		pq.push(nw[i]);
		if (sm >= l)break;
	}
	
	//cout << sm << nl;
	
	vector<int> av;
	for (int i = n-sz(pq)-1; i >= 0; --i) {
		av.push_back(nw[i].second);
		//cout << nw[i].first << nl;
	}
	
	//reverse(all(av));
	
	if (sm < l || nw[0].first > u)return {};
	while (sm > u && sz(av)) {
		sm -= pq.top().first; pq.pop();
		int i = av.back(); av.pop_back();
		sm += w[i]; pq.push(P(w[i], i));
	}
	
	vector<int> res;
	if (l <= sm && sm <= u)
		while (!pq.empty()) {
			res.push_back(pq.top().second);
			pq.pop();
		}
	return res;
}

//int main() {
   //int n; cin >> n;
   //vector<int> w(n); 
   //for (auto &i: w)cin >> i;
   
   //int l, u; cin >> l >> u;
   //vector<int> res = find_subset(l, u, w);
   //set<int> s;
   //for (auto i: res)s.insert(i);
   //int sm = 0;
   //for (auto i: res)sm += w[i];
   //if (sz(s) == sz(res) && !res.empty() && sm >= l && sm <= u)cout << "YES" << nl;
   //else cout << "NO" << nl;
   //return 0;
//}

Compilation message (stderr)

molecules.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
molecules_c.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
#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...