1411021337-ny-African children

Source: Internet
Author: User
Tags strcmp
Zookeeper African children time limit: 1000 MS | memory limit: 65535 kb difficulty: 2
Description
All the children who live in Africa are black. Why?
First, they are located in the tropical area with severe solar radiation.
Second, they do not often take a bath. (How to take a bath if you are short of water all the year round .)
Now, in an African tribe, they only have one place to take a bath, and the bath time is very short, instantly there is wood !! (This is also no way, lack of water !!)
Each child can take a bath for a period of time. Besides, they can be washed together (whether you are a boy or a girl ).
So who should take a bath at what time? It must be their great "bath. "Bath" has a schedule to record the children in the tribe when they can take a bath. Now, the "bath" God asked you, How many times does one day need him to turn on and off at least the faucet that takes a bath? Because it is very effort-consuming to enable and disable a single tap, even if it is completed instantly.
Input
Multiple groups of data
The first line is 0 <n <= 100.
Next n rows, each row has a time period. H1h1: M1M1-H2H2: m2m2, in 24-hour format.
Make sure that the time period is within one day. However, there is no guarantee that h1h1: m1m1 is prior to h2h2: m2m2.
Output
How many times does the "bath" God need to turn on and off the faucet at least?
Sample Input
100:12-12:12200:12-12:1214:00-12:00
Sample output
11
Prompt
PS: Once enabled or disabled
Solutions
The greedy knowledge is used to sort by End Time in ascending order.
Error cause
In this question, I first understood the question as a problem of at least continuous intervals due to a read error, after going to WA for multiple times, I read the questions carefully and found out that it was the interval selection problem in the greedy algorithm.
In this question,
No matter the case ①:
-
-
Case 2:
-
-
It should only be one time, because there are cross time points in both cases, and the two children can finish washing instantly at this time point, so they only need to open the tap once.
In my opinion, the question is not completely clear. It should be that the TAP is opened for the least number of times, that is, all the persons that can be washed together.
For example, if the data you provided is in fact, you only need to open the faucet once between and, the two children can finish the washing at the same time, and then open it again at, and the other one is done after the washing (all instantly ...). Instead of waiting for a person to wash them all the time, it would not be enough to close every night.
Code
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; struct time {char sta [5], end [5];} Times [110]; bool CMP (time g, time h) {return strcmp (G. end, H. end) <0;} Char E [5], F [5], K [5]; int main () {int N; int A, B, C, D; int I, j; int sum; while (scanf ("% d", & N )! = EOF) {for (I = 0; I <n; I ++) {scanf ("% d % * C % d", & A, & B, & C, & D ); E [0] = A/10 + '0'; E [1] = A % 10 + '0'; E [2] = B/10 + '0 '; E [3] = B % 10 + '0'; F [0] = C/10 + '0'; F [1] = C % 10 + '0 '; f [2] = D/10 + '0'; F [3] = D % 10 + '0'; If (strcmp (e, f) <0) {strcpy (Times [I]. STA, e); strcpy (Times [I]. end, f);} else {strcpy (Times [I]. STA, f); strcpy (Times [I]. end, e) ;}/// int type storage can be directly used here/* scanf ("% d % C % d", & B1, & C, & B2, & C, & E1, & C, & E2); t [I]. B = b1 * 100 + B2; t [I]. E = e1 * 100 + E2; // here, the child's start and end time are converted from two variables: hour and minute into a convenient operation if (T [I]. b> T [I]. e) Swap (T [I]. b, T [I]. e); */sort (times, times + N, CMP); sum = 1; strcpy (K, times [0]. end); for (I = 1; I <n; I ++) {If (strcmp (Times [I]. STA, k)> 0) {sum ++; strcpy (K, times [I]. end); // select the least public vertex among multiple intervals} printf ("% d \ n", sum);} return 0 ;}


1411021337-ny-African children

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.