Live Archive 3177 3177, livearchive3177

Source: Internet
Author: User

Live Archive 3177 3177, livearchive3177

3177-Beijing Guards
Time limit: 3.000 seconds
Beijing was once surrounded by four rings of city Wals: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. most of these wallwere demolished in the 50 s and 60 s to make way for roads. the wallwere protected by guard towers, and there was a guard living in each tower. the wall can be considered to be a large ring, where every guard tower has exaetly two neighbors.

The guard had to keep an eye on his section of the wall all day, so he had to stay in the tower. this is a very boring job, thus it is important to keep the guards motivated. the best way to motivate a guard is to give him lots of awards. there are several different types of awards that can be given: the Distinguished Service Award, the Nicest Uniform Award, the Master Guard Award, the Superior Eyesight Award, etc. the Central Department of City Guards determined how many awards have to be given to each of the guards. an award can be given to more than one guard. however, you have to pay attention to one thing: you shocould not give the same award to two neighbors, since a guard cannot be proud of his award if his neighbor already has this award. the task is to write a program that determines how does different types of awards are required to keep all the guards motivated.

Input

The input contains several blocks of test eases. Each case begins with a line containing a single integer l ≤ N ≤ 100000, the number of guard towers. the next n lines correspond to the n guards: each line contains an integer, the number of awards the guard requires. each guard requires at least 1, and at most l00000 awards. guard I and I + 1 are neighbors, they cannot receive the same award. the first guard and the last guard are also neighbors.

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

Output

For each test case, you have to output a line containing a single integer, the minimum number x of award types that allows us to motivate the guards. that is, if we have x types of awards, then we can give as login awards to each guard as he requires, and we can do it in such a way that the same type of award is not 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

N people form a ring. n is adjacent to 1, n-1. Each person needs a certain number of gifts. the gifts of two adjacent persons cannot be the same, and the number of each gift is infinite. Ask the total number of gifts that meet the minimum conditions.
Analysis: If n is an even number, it is the maximum value of two adjacent numbers. If n is an odd number, it can only be enumerated. The gift is divided into two parts: left and right, if you want to select an even number on the left and an odd number on the right, you only need to judge whether l [n] is equal to 0 (because the first 1st are selected on the leftmost, if n and 1 are repeated, then l [n] is certainly not 0 ). (Refer to getting started classic );
Code:

# Include <cstdio> # include <cstring> # include <algorithm> const int maxn = 1e5 + 5; using namespace std; int l [maxn], r [maxn], s [maxn], n; bool test (int p) {int x = s [1], y = p-x; l [1] = s [1], r [1] = 0; // divide p into two parts based on the first one. Because the first one is on the left of the selected one, select the right one for the next one, in this case, the last one is to select the right one. If l [n] = 0, it means that the requirements are met (clever and worth thinking about) for (int I = 2; I <= n; + I) {if (I % 2 = 0) {l [I] = min (x-l [I-1], s [I]); r [I] = s [I]-l [I];} else {r [I] = min (y-r [I-1], s [I]); l [I] = s [I]-r [I];} return l [n] = 0;} int main () {while (scanf ("% d", & n), n) {for (int I = 1; I <= n; ++ I) scanf ("% d ", & s [I]); if (n = 1) {printf ("% d \ n", s [1]); continue ;} s [n + 1] = s [1]; int L = 0; for (int I = 1; I <= n; ++ I) L = max (L, s [I] + s [I + 1]); if (n % 2 = 1) {int R = L * 3; // for (int I = 1; I <= n; ++ I) R = max (R, s [I] * 3); int m; while (L <R) {m = L + (R-L) /2; if (test (m) R = m; else L = m + 1 ;}} printf ("% d \ n", L) ;}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.