0

I was struggle with an interval schedule question, the question description as follow:

Description: Lanran has N friends. Every Sunday, Lanran has to play with his friends. The i-th friend can play with Lanran from time a to time b (a and b are included). However, Lanran has to play with each of his friends for the same amount of time. Lanran wants to play with his friends as long as possible. But he is very stupid. So he asks for your help to calculate the maximum time he can play with each of his friends.

Input The first line contains one integer N. Each of the next N (N <= 5000) lines contains two integers a and b (1 <= a, b <= 10000), which show the time interval of the i-th friend.

Output Output a single integer, shows the maximum time Lanran can play with each of his friends.

I think this a greedy problem, and I choose the minimal time friend, which is the already played time + possible playing time till b of friend, and play with him at i-th second. Here's the code:

#include <bits/stdc++.h>

using namespace std;
const int N = 5010;
int n, s[N], e[N], cnt[N], me;

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int partition(int low, int high) {
    int pivot = s[high];
    int i = (low - 1);

    for (int j = low; j <= high - 1; j++) {
        if (s[j] <= pivot) {
            i++;
            swap(&s[i], &s[j]);
            swap(&e[i], &e[j]);
        }
    }
    swap(&s[i + 1], &s[high]);
    swap(&e[i + 1], &e[high]);
    return (i + 1);
}

void quickSort(int low, int high) {
    if (low < high) {
        int pi = partition(low, high);
        quickSort(low, pi - 1);
        quickSort(pi + 1, high);
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &s[i], &e[i]);
        if (e[i] < s[i]) { printf("0\n"); return 0; }
        if (e[i] > me) me = e[i];
    }
    quickSort(0, n - 1);
    for (int i = 1; i <= me; ++i) {
        int id = -1, mi = 0x7fffffff;
        for (int j = 0; j < n; ++j) {
            if (s[j] > i || i > e[j]) continue;
            if (cnt[j] + e[j] - i + 1 < mi) { id = j; mi = cnt[j] + e[j] - i + 1; }
        }
        ++cnt[id];
    }
    int ans = 0x7fffffff;
    for (int i = 0; i < n; ++i) if (cnt[i] < ans) ans = cnt[i];
    printf("%d\n", ans);
    return 0;
}

So does I make something wrong? I got 7 wrong answer in 10 test cases.

  • There is no need to write your own swap function when there is already [std::swap](https://en.cppreference.com/w/cpp/algorithm/swap). Also, why are you not using [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort)? – PaulMcKenzie Mar 21 '19 at 06:25
  • Also please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Mar 21 '19 at 06:27

1 Answers1

0

Looks like it is same as standard activity selection problem. I am pasting related standard algorithm. you can find wiki : https://en.wikipedia.org/wiki/Activity_selection_problem. Greedy-Iterative-Activity-Selector(A, s, f):

Sort A by finish times stored in f

S = {A[1]} 
k = 1

n = A.length

for i = 2 to n:
   if s[i] ≥ f[k]: 
       S = S U {A[i]}
       k = i

return S
Ram
  • 73
  • 1
  • 7