# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116734 | 2024-11-22T09:09:24 Z | SalihSahin | Detecting Molecules (IOI16_molecules) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> #define pb push_back #define int long long using namespace std; #include "molecules.h" vector<int> find_subset(int l, int u, vector<int> w) { int n = w.size(); vector<pair<int, int> > a(n); for(int i = 0; i < n; i++){ a[i] = {w[i], i}; } sort(a.begin(), a.end()); vector<int> pre(n+1); for(int i = 0; i < n; i++){ pre[i+1] = pre[i] + a[i].first; } vector<int> ans; int r = 0; int st = -1, sz = -1, lst = -1; for(int i = 0; i < n; i++){ while(r < n && a[r].first - a[i].first <= u - l) r++; bool ok = 0; int x = 0; int lp = 1, rp = r - i; while(lp < rp){ int mid = (lp + rp)/2; if(pre[i + mid] - pre[i] >= u) rp = mid; else lp = mid + 1; } if(lp > 1 && pre[i + lp] - pre[i] > u) lp--; if(pre[i + lp] - pre[i] > u) continue; int cntlmax = lp; // cntlmax ya da daha az kisi olacak lp = 1, rp = r - i; while(lp < rp){ int mid = (lp + rp)/2; if(pre[r] - pre[r - mid] < l) lp = mid + 1; else rp = mid; } if(pre[r] - pre[r - lp] < l) continue; int cntrmin = lp; if(cntrmin <= cntlmax){ x = cntrmin; ok = 1; } if(ok){ st = i; sz = x; lst = r-1; break; } } if(st == -1) return ans; else{ //cout<<st<<" den baslayan "<<sz<<" sizelık array dondurcez"<<endl; // [st, lst] aralıgından sz sizelık secicez int sum = 0, rem = sz; for(int i = 0; i < sz; i++){ sum += a[i + st].first; } for(int i = lst; i >= st; i--){ if(!rem) break; int now = a[i].first; int added = a[st + rem - 1].first; if(sum + (now - added) <= u){ ans.pb(a[i].second); rem--; sum += (now - added); } } return ans; } }