ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Общий форум

Milking Cows problem, why wrong?
Послано Junhai Feng 23 сен 2016 06:51
Milking Cows


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.

PROGRAM NAME: milk2

INPUT FORMAT

Line 1: The single integer
Lines 2..N+1: Two non-negative integers less than 1000000, the starting and ending time in seconds after 0500

SAMPLE INPUT (file milk2.in)
3
300 1000
700 1200
1500 2100



OUTPUT FORMAT
A single line with two integers that represent the longest continuous time of milking and the longest idle time.
SAMPLE OUTPUT (file milk2.out)
900 300
=======following is C code==================
#include <stdio.h>
void doSort(long nums[5000][2],int n);

int main(int argc){
    long nums[5000][2];
    long max_line[2],cur_line[2],max_gap;
    int n,i,j;

    scanf("%d",&n);
    for (i=0;i<n;i++){
        scanf("%d",&nums[i][0]);
        scanf("%d",&nums[i][1]);
    }

    doSort(nums,n);
    max_line[0] = nums[0][0];
    max_line[1] = nums[0][1];

    cur_line[0] = nums[0][0];
    cur_line[1] = nums[0][1];
    //printf("%d,%d\n",nums[0][0],nums[0][1]);
    for (j=1;j<n;j++){

        // case of line
        if (nums[j][0] <= cur_line[1] ){
            if (nums[j][1]>cur_line[1]){
                // line be extended
                cur_line[1] = nums[j][1];
            }
            // printf("l:%d,%d\n",nums[j][0],nums[j][1]);
        }
        else{ // case of gap
            // get gap value
            if ((nums[j][0] - cur_line[1])> max_gap){
                max_gap = nums[j][0] - cur_line[1];
            }

            // save current line into max line if cur > max
            if ((cur_line[1] - cur_line[0] )> (max_line[1] - max_line[0]))
            {
                max_line[0] = cur_line[0];
                max_line[1] = cur_line[1];
            }
            cur_line[0] = nums[j][0];
            cur_line[1] = nums[j][1];
            // printf("g:%d,%d\n",nums[j][0],nums[j][1]);
        }
    }
    // save current line into max line if cur > max
    if ((cur_line[1] - cur_line[0] )> (max_line[1] - max_line[0]))
    {
        max_line[0] = cur_line[0];
        max_line[1] = cur_line[1];
    }


    printf("%d ",max_line[1]-max_line[0]);
    printf("%d",max_gap);
    return 0;
}

void doSort(long nums[5000][2],int n){
    int j,p;
    int temp[2];
    for (p=1;p<n;p++){
        temp[0] = nums[p][0];
        temp[1] = nums[p][1];
        for(j=p;j>0 && nums[j-1][0] > temp[0];j--){
            nums[j][0]=nums[j-1][0];
            nums[j][1]=nums[j-1][1];
        }
        nums[j][0]=temp[0];
        nums[j][1]=temp[1];
    }
}
Re: Milking Cows problem, why wrong?
Послано Junhai Feng 23 сен 2016 07:00
tried test case:
6
3000 4000
3000 3500
3500 3600
3000 4500
5000 10000
1000 2000

out put : 5000 1000
I think this case is sucessful, but Judge site gave me the feedback:Wrong Answer!
I do not know which test case was not passed..<Judge site does not feedback more detail info>