출처: https://train.usaco.org/usacogate


문제 설명

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.

입력 양식

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

출력 양식

A single line with two integers that represent the longest continuous time of milking and the longest idle time.

입력 예

3 
300 1000 
700 1200 
1500 2100

출력 예

900 300


문제 풀이 내용

농부들이 작업하는 시간들을 (시작시간, 끝나는 시간) 쌍으로 만들고, 시작 시간을 기준으로 정렬한다. 앞에서부터 탐색해서 다음 시작하는 시간과 끝나는 시간을 비교해서 계속 겹쳐간다. 겹쳐지지 않는 시간들 중에서 가장 긴시간과 겹쳐진 시간들에서 제일 긴시간을 찾아서 출력한다.

프로그램 내용

더보기
    vector< pair<int, int> > farmers;

    for(int idx=0; idx < n_Farmer; ++idx)
    {
        int start_time =0, end_time =0;
        fin >> start_time >> end_time ;
        farmers.push_back(make_pair(start_time, end_time));
    }

    sort(farmers.begin(),farmers.end(),compare);

    int work_time =0, work_gap=0;

    int t_start=farmers[0].first, t_end=farmers[0].second;

    for(int idx=0; idx < n_Farmer; ++idx)
    {
        int t_work=0, t_gap=0;

        if( farmers[idx].first <= t_end )
        {
            if(farmers[idx].second > t_end)
            {
                t_end = farmers[idx].second;
            }
            t_work = t_end - t_start;
        }

        else if ( farmers[idx].first > t_end )
        {
            t_gap = farmers[idx].first - t_end;
            t_work = t_end - t_start;
            t_start = farmers[idx].first;
            t_end = farmers[idx].second;
        }

        if (work_time < t_work) work_time = t_work;
        if (work_gap < t_gap) work_gap = t_gap;
    }

 

Chapter 1. Getting started

 

'USACO Training' 카테고리의 다른 글

Problem 1.3.4 Name That Numbers  (0) 2019.09.07
Problem 1.3.3 Transformations  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.6 Friday the Thirteenth  (0) 2019.09.07
Problem 1.2.5 Greedy Gift Givers  (0) 2019.09.07

출처: https://train.usaco.org/usacogate


문제 설명

Broken Necklace
You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29:

                1 2                               1 2
            r b b r                           b r r b
          r         b                       b         b
         r           r                     b           r
        r             r                   w             r
       b               r                 w               w
      b                 b               r                 r
      b                 b               b                 b
      b                 b               r                 b
       r               r                 b               r
        b             r                   r             r
         b           r                     r           r
           r       r                         r       b
             r b r                             r r w
            Figure A                         Figure B
                        r red bead
                        b blue bead
                        w white bead
The beads considered first and second in the text that follows have been marked in the picture.

The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .

Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).

Determine the point where the necklace should be broken so that the most number of beads can be collected.

Example
For example, for the necklace in Figure A, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or else between bead 24 and bead 25.

In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration can include any of the three symbols r, b and w.

Write a program to determine the largest number of beads that can be collected from a supplied necklace.

입력 양식

Line 1: N, the number of beads
Line 2: a string of N characters, each of which is r, b, or w

출력 양식

A single line containing the maximum of number of beads that can be collected from the supplied necklace.

입력 예

29 
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

출력 예

11


문제 풀이 내용

목걸이는 원형으로 연결되어 있으므로, 시작과 끝부분을 서로 연결해서 2번의 배열 안에서만 세면 가능하다. 흰색은 어떤 색이든 연결이 되고, 빨간색과 파란색이 서로 달라지는데, 두번 바뀌기 전까지 배열을 세는 방법이다.

프로그램 내용

더보기
#define MAX_BEAD 350

    int Length = 0;
    string Beads;

        Length = Beads.length();

    int count = 0;
    int MAX = 0;
    char color;
    int flag=0;

    string ExtBeads = Beads + Beads;

    for(int idx=0; idx < Length; ++idx)
    {
        char current = ExtBeads[idx];

        if(current == 'w')
            flag = 0;
        else
            flag = 1;

        count =0;

        int idx2 = idx;
        while( flag <=2 )
        {
            while (idx2 < (Length + idx) && ( ExtBeads[idx2] == current || ExtBeads[idx2] == 'w'))
            {
                count++;
                idx2++;
            }
            flag++;
            current = ExtBeads[idx2];
        }

        if ( count > MAX ) MAX = count;
    }

 

Chapter 1. Getting started

'USACO Training' 카테고리의 다른 글

Problem 1.3.3 Transformations  (0) 2019.09.07
Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.6 Friday the Thirteenth  (0) 2019.09.07
Problem 1.2.5 Greedy Gift Givers  (0) 2019.09.07
Problem 1.2.2 Ride  (0) 2019.09.07

출처: https://train.usaco.org/usacogate


문제 설명

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.

Note that the start year is NINETEEN HUNDRED, not NINETEEN NINETY.

There are few facts you need to know before you can solve this problem:

January 1, 1900 was on a Monday.
Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

입력 양식

One line with the integer N.

출력 양식

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

입력 예

20

출력 예

36 33 34 33 35 35 34


문제 풀이 내용

매달 13일이 요일마다 몇번씩 있었는지 세는 문제이다. 1900. 1. 1 이 월요일에서 시작하기 때문에 첫번째 13일이 토요일이 되는 것을 기준으로 각 달이 며칠인지 더해서 계산했다.

프로그램 내용

더보기
#define BEGIN_YEAR 1900
#define FIRST_DAY 2 // Jan. 1. 1990 - Monday
/// Sat=0, Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6
#define DAYS_IN_WEEK 7
#define MONTH_IN_YEAR 12
#define MAX_IN_YEAR 400

/*
Leap Year : year % 4 == 0 && year % 100 != 100 && year %400 == 0
*/
bool LeapYear (int inYear)
{
    bool LeapFlag = false;

    if (inYear % 4 == 0) { LeapFlag = true; }
    if (inYear % 100 == 0) { LeapFlag = false; }
    if (inYear % 400 == 0) { LeapFlag = true; }

    return LeapFlag;
};

int Days = 13 + FIRST_DAY - 1; /// 15 Mon - Sat

    for (int index = 0; index < InYear ; ++index)
    {
        int year = BEGIN_YEAR + index;
        if(!LeapYear (year))
        {
            for (int idx =0; idx< MONTH_IN_YEAR ; ++idx)
            {
                ++Thirteen[Days%7];
                Days += DayInMonth[idx];
            }
        }
        else
        {
            for (int idx =0; idx< MONTH_IN_YEAR ; ++idx)
            {
                ++Thirteen[Days%7];
                Days += LeapDayInMonth[idx];
            }
        }
    }

    for (int index=0; index<DAYS_IN_WEEK ; ++index)
    {
        fout << Thirteen[index];
        if(index < 6)
            fout << " ";
    }

 

Chapter 1. Getting started

'USACO Training' 카테고리의 다른 글

Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.5 Greedy Gift Givers  (0) 2019.09.07
Problem 1.2.2 Ride  (0) 2019.09.07
USACO Training - Chapter 1. Getting started  (0) 2019.09.06

+ Recent posts