This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
struct guest
{
int start;
int end;
};
struct breakLength
{
int start;
int length;
};
int binarySearchGuests(const std::vector<guest>& guests, int targetStart) {
int low = 0;
int high = guests.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (guests[mid].start == targetStart) {
return mid; // Target found, return its index
} else if (guests[mid].start < targetStart) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Target not found
}
int main()
{
int n; // Number of guests
int k; // Number of matches
cin >> n >> k;
vector<guest> guests(n);
vector<breakLength> breaks(n-1);
for (int i = 0; i < n; i++)
{
int temp = 0;
cin >> temp;
guests[i].start = temp;
guests[i].end = temp+1;
if (i > 0)
{
breaks[i-1].start = guests[i].start;
breaks[i-1].length = guests[i].start - guests[i-1].end; //Length of break between guests
}
}
//Combine guests until there are k guests
//Order breaks by length
sort(breaks.begin(), breaks.end(), [](breakLength a, breakLength b) { return a.length < b.length; });
int i = 0;
while(n - i > k)
{
int j = binarySearchGuests(guests, breaks[i].start);
int x = 1;
while(guests[j-x].end == -1)
{
x++;
}
guests[j-x].end = guests[j].end;
guests.erase(guests.begin() + j);
i++;
}
int stove = 0;
n = guests.size();
for(int i = 0; i < n; i++)
{
stove += guests[i].end - guests[i].start;
}
cout << stove << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |