2. Task Scheduling
Implement a prototype service for resource estimation.
Given a set of n tasks, the ith (0 ≤ i < n) task runs from time start[i] through end[i].
Implement a task scheduler method that finds the minimum number of machines required to complete the tasks. A task can be scheduled on exactly one machine, and one machine can run only one task at a time.
Example
Suppose n = 5, start = [1, 8, 3, 9, 6], end = [7, 9, 6, 14,7].
Consider the following task schedule. Times in parentheses are the inclusive start and end times for each job.
• Machine 1: [(1, 7), (8, 9)]
• Machine 2: [(3, 6), (9, 14)]
• Machine 3: [(6, 7)]
Here, the number of machines required is 3.
Function Description:
Complete the function getMinMachines in the editor below.
getMinMachines has the following parameters:
int start[n]: the start times of tasks int end[n]: the end times of tasks
Returns:
int: the minimum number of machines required to run all the tasks
Constraints:
• 15n≤2*10^5
• 1 ≤ start[if ≤ end[i] ≤ 10^9
Input Format for Custom Testing:
The first line contains an integer n, the number of tasks.
Each of the next n lines contains an integer start[i].
The next line contains the same integer n, the number of tasks.
Each of the next n lines contains an integer end[i].
• Sample Case 0:
Sample Input 0:
STDIN FUNCTION
------ -----------
5 → start[] size n = 5
2 → start[] = [2, 1, 5, 5, 8]
1
5
5
8
5 → end[] size n = 5
5 → end[] = [5, 3, 8, 6, 12]
3
8
6
12
Sample Output 0:
3
Explanation:
• Machine 1: [(1,3), (5,8)]
• Machine 2: [(2,5), (8, 12)]
• Machine 3: [(5,6)]
• Sample Case 1:
Sample Input 1:
STDIN FUNCTION
---- -----------
4 → start[] size n = 4
2 → start[] = [2, 2, 2, 2]
2
2
2
4 → end[] size n = 4
5 → end[] = [5, 5, 5, 5]
5
5
5
Sample Output 1:
4
Explanation:
• Machine 1: [(2, 5)]
• Machine 2: [(2, 5)]
• Machine 3: [(2, 5)]
• Machine 4: [(2, 5)]
I am now in 1032 rating. But nowadays it is hard for me to get ratings because of huge amounts of submissions. What should I do to perform better. My only target is to become specialist
Читать полностью…I have codeforces accounts for sale, expert, candidate master, master, grandmaster, dm me, payment via crypto
Читать полностью…import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
// Function to calculate the sum of an arithmetic sequence
static long findSum(long n, long a, long d) {
long result = n * (2 * a + (n - 1) * d);
result = result / 2;
return result;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] data = br.readLine().split(" ");
int t = Integer.parseInt(data[0]); // Number of test cases
int index = 1;
ArrayList<String> results = new ArrayList<>();
for (int i = 0; i < t; i++) {
long n = Long.parseLong(data[index]); // Length of sequence
long k = Long.parseLong(data[index + 1]); // Start value
index += 2;
long low = k;
long high = k + n - 1;
long left = k;
long right = k + n - 1;
long position = 0;
int count = 0;
while (left <= right) {
count++;
long mid = (left + right) / 2;
long posSum = findSum(mid - low + 1, low, 1);
long negSum = findSum(high - mid, mid + 1, 1);
if (posSum <= negSum) {
position = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
long minDiff = Long.MAX_VALUE;
long result = position;
for (long j = position - 10; j <= position + 10; j++) {
if (j < low || j > high) continue;
long posSum = findSum(j - low + 1, low, 1);
long negSum = findSum(high - j, j + 1, 1);
long currentDiff = Math.abs(posSum - negSum);
if (currentDiff < minDiff) {
minDiff = currentDiff;
result = j;
}
}
results.add(String.valueOf(minDiff));
}
// Output results
for (String res : results) {
System.out.println(res);
}
}
}
I have codeforces accounts for sale, expert, candidate master, master, grandmaster, dm me, payment via crypto
Читать полностью…I have codeforces accounts for sale, expert, candidate master, master, grandmaster, dm me, payment via crypto
Читать полностью…#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int m = n;
map<int, int> freq;
int maxi = 0, ans = 0, counter = 0;
while (n--) {
int x, y;
cin >> x >> y;
freq[y]++;
counter++;
int adjustment = freq[y] - 2; // Equivalent to (mp[y] - 1 - 1)
ans -= (adjustment) * (adjustment + 1) / 2;
int current_count = freq[y] - 1;
ans += (current_count) * (current_count + 1) / 2;
maxi = max(maxi, freq[y]);
int solution = ans;
int adjustment_maxi = maxi - 1;
solution -= (adjustment_maxi) * (adjustment_maxi + 1) / 2;
int adjustment_full = maxi + (m - counter) - 1;
solution += (adjustment_full) * (adjustment_full + 1) / 2;
cout << solution << " ";
}
cout << endl;
}
return 0;
}
#include <bits/stdc++.h>
#define ANTIHACK(x,k) ((x-k) + ((n^q^l*r)==855401101))
using namespace std;
int p = 1000000007;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long ttt;
cin >> ttt;
nexttc:
while (ttt--) {
long long n, q;
cin >> n >> q;
vector<long long> a(2*n);
for (long long i=0; i<n; i++) {
cin >> a[i];
a[i+n] = a[i];
}
vector<long long> pref(2*n + 1, 0);
for (int i=1; i<=2*n; i++) {
pref[i] = a[i-1] + pref[i-1];
}
while (q--) {
long long l, r;
cin >> l >> r;
l--; r--;
long long ans = p + pref[n] * (r/n - l/n - 1);
ans += pref[l/n + n] - pref[l/n + l%n];
ans += pref[r/n + r%n + 1] - pref[r/n];
cout << ANTIHACK(ans, p) << endl;
}
}
return 0;
}