CF Gym 100637F The Pool for Lucky Ones

Source: Internet
Author: User
Tags define local

Test instructions: Give you a series of nonnegative integers, you can add a non-zero minus 1, add to the adjacent number, to make all the largest number and minimum.

The puzzle: simulation can be over. can also be analyzed, you can reduce the maximum number and if the maximum number appears greater than or equal to 3 times, you can add one to the maximum number, or a maximum number minus one, the maximum number appears reduced one time. But the drug takes care of some special cases, as detailed below.

Sweep first, if the maximum number is 0, then output 0, if the maximum number is 1, then if it can be combined into 2, then output 2, otherwise the output number and sum. Otherwise, see the maximum number of occurrences, if for a time, then check if there is a number smaller than it 3, (if it is only 2 smaller than it, then the operation will appear at least two times the number of large numbers), if there is a check the operation before the number of large numbers, and then output Max or Max-1 according to the check If there are two times, check two maximum numbers around there is no smaller than it 2 of the number, if there is output Max, there is no output max+1, (max>=2 and do not meet the former, there must be a number not 0); for times greater than or equal to 3 times, check the maximum number of digits around a non 0 number, If there is output max+1, otherwise sum-max.

#include <cstdio>#include<cmath>#include<vector>#include<map>#include<Set>#include<algorithm>using namespacestd;//#define LOCALtypedefLong Longll;Const intMAXN = 1e5+5;intP[MAXN];intMain () {#ifdef local freopen ("In.txt","R", stdin); //freopen ("OUT.txt", "w", stdout);#endif //Local    intN; scanf ("%d",&N); ll Max=0; intpos =-1;  for(inti =0; i < n; i++) {scanf ("%d", p+i); if(p[i]>Max) {Max= P[i];p OS =i; }    }    if(Max = =0) {printf ("0");return 0;} if(Max = =1){        intsum = p[0]; BOOLCanmerge =false;  for(inti =1; I < n; i++) {sum+=P[i]; if(p[i]&&p[i-1]) {Canmerge =true; Break; }        }        if(Canmerge) printf ("2"); Elseprintf"%d", sum); return 0; }    Long Longsum =0; BOOLCanmerge =false; BOOLLessone =false;  for(inti = pos; I < n; i++){        if(P[i] = = Max) sum + =Max; }    if(Sum = =Max) {        if(pos>0) {if(p[pos-1]<=p[pos]-3) Lessone =true; } if(pos<n-1) {if(p[pos+1]<=p[pos]-3) Lessone =true; } if(lessone) { for(inti =0; I < n; i++){                if(P[i] = = max-1) {printf ("%d", Max); return 0; }} printf ("%d", max-1); }        Elseprintf"%d", Max); return 0; }    if(Sum = = max*2){//Keng         for(inti = pos; I < n; i++){            if(P[i] = =Max) {                if((i>0&& p[i-1]<= max-2)|| (i<n-1&& p[i+1] <= max-2) {printf ("%d", Max);return 0; }}} printf ("%d", max+1); }     for(inti = pos; I < n; i++){        if(P[i] = =Max) {            if((i>0&& p[i-1])|| (i<n-1&& p[i+1]) {printf ("%d", max+1);return 0; } }} printf ("%i64d", sum-Max); return 0;}

CF Gym 100637F The Pool for Lucky Ones

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.