CF 610B Vika and Squares

来源:互联网 发布:自学网c语言 编辑:程序博客网 时间:2024/05/28 15:29

用颜料涂正方形的问题。

Description
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.

Input
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.

Output
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.

Sample Input

Input
5
2 4 2 3 3
Output
12
Input
3
5 5 5
Output
15
Input
6
10 10 10 1 10 10
Output
11

Hint
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.

大概题意:
就是用颜料桶给正方形涂颜色。但是每桶中的颜料数不同,必须一个一个按照顺序涂,按颜料桶顺序涂颜色,直到涂到颜料桶里没有颜料为止,求出所能涂的最多的正方形数目。

解题思路:
1.求出最小值,应该从最小值的后一个数开始。
2.此时分两种情况,只有1个最小值,和有2个及以上的最小值。这时就需要比较从哪个最小值开始。
这里可以有一个特殊情况,就是所有数相等,那么相加即可。也可以不分。
3.将出现最小值的位置记录下来(数组),再比较出现最小值位置相差多少(数组)。从位置相差比较大的那个位置开始算。
4.注意数值溢出,需要用到long long int.

代码:

#include<stdio.h>#include<iostream>#include<algorithm>#define N 200010#define M 200int main(){    long long int n,a[N];    while(scanf("%lld",&n)!=EOF)    {        for(int i=0;i<n;i++)            scanf("%lld",&a[i]);       long long int min=a[0];       for(int i=1;i<n;i++)        if(a[i]<min)           min=a[i];        int k,count=0,t[n],w[n],j=0;        for(int i=0;i<n;i++)             if(a[i]==min)             {                 count++;                 t[j]=i;                 j++;             }    if(count==1)       printf("%lld\n",(min+1)*n-1);    else if(count==n)  printf("%lld\n",n*min);    else    {        int i;        for(i=1;i<count;i++)            w[i-1]=t[i]-t[i-1]-1;        w[i-1]=n-1-t[i-1]+t[0];        int t=i;        long long int max=w[0];        for(i=1;i<t;i++)        if(w[i]>max)           max=w[i];        printf("%lld\n",max+min*n);    }    }    return 0;}

个人认为这个程序非常繁琐,但是不知如何优化,ac的时候我真是激动万分。
这边还有那个M的问题,本来我也想像N一样大,可是貌似申请内存过多,程序根本无法运行。
就减小了M的值,这边就感觉会不太严谨。

0 0
原创粉丝点击