纪中训练5月30日提高组T1

【USACO 2017 December Silver】My Cow Ate My Homework

  • 题目
    • Description
    • Input
    • Output
    • Sample Input
    • Sample Output
  • 题解

题目

Description

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.

Input

The first line of input contains N, and the next line contains the scores on the N homework questions.

Output

Please output, one value per line, all values of K which would have earned you the maximum possible score.

Sample Input

5
3 1 9 2 7

Sample Output

2

题解

统计前缀和 s s s
暴力+线段树
暴力减去前 i i i个,统计减掉的和 x x x
然后线段树求出 [ i + 1 , n ] [i+1,n] [i+1,n]里的最小值 m i n min min
算出平均数 ( s − x − m i n ) / ( n − i − 1 ) (s-x-min)/(n-i-1) (sxmin)/(ni1)
统计

#include
#include
#include
#include
using namespace std;
int i,n,s,x,num;
double sum,Max;
int a[100005],ans[100005],tree[400005];
void biuld(int now,int l,int r)
{
	if (l==r)
	{
		tree[now]=a[l];
		return;	
	}
	int mid=(l+r)>>1;
	biuld(now*2,l,mid);
	biuld(now*2+1,mid+1,r);
	tree[now]=min(tree[now*2],tree[now*2+1]);
}
int find(int now,int l,int r,int p,int q)
{
	if (l==p&&r==q)
		return tree[now];
	int mid=(l+r)>>1;
	if (q<=mid) return find(now*2,l,mid,p,q);
	else if (p>mid) return find(now*2+1,mid+1,r,p,q);
	else return min(find(now*2,l,mid,p,mid),find(now*2+1,mid+1,r,mid+1,q));
}
int main()
{
	freopen("homework.in","r",stdin);
	freopen("homework.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		s+=a[i];
	}
	biuld(1,1,n);
	for (i=1;i<=n-2;i++)
	{
		x+=a[i];
		sum=1.0*(s-x-find(1,1,n,i+1,n))/(n-i-1);
		if (sum>Max) 
		{
			memset(ans,0,sizeof(ans));
			num=1;
			ans[num]=i;
			Max=sum;	
		}
		else
		if (sum==Max)
		{
			num++;
			ans[num]=i;
		}		
	}
	for (i=1;i<=num;i++)
		printf("%d\n",ans[i]);
	fclose(stdin);
	fclose(stdout);
	return 0;
}

你可能感兴趣的:(信息学总结)