链接:https://www.nowcoder.com/acm/contest/59/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
给你一个长为n的序列a
定义f(i,j)=(i-j)2+g(i,j)2
g是这样的一个函数
求最小的f(i,j)的值,i!=j
输入描述:
第一行一个数n
之后一行n个数表示序列a
输出描述:
输出一行一个数表示答案
示例1
输入
4
1 0 0 -1
输出
1
备注:
对于100%的数据,2 <= n <= 100000 , |ai| <= 10000
这题大概就是和一个序列求和为k是一样的思路。
#include<bits/stdc++.h>
#define PI 3.1415926
#define INF 1e18
#define inf 1e9
#define min(a,b) a<b?a:b
#define max(a,b) a>b?a:b
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 100005;
int num[_max];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
while(cin>>n){
ll sum = INF;
ll cnt = INF;
ll cnt1 = 0;
int k1 = 0;
int k = 0;
int a;
for(int i = 1 ; i <= n ; i++){
cin >> a;
cnt1 += a;
k1++;
ll ch1 = k1*k1;
if(abs(a) <= cnt+a){
cnt = a;
k = 1;
}
else{
cnt += a;
k++;
}
ll ch = k*k;
if(ch+cnt*cnt < sum)
sum = ch+cnt*cnt;
else if(ch > sum){
k = 0;
cnt = INF;
}
if(ch1+cnt1*cnt1<sum){
sum = ch1+cnt1*cnt1;
cnt = cnt1;
k = k1;
}
if(ch1 > 10000*10000){
k1 = 0;
cnt1 = 0;
}
//cout<<sum<<endl;
}
cout << sum <<endl;
}
return 0;
}