题目描述
In your bovine history class, you have been given a rather long homework assignment with N questions (3≤N≤100,000), each graded with an integer score in the range 0…10,000. As is often customary, your teacher plans to assign a final grade by discarding a question on which you received the lowest score and then averaging the remaining scores together. Unfortunately, your pet cow Bessie has just eaten your answers to the first K questions! (K could be as small as 1 or as large as N−2).
After copious explanation, your teacher finally believes your story, and agrees to grade the remaining non-eaten part of the assignment the same way as before – by removing the lowest-scoring question (or one such question, in the event of a tie) and averaging the rest.
Please output all values of K which would have earned you the maximum possible score according to this grading scheme, in sorted order.
输入
The first line of input contains N, and the next line contains the scores on the N homework questions.
输出
Please output, one value per line, all values of K which would have earned you the maximum possible score.
样例输入 Copy
5
3 1 9 2 7
样例输出 Copy
2
提示
If Bessie eats the first two questions, then the remaining scores are 9, 2, and 7. Removing the minimum and averaging, we get a final grade of 8, which is the highest possible.
解析:
本题只要维护区间最小即可。
有两种做法
第一种利用后缀维护最小
第二种用线段树维护区间最小
前缀和+后缀维护最小
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1000;
int n;
double a[N];
double minn[N];
double sum[N];
vector<double > v;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
minn[n]=a[n];
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
for(int i=n-1;i>=1;i--) minn[i]=min(minn[i+1],a[i]);
double ans=0;
for(int i=1;i<=n-2;i++)
{
double res=sum[n]-sum[i];
res=res-minn[i+1];
if(res/(1.0*(n-i-1))>ans)
{
ans=res/(1.0*(n-i-1));
}
v.push_back(res/(1.0*(n-i-1)));
}
for(int i=0;i<v.size();i++)
{
if(v[i]==ans) cout<<i+1<<endl;
}
}
线段树维护区间最小
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=5e5+5;
int n;
int maxs[inf*4],mins[inf*4],a[inf];
vector<double > v;
ll sum[inf*4];
void pushup(int rt)
{
maxs[rt]=max(maxs[rt*2],maxs[rt*2+1]);
mins[rt]=min(mins[rt*2],mins[rt*2+1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
maxs[rt]=mins[rt]=a[l];
return ;
}
int m=(l+r)/2;
build(l,m,rt*2);
build(m+1,r,rt*2+1);
pushup(rt);
}
int query_max(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return maxs[rt];
}
int m=(l+r)/2;
int ans=-1;//做临时比较
if(L<=m) ans=max(ans,query_max(L,R,l,m,rt*2));
if(R>m) ans=max(ans,query_max(L,R,m+1,r,rt*2+1));
return ans;
}
int query_min(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return mins[rt];
}
int m=(l+r)/2;
int ans=1000005;//做临时比较
if(L<=m) ans=min(ans,query_min(L,R,l,m,rt*2));
if(R>m) ans=min(ans,query_min(L,R,m+1,r,rt*2+1));
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++ )sum[i]=sum[i-1]+1ll*a[i];
build(1,n,1);
double ans=0;
int pos=0;
// cout<<2<
for(int i=1;i<=n-2;i++)
{
int minn=query_min(i+1,n,1,n,1);
// cout<
ll sumn=sum[n]-sum[i];
// cout<
double res=1.0*sumn-1.0*minn;
if(res/(1.0*(n-i-1) )>ans)
{
ans=res/(1.0*(n-i-1));
pos=i;
}
v.push_back(res/(1.0*(n-i-1)));
}
for(int i=0;i<v.size();i++)
{
if(v[i]==ans) cout<<i+1<<endl;
}
}