0% found this document useful (0 votes)
4 views4 pages

Cpp Simple Solutions

The document presents solutions to three competitive programming problems using C++. The first problem involves calculating the minimum total time for server upgrades while managing request interruptions, using binary search and the inclusion-exclusion principle. The second problem focuses on finding the k-th greatest element in prefixes of an array using a min-heap, and the third problem addresses task scheduling with memory constraints, utilizing a two-pointer technique after grouping tasks by type.

Uploaded by

nainsiverma2528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Cpp Simple Solutions

The document presents solutions to three competitive programming problems using C++. The first problem involves calculating the minimum total time for server upgrades while managing request interruptions, using binary search and the inclusion-exclusion principle. The second problem focuses on finding the k-th greatest element in prefixes of an array using a min-heap, and the third problem addresses task scheduling with memory constraints, utilizing a two-pointer technique after grouping tasks by type.

Uploaded by

nainsiverma2528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Competitive Programming Problems -

C++ Solutions
1. Server Upgrade Planning
Problem: Two servers require upgrades taking t1 and t2 seconds. Upgrades pause during
seconds when requests arrive (multiples of req1 for server 1, req2 for server 2). Only one
server can be upgraded at a time. Find the minimum total time to complete upgrades.

Approach: Use binary search over total time and inclusion-exclusion principle to calculate
blocked seconds.

C++ Code:

#include <iostream>
#include <algorithm>
using namespace std;

long long gcd(long long a, long long b) {


while (b) {
long long temp = b;
b = a % b;
a = temp;
}
return a;
}

long long getMinUpgradationTime(long long req1, long long t1, long long req2, long long t2)
{
long long lo = 1, hi = 1000000000000000LL, ans = hi;
long long lcm = (req1 / gcd(req1, req2)) * req2;

while (lo <= hi) {


long long mid = lo + (hi - lo) / 2;
long long blockedFor1 = mid / req1;
long long blockedFor2 = mid / req2;
long long blockedForBoth = mid / lcm;

long long alone1 = blockedFor2 - blockedForBoth;


long long alone2 = blockedFor1 - blockedForBoth;
long long free_slots = mid - (blockedFor1 + blockedFor2 - blockedForBoth);

long long rem_t1 = max(0LL, t1 - alone1);


long long rem_t2 = max(0LL, t2 - alone2);

if (rem_t1 + rem_t2 <= free_slots) {


ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}

int main() {
long long req1, t1, req2, t2;
cin >> req1 >> t1 >> req2 >> t2;
cout << getMinUpgradationTime(req1, t1, req2, t2) << "\n";
return 0;
}

2. Finding K-th Greatest Element in Prefixes


Problem: For every prefix of the array of length i (i from k to n), find the k-th greatest
element.

Approach: Use a min-heap of size k to track the k greatest elements dynamically.

C++ Code:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

vector<int> getGreatestElements(const vector<int>& arr, int k) {


priority_queue<int, vector<int>, greater<int>> minHeap;
vector<int> result;
for (int i = 0; i < arr.size(); ++i) {
if (minHeap.size() < k) {
minHeap.push(arr[i]);
} else if (arr[i] > minHeap.top()) {
minHeap.pop();
minHeap.push(arr[i]);
}
if (i >= k - 1) {
result.push_back(minHeap.top());
}
}
return result;
}

int main() {
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
vector<int> res = getGreatestElements(arr, k);
for (int val : res) cout << val << " ";
cout << endl;
return 0;
}

3. Task Scheduling
Problem: Given n tasks each with memory and type, at most two tasks can run in parallel if
they share the same type and combined memory ≤ max_memory.
Each task takes 1 unit time. Find minimal total processing time.

Approach: Group tasks by type, sort them, then pair using two-pointer technique.

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int getMinTime(const vector<int>& task_memory, const vector<int>& task_type, int


max_memory) {
map<int, vector<int>> tasks_by_type;
for (int i = 0; i < task_memory.size(); ++i) {
tasks_by_type[task_type[i]].push_back(task_memory[i]);
}

int total_time = 0;
for (auto& [type, tasks] : tasks_by_type) {
sort(tasks.begin(), tasks.end());
int left = 0, right = tasks.size() - 1;
while (left <= right) {
if (left != right && tasks[left] + tasks[right] <= max_memory) {
left++;
right--;
} else {
right--;
}
total_time++;
}
}
return total_time;
}

int main() {
int n, max_memory;
cin >> n >> max_memory;
vector<int> task_memory(n), task_type(n);
for (int i = 0; i < n; ++i) cin >> task_memory[i];
for (int i = 0; i < n; ++i) cin >> task_type[i];

cout << getMinTime(task_memory, task_type, max_memory) << "\n";


return 0;
}

You might also like