Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i.
Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops.
Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of jars with colors Vika has.
The second line of the input contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has.
The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above.
5
2 4 2 3 3
12
3
5 5 5
15
6
10 10 10 1 10 10
11
In the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.
In the second sample Vika can start to paint using any color.
In the third sample Vika should start painting using color number 5.
比赛的时候没有做出
题意: n种颜色的涂料 1,2,3,,,n
ai代表相应涂料的i有多少升,题目要求 若当前使用涂料x 下一次必须使用涂料x+1 当x==n时 下一次使用涂料1 也就是循环的
问 最多能使用多少涂料
解: 简化一下: 找到涂料最少的容量minx n*minx 为最少使用 其次 便是在一个循环链中寻找最长连续!=minx的子序列的长度
这种是 模仿晏的
#include<bits/stdc++.h> using namespace std; #define LL __int64 LL a[200005]; LL max (LL a, LL b) { if(a>=b) return a; else return b; } int main() { LL n,minx; scanf("%I64d",&n); scanf("%I64d",&a[1]); //if(n==1) // printf("1 "); // else minx=a[1]; for(LL i=2; i<=n; i++) { scanf("%I64d",&a[i]); if(minx>a[i]) minx=a[i]; } LL mm=0,nn=0; for(LL i=1; i<=n; i++) { if(a[i]!=minx) nn++; else { mm=max(mm,nn); nn=0; } mm=max(mm,nn); } //cout<<mm<<endl; int xx=0; for(int i=1; i<=n; i++) { if(a[i]==minx) break; else xx++; } //mm=max(mm,xx); //xx=0; for(int i=n; i>=1; i--) if(a[i]==minx) break; else xx++; mm=max(mm,xx); // cout<<mm<<endl; printf("%I64d ",minx*n+mm); return 0; }
然后机智一下 想起之前的回文操作 加一倍变成a[2*n] 呵呵
#include<bits/stdc++.h> using namespace std; #define LL __int64 LL a[400005]; LL max (LL a, LL b) { if(a>=b) return a; else return b; } int main() { LL n,minx; scanf("%I64d",&n); scanf("%I64d",&a[1]); //if(n==1) // printf("1 "); // else minx=a[1]; a[n+1]=a[1]; for(LL i=2; i<=n; i++) { scanf("%I64d",&a[i]); a[n+i]=a[i]; if(minx>a[i]) minx=a[i]; } LL mm=0,nn=0; for(LL i=1; i<=2*n; i++) { if(a[i]!=minx) nn++; else nn=0; mm=max(mm,nn); } // cout<<mm<<endl; printf("%I64d ",minx*n+mm); return 0; }