# AYITACM2016 Province third week m-beijing guards (greedy + two)

Source: Internet
Author: User

Beijing was once surrounded by four rings of city walls:the Forbidden City Wall, the Imperial city
Wall, the Inner City Wall, and nally the Outer city Wall. Most of these walls were demolished in
The 50s and 60s to make -to-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 has
exaetly, neighbors.
the guard had to keep a eye on his sections of the wall all day, so he had to stay in the tower.
This is a very boring job, thus it's important to keep the guards motivated. the best of the motivate
a guard is to give him lots of awards. There is several di?erent 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 is guards determined how many awards has to
be given to each of the guards. An award can is given to more than one guard. However, you have
to pay attention to one thing:you should not give the same award to one neighbors, since a guard
cannot be proud of He award if his neighbor already have this award. The task is to write a program
That's determines how many di?erent types of awards is 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. RST
Guard and the last guard is also neighbors.
The input is terminated by a block with n = 0.
Output
for each test case, you had to output a line containing a single integer, the minimum number x of
award types that allows us to motivate the guards. That's, if we have an X types of awards, then we can
give as many awards to each guard as he requires, and we can does it in such a by that 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

Test instructions

n Individuals, everyone wants a gift, the next two people will chat to show off gifts, can't make them the same gift, ask at least how many gifts to meet the conditions.

Ideas:

The number of gifts for an even number of people is the sum of the two neighbors who want the greatest number of gifts.

Odd number of people words more complex, binary search the smallest gift number p, the gift is divided into two piles, left stacking x, right heap y (x+y=p), Left[i],right[i] respectively said I took the left, right two heaps how many gifts, let the odd number of people try to take from the right, The even number of people try to take from the left, the first person took the left all the X, and finally just to determine whether the last person to take the left gift on the line.

`#include <stdio.h> #include <string.h> #include <algorithm> #include <string>using namespace    Std;int a[100010],left[100010],right[100010],n;int judge (int p) {int x,y,i;    left[1]=a[1],right[1]=0; x=a[1],y=p-a[1];//the gift into two piles for (i=2; i<=n; i++) {if (i%2==0)//If it is an even number of security guards {Left[i]=min (A[i]        , x-left[i-1]);//As far as possible from the left to determine whether a person left a gift is enough, if not enough to take the right right[i]=a[i]-left[i];//record on the right to take a few gifts} else    {right[i]=min (a[i],y-right[i-1]);//As far as possible from the right to determine whether the present is enough left[i]=a[i]-right[i];//records need to get a few from the right. Return left[n]==0;//determine if the last person took the present on the left}int main () {while (~SCANF ("%d", &n) &&n) {int i,l=0,r=0,            M;//L is the lower limit of two points, R is the upper limit for (I=1; i<=n; i++) {scanf ("%d", &a[i]); R=max (r,a[i]*3);//R is three times times the maximum number of security that needs a gift,} if (n==1)//If there is only one security guard, the gift quantity is the quantity he needs {printf ("%d\n", a[1]            );        Continue } a[n+1]=a[1];//because it is a ring, you need to consider the lastOne and the first relationship for (I=1; i<=n; i++) L=max (l,a[i]+a[i+1]);//The minimum value for the sum of the adjacent security gifts if (n%2==1)//If the number of security guards is odd                {while (l<=r)//e to find the optimal solution {m= (l+r)/2;                if (judge (m))//If the gift is sufficient, one more gift r=m-1;            else//If the gift is not enough, increase the gift l=m+1;     } printf ("%d\n", r+1);//Last Output Gift quantity} else//If the number of security is even, the optimal solution is the maximum value of the sum of adjacent security gifts printf ("%d\n", L); } return 0;}`

AYITACM2016 Province third week m-beijing guards (greedy + two)

Related Keywords:

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.