q2.cpp
· 1.6 KiB · C++
Неформатований
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
# updated
ll minCostProduct(const vector<vector<ll>> &seg)
{
int n = seg.size();
// Sort by starting endpoint (L)
vector<vector<ll>> sortedL = seg;
sort(sortedL.begin(), sortedL.end(), [](const vector<ll> &a, const vector<ll> &b)
{
if (a[0] != b[0])
return a[0] < b[0];
return a[1] < b[1]; });
// Sort by ending endpoint (R)
vector<vector<ll>> sortedR = seg;
sort(sortedR.begin(), sortedR.end(), [](const vector<ll> &a, const vector<ll> &b)
{
if (a[1] != b[1])
return a[1] < b[1];
return a[0] < b[0]; });
ll minCost = LLONG_MAX; // Minimum cost of valid non-overlapping segments
ll minProd = LLONG_MAX; // Minimum product of costs for valid pairs
int r = 0;
// Iterate over sortedL (by left endpoint)
for (const auto &cur : sortedL)
{
// Check for valid non-overlapping segments (sortedR's R < cur.L)
while (r < n && sortedR[r][1] < cur[0])
{
minCost = min(minCost, sortedR[r][2]); // sortedR[r][2] is the cost
++r;
}
// Calculate the product of costs if a valid segment is found
if (minCost != LLONG_MAX)
{
ll prod = minCost * cur[2];
minProd = min(minProd, prod);
}
}
// Return the result
return (minProd != LLONG_MAX) ? minProd : -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 | using namespace std; |
7 | #define ll long long |
8 | # updated |
9 | ll minCostProduct(const vector<vector<ll>> &seg) |
10 | { |
11 | int n = seg.size(); |
12 | |
13 | // Sort by starting endpoint (L) |
14 | vector<vector<ll>> sortedL = seg; |
15 | sort(sortedL.begin(), sortedL.end(), [](const vector<ll> &a, const vector<ll> &b) |
16 | { |
17 | if (a[0] != b[0]) |
18 | return a[0] < b[0]; |
19 | return a[1] < b[1]; }); |
20 | |
21 | // Sort by ending endpoint (R) |
22 | vector<vector<ll>> sortedR = seg; |
23 | sort(sortedR.begin(), sortedR.end(), [](const vector<ll> &a, const vector<ll> &b) |
24 | { |
25 | if (a[1] != b[1]) |
26 | return a[1] < b[1]; |
27 | return a[0] < b[0]; }); |
28 | |
29 | ll minCost = LLONG_MAX; // Minimum cost of valid non-overlapping segments |
30 | ll minProd = LLONG_MAX; // Minimum product of costs for valid pairs |
31 | |
32 | int r = 0; |
33 | |
34 | // Iterate over sortedL (by left endpoint) |
35 | for (const auto &cur : sortedL) |
36 | { |
37 | // Check for valid non-overlapping segments (sortedR's R < cur.L) |
38 | while (r < n && sortedR[r][1] < cur[0]) |
39 | { |
40 | minCost = min(minCost, sortedR[r][2]); // sortedR[r][2] is the cost |
41 | ++r; |
42 | } |
43 | |
44 | // Calculate the product of costs if a valid segment is found |
45 | if (minCost != LLONG_MAX) |
46 | { |
47 | ll prod = minCost * cur[2]; |
48 | minProd = min(minProd, prod); |
49 | } |
50 | } |
51 | |
52 | // Return the result |
53 | return (minProd != LLONG_MAX) ? minProd : -1; // Return -1 if no valid pair is found |
54 | } |
q3.cpp
· 298 B · C++
Неформатований
int minMoves(vector<int>arr, int n, int k) {
sort(arr.begin(), arr.end());
int i = 0, j = n - 1;
int moves = 0;
while (i <= j) {
if (arr[i] + arr[j] <= k) {
i++;
}
j--;
moves++;
}
return moves;
}
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 | } |