ZSJZ训练 2020.05.30【NOIP提高组】模拟 反思&题解

这次考试才110分,第19,不满意!

T1:【USACO 2017 December Silver】My Cow Ate My Homework

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.

在你的历史课上,你得到了一个很长的作业。这个作业包含了N个题目(3 ≤ N ≤ 100,000),每个题目的成绩在0~10,000之间。

按照惯例,你的老师按照以下方式计算最终成绩:去掉你最低的一个成绩,然后将其余成绩的平均成绩作为最终成绩。但不幸的是,你的宠物牛“贝西”刚刚吃了前K个题目的答案!(1 ≤ K ≤ N-2)

经过你的一番解释,老师终于相信了你的故事,并且同意对你有答案的题目(没有被吃掉答案的题目)像之前一样给分——通过去掉最低的成绩(如果有多个最低成绩,则只去掉其中一个)并取剩余成绩的平均成绩。
根据这一成绩计算方案,请按升序输出所有可以使你最终成绩最高的K的值。

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

反思&题解

考试&正解思路: 暴力枚举区间,线段树取最小值删掉之后判断更形就行了
反思: 比较水,签到题

CODE

#include
using namespace std;
int n,mn,sum[100005],ans[100005],a[100005],tree[400005];
void build(int now,int l,int r)
{
	int mid;
	
	if (l==r) tree[now]=a[l];
	else
	{
		mid=l+r>>1;
		build(now<<1,l,mid);
		build(now<<1|1,mid+1,r);
		tree[now]=min(tree[now<<1],tree[now<<1|1]);
	}
}
void find(int now,int l,int r,int x,int y)
{
	int mid;
	
	if (l==x && r==y) mn=min(mn,tree[now]);
	else
	{
		mid=l+r>>1;
		if (y<=mid) find(now<<1|1,l,mid,x,y);
		else if (x>mid) find(now<<1|1,mid+1,r,x,y);
		else
		{
			find(now<<1,l,mid,x,mid);
			find(now<<1|1,mid+1,r,mid+1,y);
		}
	}
}
int main()
{
	freopen("homework.in","r",stdin);
	freopen("homework.out","w",stdout);
	scanf("%d",&n);
	sum[0]=0;
	int i;
	for (i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	build(1,1,n);
	int tot1;
	double mx=-12345678;
	for (i=1;i<=n-2;i++)
	{
		int tot=sum[n]-sum[i];
		mn=12345678;
		find(1,1,n,i+1,n);
		if (1.0*(tot-mn)/(n-i-1)>mx)
		{
			mx=1.0*(tot-mn)/(n-i-1);
			tot1=1;
			ans[1]=i;	
		}
		else if (1.0*(tot-mn)/(n-i-1)==mx)
		{
			tot1++;
			ans[tot1]=i;	
		}	
	}
	for (i=1;i<=tot1;i++)
		printf("%d\n",ans[i]);
	
	return 0;
}

T2:【USACO 2017 December Gold】Haybale Feast

Description

Farmer John is preparing a delicious meal for his cows! In his barn, he has N haybales (1≤N≤100,000). The ith haybale has a certain flavor Fi (1≤Fi≤10^9) and a certain spiciness Si (1≤Si≤10^9). The meal will consist of a single course, being a contiguous interval containing one or more consecutive haybales (Farmer John cannot change the order of the haybales). The total flavor of the meal is the sum of the flavors in the interval. The spiciness of the meal is the maximum spiciness of all haybales in the interval.

Farmer John would like to determine the minimum spiciness his single-course meal could achieve, given that it must have a total flavor of at least M (1≤M≤10^18).
在这里插入图片描述

Input

The first line contains the integers N and M, the number of haybales and the minimum total flavor the meal must have, respectively. The next N lines describe the N haybales with two integers per line, first the flavor F and then the spiciness S.

Output

Please output the minimum spiciness in a single course meal that satisfies the minimum flavor requirement. There will always be at least one single-course meal that satisfies the flavor requirement.

Sample Input

5 10
4 10
6 15
3 5
4 9
3 6

Sample Output

9

反思&题解

考试思路: 暴力枚举区间,线段树取最大值判断
正解思路: 二分加一个简单的判断
反思: 又是一道在考试的时候想到正解的题,只是判断的一个细节有问题,拖到比赛快结束,只能打暴力……

CODE

#include
long long m;
int f[100005],s[100005],n;
bool check(int k)
{
    long long tot=0;
    int i;
    for (i=1;i<=n;i++)
    {
        if (s[i]>k)
		{
			tot=0;
			continue;
		}
        tot+=f[i];
        if (tot>=m) return true;
    }
    return false;
}
int main()
{
	freopen("hayfeast.in","r",stdin);
	freopen("hayfeast.out","w",stdout);
    scanf("%d%lld",&n,&m);
    int i;
    for (i=1;i<=n;++i)
    	scanf("%d%d",&f[i],&s[i]);
    int mid,l=0,r=1000000000;
    while (l<r)
    {
        mid=(l+r)>>1;
        if (check(mid)) r=mid;
		else l=mid+1;
    }
    printf("%d\n",l);
}

T3:【USACO 2017 December Gold】A Pie for a Pie

Description

Bessie and Elsie have each baked N pies (1≤N≤10^5). Each of the 2N pies has a tastiness value according to Bessie and a (possibly different) tastiness value according to Elsie. Bessie is thinking about giving one of her pies to Elsie. If Elsie receives a pie from Bessie, she will feel obligated to give one of her pies to Bessie. So as to not appear stingy nor flamboyant, Elsie will try to pick a pie that is at least as tasty (in Elsie’s eyes) as the pie she received, but no more than D units tastier (0≤D≤10^9). Such a pie may not exist, in which case Elsie will adopt a pseudonym and exile herself to Japan.

But if Elsie does give Bessie a pie in return, Bessie will similarly try to give Elsie a pie which is at least as tasty but no more than D units tastier (in Bessie’s eyes) as the pie Elsie just gave her. Should this be impossible, Bessie too will exile herself. Otherwise she will give her chosen pie to Elsie. This cycle will continue until one of the cows is exiled, an unhappy outcome, or one of the cows receives a pie which she accords a tastiness value of 0, in which case the gift exchange will end and both cows will be happy.
Bessie和Elsie各自烤了 N(1≤N≤10^5)个馅饼)。Bessie 会这 2N 个馅饼打分,Elsie 也会。二者的打分均为一个<=10^9的非负整数。由于她们口味不同,每个派的两个分数可能不同。 她们想互赠礼物。开始时,Bessie 送给 Elsie 一个馅饼。她们收到礼物(对方做的馅饼)后都会回赠对方一个自己做的馅饼。 她们选择回礼的方法相同。以 Elsie 为例,Elsie 根据自己的打分来选择回礼。回礼的分数至少要大于等于她收到的馅饼的分数,但两个馅饼的分数差不能大于 D(0≤D≤10^9) 。如果有多个馅饼满足要求,Elsie 可以选择其中的任意一个作为回礼。若没有馅饼满足要求,Elsie 会放弃。Bessie 选择回礼的方法同理。 她们之间的礼物交换将持续到一头牛放弃(Bad End),或一头牛收到一个她自己打分为 00 的馅饼,此时礼物交换愉快结束(Happy End)。 请注意,不能把一个馅饼赠送两次,不能把馅饼送给自己。 Bessie 想知道:对于每个她做的馅饼,如果她将这个馅饼作为最开始送给 Elsie 的礼物,她俩至少要互赠多少次馅饼(Bessie 给 Elsie 算一次,Elsie 回赠 Bessie 又算一次),才能 Happy End 。如果不可能 Happy End,请输出 -1。

Input

The first line contains the two integers N and D. The next 2N lines contain two space-separated integers each, respectively denoting the value of a particular pie according to Bessie, and the value of that pie according to Elsie.

The first N lines refer to Bessie’s pies, and the remaining N lines refer to Elsie’s pies.

It is guaranteed that all tastiness values are in the range [0,10^9].
第一行有两个整数 N, D。
在接下来的 2N行中,每行有两个整数,表示对于某个馅饼,Bessie 的打分和 Elsie 的打分。其中,前 N 行表示 Bessie 做的馅饼,后 N行则是 Elsie 做的馅饼。

Output

here should be N lines in the output. Line i should contain a single integer: the minimum number of pies that could be gifted in a happy gift exchange started with Bessie’s pie i. If no gift exchange starting with pie i is happy, then line i should contain the single integer −1 instead.
输出共 N 行,每行一个整数。 第 i 行的整数表示:如果 Bessie 将馅饼 i作为最开始送给 Elsie 的礼物,她俩至少要互赠多少次馅饼才能 Happy End 。如果不可能 Happy End,请在该行输出 -1。

Sample Input

2 1
1 1
5 0
4 2
1 4

Sample Output

3
1

反思&题解

正解思路: SPFA,建边线段树优化。
反思: 毒瘤……
这题就不放code了,主要是因为我不会,没码完……

你可能感兴趣的:(反思,题解)