Submission #1144130

#TimeUsernameProblemLanguageResultExecution timeMemory
1144130crispxxDetecting Molecules (IOI16_molecules)C++20
69 / 100
33 ms4028 KiB
#include <bits/stdc++.h>
#include "molecules.h"

// #include "grader.cpp"

using namespace std;

#define all(x) x.begin(), x.end()
#define pb push_back
#define nl '\n'

// vector<int> find_subset(int l, int u, vector<int> w) {
	// int n = w.size();
	
	// vector<int> dp(u + 1), p(u + 1);
	
	// dp[0] = 1;
	
	// for(int i = 0; i < n; i++) {
		// for(int j = u; j >= w[i]; j--) {
			// if(dp[j - w[i]]) {
				// dp[j] = 1;
				// p[j] = i;
			// }
		// }
	// }
	
	// int j = -1;
	
	// for(int i = l; i <= u; i++) {
		// if(dp[i]) {
			// j = i;
			// break;
		// }
	// }
	
	// if(j == -1) return {};
	
	// vector<int> ans;
	
	// while(j > 0) {
		// int idx = p[j];
		
		// if(idx == -1) break;
		
		// ans.pb(idx);
		// j -= w[idx];
	// }
	
	// return ans;
// }

vector<int> find_subset(int l, int u, vector<int> w) {
    int n = w.size();
    vector<pair<int, int>> weight_index;
    for (int i = 0; i < n; i++) {
        weight_index.push_back({w[i], i});
    }
    
    // Sort by weight
    sort(weight_index.begin(), weight_index.end());
    
    int sum = 0;
    int left = 0;
    
    // Two pointers approach
    for (int right = 0; right < n; right++) {
        sum += weight_index[right].first;
        
        // Shrink the window if sum exceeds 'u'
        while (sum > u && left <= right) {
            sum -= weight_index[left].first;
            left++;
        }
        
        // Check if current sum is within the range
        if (sum >= l && sum <= u) {
            vector<int> result;
            for (int i = left; i <= right; i++) {
                result.push_back(weight_index[i].second);
            }
            return result;
        }
    }
    
    return {}; // No valid subset found
}

/**
5 15 17
5 5 5 5 6

5 6 10
2 5 7 5 3


**/

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...