[Topcoder] srm157 div2 Summary

Source: Internet
Author: User

250 points:A simple binary game is usually used to guess digital games.

Code: GitHub

500 points:Give an employee a punching time period for one day and ask for the employee's salary for the day. The normal start time is from 6:00:00 to 18:00:00, the salary is wage, and the other time salary is 1.5 * wage.

My thinking is straightforward. I divide the time into three time periods for calculation: 00: 00 ~ ~ ~ 23:59:59. The pseudo code is as follows:

 1 if(arrival time < 06:00:00) 2        money += 1.5*wage*(min(06:00:00,departure time)-arrival time); 3        if(departure time > 06:00:00) 4          arrival time = 06:00:00; 5  6 if(arrival time >= 06:00:00 && arrival time <18:00:00) 7         money += wage*(min(departure time,18:00:00) - 06:00:00); 8         if(departure time > 18:00:00) 9             arrival time = 18:00:00;10 11 if(arrival time >= 18:00:00)12         money += 1.5*wage*(departure time - arrival time);

In actual implementation, I directly used the Java date class, which is very troublesome when calculating the time difference. In fact, we only need to convert all the time into seconds, then, calculate the amount of money a person earns per second, so that it is easy to compare the time size or calculate the difference between the two time points. Therefore, if you need to compare the time and difference in the future, try to convert it into seconds for calculation, which saves a lot of trouble. However, after half a day today, I have accumulated some knowledge about the Java date class. The accumulation is as follows:

1. Convert the string like "HH: mm: SS" to the date type:

SimpleDateFormat fommater = new SimpleDateFormat("HH:mm:ss");Date arriDate = fommater.parse(arrival);

The preceding parse function may throw a parseexception and must be processed.

2. Determine whether the time t1 is before or after the time t2:

t1.before(t2);t1.after(t2);

3. Calculate the difference between time t1 and T2. the unit is milliseconds)

long bewteen = t1.getTime() - t2.getTime();

Complete code: GitHub

The official solution for this question is also very simple: After the arrival time and departure time are changed to seconds, the arrival time is traversed to departure time in one second, if the current second is within the normal working range, increase the counter T1; if it is within the overtime range, increase the counter T2, the final result is wage * t1 + 1.5 * wage * t2 (wage is the money earned per second ). This method is much easier to implement than the preceding three-step judgment.

1000 points:Two hourglass values are provided. One can be used to measure the time glass1, and the other can be used to measure the time of glass2. The first ten smallest values are required to measure the time.

At first glance, there was no idea. After reading the answer, we found that we should use the recursive + memorandum method: the recursive function helper is defined as void helper (INT sand1, int sand2, int time) to indicate the time at the moment, the time sand1 and sand2 can be measured by the two hourglass, respectively. At this time, there are two possibilities:

1. If sand1 or sand2 is at least empty, we have four options:

(1) Put sand1 upside down, sand2 does not move, recursive call helper (glass1-sand1, sand2, time );

(2) sand2 inverted, sand1 does not move, recursive call helper (sand1, glass2-sand2, time );

(3) The sand1 and sand2 all inverted, recursive call helper (glass1-sand1, glass2-sand2, time );

(4) sand1 and sand2 do not move. Wait for the blank ones to be empty. You need to check which of the two hourglass has not been missed yet. If 1 has not been missed, call helper (0, 0, time + sand1 );

2. sand1 and sand2 are not empty, so we have to wait for them to have a smaller Sandglass leakage to complete the action, then recursively call helper (sand1-min (sand1, sand2 ), sand2-min (sand1, sand2), time + min (sand1, sand2 ).

If recursion is simple, it will time out, because for the same group (sand1, sand2, time), we may repeat the call multiple times in the recursion process, therefore, use the memorandum visited [] [] [] to record the sand1, sand2, and time that we have used to avoid repeated calls.

Final code: GitHub

Complete official question

[Topcoder] srm157 div2 Summary

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.