Multivariate function (第三次积分赛)
Multivariate function
描述
输入
第一行一个整数T,表示T组测试数据 (1≤T≤10).
每组数据第一行一个整数n(4≤n≤1000).
第二行 n个浮点数: X1,X2X3......Xn(1<=Xi<=1000000)X1,X2X3......Xn(1<=Xi<=1000000)
输出
输出最大值 y,保留三位小数.
输入样例 1
2
4
1.0 2.0 3.0 4.0
5
1.6 2.6 7.1 2.3 2.6
输出样例 1
0.167
1.530
首先我们将题意进行转化
y=max(Xj∗XpXk−XiXi+Xj)y=max(Xj∗XpXk−XiXi+Xj)
然后 O(n2)预处理XpXkO(n2)预处理XpXk ,最后遍历Xi,XjXi,Xj,就可以了时间复杂度
O(n2)O(n2) 题意的数据为1000没有超
AC code:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e3+5;
const double INF = 1.0*0x7f7f7f7f;
double v[maxn],vis[maxn],per[maxn];
int main(){
int t; cin>>t;
while(t--)
{
int n; scanf("%d",&n);
for (int i = 1;i<=n;i++) {
scanf("%lf",&v[i]);
//printf("v[%d] = %lf\n",i,v[i]);
}
for (int i = n-1;i>=1;i--) {
double minn = INF;
for (int j = i+1;j<=n;j++) {
minn = min(minn,v[j]);
}
//printf("minn = %lf\n",minn);
vis[i] = v[i]/minn;
//printf("vis[%d] = %lf\n",i,vis[i]);
}
double maxx = -INF;
for (int i = n-1;i>=1;i--) {
maxx = max(maxx,vis[i]);
per[i] = maxx;
//printf("per[%d] = %lf\n",i,per[i]);
}
double ans = -INF;
for (int i = 1;i<=n-3;i++) {
for (int j = i+1;j<=n-2;j++) {
ans = max(ans,(v[j]*per[j+1]-v[i])/(v[i]+v[j]));
}
}
printf("%.3lf\n",ans);
}
return 0;
}