给定包含N个整数的数组A1, A2, ... AN,你可以选择任意一个Ai,将Ai旋转到数组第一项,即将数组变成:
Ai, Ai+1, Ai+2, ... AN, A1, A2, ..., Ai-1
现在小Hi希望旋转之后的数组满足:
对于任意K(1 ≤ i ≤ N),前K项的和都是正数。
例如对于A=[3, -5, 2, -2, 3, 0],旋转成[3, 0, 3, -5, 2, -2]满足条件。
请你输出i,代表将Ai旋转到第一项满足条件。
如果有多解,你可以输出任意一个i。如果无解输出-1。
Input第一行包含一个整数N。
第二行包含N个整数A1, A2, ... AN。
对于50%的数据,1 ≤ N ≤ 1000
对于100%的数据,1 ≤ N ≤ 100000, -1000000 ≤ Ai ≤ 1000000
Output一个整数表示答案。
6 3 -5 2 -2 3 0Sample Output
5
这题想不到暴力能过,那就暴力吧,刚开始把无解情况的输出写成了
cout << '-1' << endl;
wr了几次下次要细心点
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1e5 + 10;
ll th[inf];
int main()
{
int n;
scanf("%d", &n);
for(int i = 0; i < n; i ++){
scanf("%lld", &th[i]);
}
int i, j, z;
for(i = 0; i < n; i ++){
ll tot;
if(th[i] <= 0) continue;
tot = th[i];
for(j = i + 1; j < n; j ++){
tot += th[j];
if(tot <= 0) break;
}
for(z = 0;z < i; z ++){
tot += th[z];
if(tot <= 0) break;
}
if(z == i && j == n) break;
}
int ans = -1;
if(i != n) cout << i + 1 << endl;
else cout << -1 << endl;
return 0;
}