q2.cpp
· 1.8 KiB · C++
Raw
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
// Struct to represent a segment with L, R, and cost
struct Segment {
int L, R, cost;
Segment(int left, int right, int c) : L(left), R(right), cost(c) {}
};
// Function to calculate the minimum product of valid non-overlapping segments
long long findMinCostProduct(const vector<Segment> &segments) {
int N = segments.size();
// Sorting by left endpoint (L)
vector<Segment> sortedByL = segments;
sort(sortedByL.begin(), sortedByL.end(), [](Segment a, Segment b) {
if (a.L != b.L) return a.L < b.L;
return a.R < b.R;
});
// Sorting by right endpoint (R)
vector<Segment> sortedByR = segments;
sort(sortedByR.begin(), sortedByR.end(), [](Segment a, Segment b) {
if (a.R != b.R) return a.R < b.R;
return a.L < b.L;
});
long long minCostSeen = LLONG_MAX; // Store the minimum cost of valid non-overlapping segments
long long minProduct = LLONG_MAX; // Store the minimum product of costs for valid pairs
int rIndex = 0;
// Iterate over sortedByL (by left endpoint)
for (const Segment ¤t : sortedByL) {
// Check for valid non-overlapping segments (sortedByR's R < current.L)
while (rIndex < N && sortedByR[rIndex].R < current.L) {
minCostSeen = min(minCostSeen, (long long)sortedByR[rIndex].cost);
++rIndex;
}
// If a valid segment is found, calculate the product of costs
if (minCostSeen != LLONG_MAX) {
long long product = minCostSeen * (long long)current.cost;
minProduct = min(minProduct, product);
}
}
// Return the result
return (minProduct != LLONG_MAX) ? minProduct : -1; // Return -1 if no valid pair is found
}
1 | #include <iostream> |
2 | #include <vector> |
3 | #include <algorithm> |
4 | #include <climits> |
5 | #include<bits/stdc++.h> |
6 | |
7 | using namespace std; |
8 | |
9 | #define ll long long |
10 | |
11 | // Struct to represent a segment with L, R, and cost |
12 | struct Segment { |
13 | int L, R, cost; |
14 | Segment(int left, int right, int c) : L(left), R(right), cost(c) {} |
15 | }; |
16 | |
17 | // Function to calculate the minimum product of valid non-overlapping segments |
18 | long long findMinCostProduct(const vector<Segment> &segments) { |
19 | int N = segments.size(); |
20 | |
21 | // Sorting by left endpoint (L) |
22 | vector<Segment> sortedByL = segments; |
23 | sort(sortedByL.begin(), sortedByL.end(), [](Segment a, Segment b) { |
24 | if (a.L != b.L) return a.L < b.L; |
25 | return a.R < b.R; |
26 | }); |
27 | |
28 | // Sorting by right endpoint (R) |
29 | vector<Segment> sortedByR = segments; |
30 | sort(sortedByR.begin(), sortedByR.end(), [](Segment a, Segment b) { |
31 | if (a.R != b.R) return a.R < b.R; |
32 | return a.L < b.L; |
33 | }); |
34 | |
35 | long long minCostSeen = LLONG_MAX; // Store the minimum cost of valid non-overlapping segments |
36 | long long minProduct = LLONG_MAX; // Store the minimum product of costs for valid pairs |
37 | |
38 | int rIndex = 0; |
39 | |
40 | // Iterate over sortedByL (by left endpoint) |
41 | for (const Segment ¤t : sortedByL) { |
42 | // Check for valid non-overlapping segments (sortedByR's R < current.L) |
43 | while (rIndex < N && sortedByR[rIndex].R < current.L) { |
44 | minCostSeen = min(minCostSeen, (long long)sortedByR[rIndex].cost); |
45 | ++rIndex; |
46 | } |
47 | |
48 | // If a valid segment is found, calculate the product of costs |
49 | if (minCostSeen != LLONG_MAX) { |
50 | long long product = minCostSeen * (long long)current.cost; |
51 | minProduct = min(minProduct, product); |
52 | } |
53 | } |
54 | |
55 | // Return the result |
56 | return (minProduct != LLONG_MAX) ? minProduct : -1; // Return -1 if no valid pair is found |
57 | } |
1 | int minMoves(vector<int>arr, int n, int k) { |
2 | |
3 | sort(arr.begin(), arr.end()); |
4 | |
5 | int i = 0, j = n - 1; |
6 | int moves = 0; |
7 | |
8 | while (i <= j) { |
9 | |
10 | if (arr[i] + arr[j] <= k) { |
11 | i++; |
12 | } |
13 | |
14 | j--; |
15 | |
16 | moves++; |
17 | } |
18 | |
19 | return moves; |
20 | } |