Codeforces610b
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces610b相关的知识,希望对你有一定的参考价值。
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
5
2 4 2 3 3
12
3
5 5 5
15
6
10 10 10 1 10 10
11
Sample Output
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.
**************************************************************************************************************
这道题是一道思路题,有了思路代码实现还是很简单的,但是做这题是我还是错了好多遍,没有及时改过来因为错误显示TLE在第6组,而我看成WA在第六组,这些小细节平时是不会犯得,但一旦发生,这道题多半就要扔了,所以以后一定要小心。
还有就是这题开始做时超时因为在for循环中用了while后来再做别的题时突然想到可以把每次出现最小值的位置都保存起来这样的就可以方便比较位置之间的距离了,我本想用数组解决,但看了别人的代码使用vector解决的觉得比数组好很多至少数组要开很大才可以保证放得下,而这样会占用很多空间
这里我先发一下我第一次错的代码,我while用的时候是想通过取余实现循环,直到出现最小为止
1 #include<iostream> 2 using namespace std; 3 long long a[500000]; 4 int main() 5 { 6 long long n; 7 while(cin>>n) 8 { 9 cin>>a[0]; 10 long long xmin=a[0]; 11 for(long long i=1;i<n;i++) 12 { 13 cin>>a[i]; 14 if(xmin>a[i]) 15 xmin=a[i]; 16 } 17 18 int xmax=0; 19 for(long long i=0;i<n;i++) 20 { 21 long long ans=0; 22 long long j=i; 23 if(a[i]>xmin) 24 { 25 while(a[j%n]>xmin)//比较当前是否大于最小 26 { 27 28 ans++; 29 j++;//计算下一个 30 } 31 } 32 // cout<<ans<<endl; 33 if(ans>xmax) 34 xmax=ans; 35 } 36 37 cout<<xmin*n+xmax<<endl; 38 } 39 }
这里我在引用一下别人的代码是AC的:
#include<bits/stdc++.h> using namespace std; int Min = 1e9+5,ans=0; int a[200005]; vector<int>P; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { if(Min>=a[i]) { Min = a[i]; ans = i; } } int first = 0; for(int i=1;i<=n;i++) { if(a[i]==Min) P.push_back(i); } P.push_back(P[0]+n); long long tmp = 0; for(int i=1;i<P.size();i++) tmp = max(tmp,1LL*P[i]-1LL*P[i-1]-1LL); printf("%lld\n",1LL*n*Min+1LL*tmp); }
自己打一遍好费劲,所以我决定先发一篇博客在自己敲一遍。
以上是关于Codeforces610b的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 86C Genetic engineering(AC自动机+DP)
CodeForces 1005D Polycarp and Div 3(思维贪心dp)
(Incomplete) Codeforces 394 (Div 2 only)