Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/gym/100637/problem/F
Description
A new swimming pool has been built on Kazan for the forthcoming Water Sports World Championship. The pool has N lanes. Some of the lanes is already occupied by swimmers. Tatar scientists has divided the lanes into the lucky and unlucky ones. The unlucky lanes is those with the maximum amount of swimmers. That's, there is no other lane where there would being more swimmers than on unlucky one. The unlucky lanes make swimmers unhappy. The rest of the lanes is considered to be lucky. The lucky lanes make people happy. The scientists took a decision to make more people happy. In order to does this they had an agreement with the pool manager saying they can move a single person from any lane to the One neighboring if it was necessary. The swimmer from the first lane can is moved to the second lane, and the swimmer from the last lane––to the one BEF Ore last.
Input
The first line contains a integer n-the amount of lanes in the pool (3≤n≤105). The second line contains N integers pi separated with spaces, describing distribution of swimmers between the lanes where Pi is the amount of swimmers on I-th Lane (0≤pi≤105).
Output
Output a single number-minimal possible number of unhappy swimmers.
Sample Input
3
1 3 5
Sample Output
5
HINT
Test instructions
Swimming pool, the most unhappy people can move a person randomly to the adjacent pool, ask the least number of unhappy people is how much
Exercises
I can only say that considering a variety of situations, violence is good.
Code
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <ctime>5#include <iostream>6#include <algorithm>7#include <Set>8#include <vector>9#include <sstream>Ten#include <queue> One#include <typeinfo> A#include <fstream> -#include <map> -#include <stack> the typedef __int64 LL; - using namespacestd; - inline ll read () - { +ll x=0, f=1; - CharCh=GetChar (); + while(ch<'0'|| Ch>'9') A { at if(ch=='-') f=-1; -Ch=GetChar (); - } - while(ch>='0'&&ch<='9') - { -x=x*Ten+ch-'0'; inCh=GetChar (); - } to returnx*F; + } - //************************************************************************************** the inthash[200005]; * intN; $ll a[200005];Panax Notoginseng intMain () - { the +scanf"%d",&n); A for(intI=1; i<=n; i++) the { +scanf"%i64d",&a[i]); -hash[a[i]]++; $ } $ ll Maxx; - for(intI=100000; i>=0; i--) - if(Hash[i]) the { -maxx=i;Wuyi Break; the } - if(maxx==0) Wu { -printf"0\n"); About return 0; $ } -a[0]=0; -a[n+1]=0; - if(hash[maxx]!=1) A { +ll ans=hash[maxx]*Maxx; the for(intI=1; i<=n; i++) - { $ if(a[i]==Maxx) the { the if(a[i+1]|| a[i-1]) the { theAns=min (ans,maxx+1); - } in if(a[i+1]<maxx-1&&i+1<=N) the { theAns=min ((hash[maxx]-1)*(Maxx), ans); About //return 0; the } the if(a[i-1]<maxx-1&&i-1>=1) the { +Ans=min ((hash[maxx]-1)*(Maxx), ans); - //return 0; the }Bayi } the } theprintf"%i64d\n", ans); - } - Else the { the for(intI=1; i<=n; i++) the { the if(a[i]==Maxx) - { the if(a[i+1]<maxx-2&&i+1<=n&&hash[maxx-1]==0) the { theprintf"%i64d\n", maxx-1);94 return 0; the } the if(a[i-1]<maxx-2&&i-1>=1&&hash[maxx-1]==0) the {98printf"%i64d\n", maxx-1); About return 0; - }101 }102 }103printf"%i64d\n", Maxx);104 } the 106 return 0;107}
View Code
Gym 100637F F. The Pool for Lucky Ones