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

Vika有n个装有不同颜色油漆的罐子,需要在无限长的1单位宽度的画纸上按特定规则涂色。题目要求找出Vika能按规则涂色的最大数量。解决方案是找到最小油漆量的起始颜色,并计算减去最小量后的最长非零序列长度。
摘要由CSDN通过智能技术生成

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
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
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值