Codeforces - 337B(div2) - Vika and Squares(练习)

来源:互联网 发布:知乎 pocket 编辑:程序博客网 时间:2024/06/08 17:59

B. Vika and Squares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 123and 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 test(s)
input
52 4 2 3 3
output
12
input
35 5 5
output
15
input
610 10 10 1 10 10
output
11
Note

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-N,接下来给出每种油漆还有多少升,刷一个格子要用掉一升的油漆。然后这个人刷油漆非要挨着用,就是油漆2用完非要用3,N用完拐回来用1,不能跳着用,直到油漆用完为止。问这个人最多能涂多少个格子

本来是想着找出最小的数的开始位置和结束位置,然后n * mmin +(从结束位置数到开始位置)就行了,wa了以后还不知为毛会wa= =直到自己测了几组例子发现.......尼玛中间的阶段更长怎么办!!!算中间的话中间还有最小的数怎么办!!!于是知道自己思路错错错错错= =

找出最小的数,所有的数减去这个最小的数,扫一遍找出不为0最长的序列的长度就好了。为了N跳到1方便,我把数组续到了2N


#include <iostream>#include <algorithm>#include <cstdio>#include <stdlib.h>#include <cmath>#include <cstring>#include <string.h>#include <cstdlib>#include <iostream>#include <set>#include <functional>#include <vector>#include <fstream>#include <iomanip>using namespace std;int a[400005];int main(){  int n;  while(scanf("%d", &n) != EOF){    int mmin = 1000000000;    for(int i = 1; i <= n; i++){      scanf("%d", &a[i]);      mmin = min(mmin, a[i]);    }    for(int i = 1; i <= n; i++){      a[i] -= mmin;    }    long long ans = 0; // 必须是longlong,int会爆数据,人蠢wa了一发后还反应了半天怎么就又wa了= =    int cnt = 0;    for(int i = n + 1; i <= 2 * n; i++){      a[i] = a[i - n];    }    for(int i = 1; i <= 2 * n; i++){      if(a[i]){        cnt++;        ans = max((long long)cnt, ans);  //上次那道题目也是,其实蛮惊讶max不能比较int和longlong,类型不同max就报错,不过这或许算是为了函数内部安全?      }      else        cnt = 0;    }    ans = ans + (long long)mmin * n; //光顾着上面longlong,这块longlong又掉了23333    printf("%I64d\n", ans);  }  return 0;}


0 0
原创粉丝点击