CERC 2004/uva 1335 Beijing Guards: Second & greedy & idea questions

Source: Internet
Author: User
Tags range time limit

1335-beijing Guards

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_ problem&problem=4081

Beijing is once surrounded by four rings city walls:the Forbidden City Wall, the Imperial City Wall, the Inner city W All, and finally the Outer City Wall. Most of the walls were demolished in the 50s and 60s to make way for roads. The walls were protected by guard towers, and there is a guard living in each tower. The wall can is considered to being a large ring, where every guard tower and has exaetly two.

The guard had to keep a eye on his and the Wall all day, and so him had to stay in the tower. This is a very boring job thus it are important to keep the guards motivated. The best way to motivate a guard are to give him lots of awards. There are several different types of awards that can be given:the Distinguished Service Award, the nicest uniform, The Master Guard Award, the Superior Eyesight award, etc. The Department of the guards determined how many awards have to is given to each of the guards. An award can is given to more than one guard. However, you are have to pay attention to one thing:you should is not give the same award to two neighbors, since a guard cannot Be proud to his award if he neighbor already has this award. The task is to write a program this determines how many different types of awards are required to keep all guards Vated.

Input

The input contains several blocks of test eases. The containing a single integerln100000, the number of guard towers, is begins with a. The next n lines correspond to then Guards:each line contains an integer, the number of awards the guard requires. Each guard requires in least 1, and at most l00000 awards. Guardi and I + 1 are neighbors, they cannot receive the same award. The guard and the last guard are also neighbors.

The input is terminated by a blocks with n = 0.

Output

For each test case, you are have to output a line containing a single integer, the minimum numberx of award types us to motivate the guards. It, if we havex types of awards, then we can give as many awards to each guard as he requires, and we can does it in Su Ch a way that's the same type of award is isn't given to neighboring guards. A Guard can receive only one award from each type.

Sample Input

3
4
2
2
5
2 2 2 2 2 5 1 1 1 1 1-0

Sample Output

8
5
3

Ideas:

1 when n is an even number, ans = max (r[i]+r[i+1]), you can draw a map of their own verification;

2 when n is an odd number, it is not satisfied, then we can use the two-point answer and then judge to find the minimum value. If there is a P gift, then the first person's gift is 1~r1, so it is not difficult to find that the most assigned strategy must be this: anyone other than 1, who is numbered even, tries to move forward as far as possible, with the number of odd-numbered people trying to take it backwards. So we just have to finally determine whether the nth person has a conflict with the first person.

3 because the topic does not want us to output the actual scheme, then we only need to record each person in [1,r1] and [r1+1,n] This range to take a few, finally determine whether the nth person has to take [1,R1] the number within the range.

4 Note that the case for n = 1 requires a special sentence.

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.