Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).
Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):
The longest time interval at least one cow was milked.
The longest time interval (after milking starts) during which no cows were being milked.
My solution:
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef struct time{
int start;
int end;
}time;
void print(time *p, int n)
{
for(int i = 0 ; i < n ; i++, p++)
printf("%d %d\n", p->start, p->end);
}
void read(FILE *fin, time *p, int n)
{
for(int i = 0 ; i < n ; i++, p++)
fscanf(fin, "%d %d", &p->start, &p->end);
}
void sort(time *p, int n)
{
int min;
time temp;
for(int i = 0 ; i <= n - 2 ; i++) {
min = (p+i)->start;
for(int j = i + 1 ; j <= n - 1 ; j ++) {
if( (p + j)->start < min )
{
min = (p + j)->start;
temp = *(p + j);
*(p + j) = *(p + i);
*(p + i) = temp;
}
}
}
}
int milked(time *p, int n)
{
int max = 0, start, end;
int i = 0, j, temp;
while(i < n)
{
start = (p + i)->start;
end = (p + i)->end;
for(j = 0 ; j < n ; j++)
{
if((p + j)->start <= end && (p + j)->end > end){
end = (p + j)->end;
i++;
}
}
temp = end - start;
if(temp > max)
max = temp;
i++;
}
return max;
}
int not_milked(time *p, int n)
{
int max = 0, start, end;
int i = 0, j, temp = 0;
while(i < n){
start = (p + i)->start;
end = (p + i)->end;
for(j = i + 1 ; j < n ; j++){
if((p + j)->start <= end && (p + j)->end > end)
end = (p + j)->end;
else if((p + j)->start > end){
temp = (p + j)->start - end;
break;
}
}
if(max < temp)
max = temp;
i = j;
}
return max;
}
int main()
{
int n, i;
time *p;
FILE *fin = fopen("milk2.in", "r");
FILE *fout = fopen("milk2.out", "w");
fscanf(fin, "%d", &n);
printf("%d\n", n);
p = (time *)malloc( n*sizeof(time) );
assert(p!=NULL);
read(fin, p, n);
//print(p, n);
printf("\n\n");
sort(p, n);
//print(p, n);
printf("\n\n");
printf("\n%d\n%d\n", milked(p, n), not_milked(p, n));
fprintf(fout, "%d %d\n", milked(p, n), not_milked(p, n));
return 0;
}
And, the answer provided by USACO :
We read the list of times, sort it by start time, and then walk over the list once, merging overlapping times. Then we walk the list watching for long milking periods and long non-milking periods.
An alternate approach would be to just keep an array of size a million and mark off times. On a nice fast processor, that’s probably fast enough, but our above algorithm will work even on slow processors, and it’s not much harder to write.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAXMILKING 5000
typedef struct Milking Milking;
struct Milking {
int begin;
int end;
};
Milking milking[MAXMILKING];
int nmilking;
int
milkcmp(const void *va, const void *vb)
{
Milking *a, *b;
a = (Milking*)va;
b = (Milking*)vb;
if(a->begin > b->begin)
return 1;
if(a->begin < b->begin)
return -1;
return 0;
}
void
main(void)
{
FILE *fin, *fout;
int i, j, t, tmilk, tnomilk;
Milking cur;
fin = fopen("milk2.in", "r");
fout = fopen("milk2.out", "w");
assert(fin != NULL && fout != NULL);
/* read input, sort */
fscanf(fin, "%d", &nmilking);
for(i=0; i<nmilking; i++)
fscanf(fin, "%d %d", &milking[i].begin, &milking[i].end);
qsort(milking, nmilking, sizeof(Milking), milkcmp);
/* walk over list, looking for long periods of time */
/* tmilk = longest milking time */
/* tnomilk = longest non-milking time */
/* cur = current span of milking time being considered */
tmilk = 0;
tnomilk = 0;
cur = milking[0];
for(i=1; i<nmilking; i++) {
if(milking[i].begin > cur.end) { /* a down time */
t = milking[i].begin - cur.end;
if(t > tnomilk)
tnomilk = t;
t = cur.end - cur.begin;
if(t > tmilk)
tmilk = t;
cur = milking[i];
} else {
if(milking[i].end > cur.end)
cur.end = milking[i].end;
}
}
/* check final milking period */
t = cur.end - cur.begin;
if(t > tmilk)
tmilk = t;
fprintf(fout, "%d %d\n", tmilk, tnomilk);
exit(0);
}