Untitled

 avatar
unknown
plain_text
5 months ago
857 B
3
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int minBoatTrips(int n, int x, vector<int>& weights) {
    // Sort the weights in ascending order
    sort(weights.begin(), weights.end());
    
    int i = 0, j = n - 1;
    int trips = 0;
    
    while (i <= j) {
        // Check if the lightest and heaviest can fit together
        if (weights[i] + weights[j] <= x) {
            // If they fit, take both
            i++;
        }
        // In any case, the heaviest person (at j) takes a trip
        j--;
        trips++;
    }
    
    return trips-1;
}

int main() {
    int n, x;
    cin >> n >> x;
    
    vector<int> weights(n);
    for (int i = 0; i < n; i++) {
        cin >> weights[i];
    }
    
    cout << minBoatTrips(n, x, weights) << endl;
    
    return 0;
}
Editor is loading...
Leave a Comment