Usaco Milking Cows report

Source: Internet
Author: User

Question:
Three farmers get up at every morning and then go to the cowshed to milk three cows. The first farmer milked his ox in 300 seconds (starting from) until 1000 seconds. The second farmer started in 700 seconds and ended in 1200 seconds. The third farmer ended in 1500 seconds. During the period, at least one farmer has been milking for 900 seconds (from 300 seconds to 1200 seconds), and the longest continuous time (from milking to milking) it is 300 seconds (from 1200 seconds to 1500 seconds ).
Your task is to compile a program, read a list of N farmers (1 <= N <= 5000) Working hours of ntou cattle, and calculate the following two points (both in seconds):
At least one person is in the milking period.
The longest period of time for unattended milking. (Starting from someone milking)

Question: Set an array to indicate the current time point. The initialization value is 0. Each time you read the start end point, add one at the start point, subtract one from the end point, and then traverse from the start, use sum to add the value of each time point. When sum is 0, it indicates no milking. If sum is greater than 0, it indicates that the milking is performed.
Code:
[Cpp]
/*
ID: lishicao
PROG: milk2
LANG: C ++
*/
# Include <iostream>
# Include <fstream>
# Include <cstring>
Using namespace std;
 
Int vis [1000500];
 
Ifstream fin ("milk2.in ");
Ofstream fout ("milk2.out ");
 
Int main ()
{
Int N;
Int start, end, Max = 0, Min = 99999999, milked = 0, notmilked = 0;
Memset (vis, 0, sizeof (vis ));
 
Fin> N;
While (N --)
{
Fin> start> end;
If (end> Max) Max = end;
If (start <Min) Min = start;
Vis [start] ++;
Vis [end] --;
}
 
Int sum = 0;
Int Count = 0;
Int flag = 0;
 
For (int I = Min; I <= Max; I ++)
{
Sum + = vis [I];
If (flag = 0 ){
If (sum = 0 ){
Flag = 1;
If (milked <Count) milked = Count;
Count = 0;
}
Count ++;
} Www.2cto.com
Else {
If (sum> 0 ){
Flag = 0;
If (notmilked <Count) notmilked = Count;
Count = 0;
}
Count ++;
}
}
Fout <milked <"" <notmilked <endl;
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.