PAT 2020年秋季 7-1 Panda and PP Milk (20 分)

PP milk (盆盆奶)is Pandas' favorite. They would line up to enjoy it as show in the picture. On the other hand, they could drink in peace only if they believe that the amount of PP milk is fairly distributed, that is, fatter panda can have more milk, and the ones with equal weight may have the same amount. Since they are lined up, each panda can only compare with its neighbor(s), and if it thinks this is unfair, the panda would fight with its neighbor.

Given that the minimum amount of milk a panda must drink is 200 ml. It is only when another bowl of milk is at least 100 ml more than its own that a panda can sense the difference.

Now given the weights of a line of pandas, your job is to help the breeder(饲养员)to decide the minimum total amount of milk that he/she must prepare, provided that the pandas are lined up in the given order.

Input Specification:

Each input file contains one test case. For each case, first a positive integer n (≤10^4​​) is given as the number of pandas. Then in the next line, n positive integers are given as the weights (in kg) of the pandas, each no more than 200. the numbers are separated by spaces.

Output Specification:

For each test case, print in a line the minimum total amount of milk that the breeder must prepare, to make sure that all the pandas can drink in peace.

Sample Input:

10
180 160 100 150 145 142 138 138 138 140

Sample Output:

3000

Hint:

The distribution of milk is the following:

400 300 200 500 400 300 200 200 200 300

实现思路:

说实话吧,这一场九月我考了第一题直接懵逼,这次满分只有二十多个,应该是历年最少了,这道题出题命题有问题,就题目的说法,题目愿意说如果一只熊猫发现自己的奶比邻居少100ml以上就会察觉,并且打架,那么按照这个思路200 500 400中间这三组肯定要打架了啊,真正的题意应该是至少满足和一个邻居比较牛奶不少于100ml就不会打架吧,又是一道狼人杀一样的文字题,服气了...满分选手我想应该都是通过样例硬推的答案,真正的题目意思应该是这样的:
从左到右比较一次,从右到左比较一次
1.如果熊猫自己比邻居熊猫都重,那就比邻居最重的熊猫多喝100ml。
2.如果熊猫自己和邻居熊猫中最重的那只一样重,那就最重的那个邻居和一样的奶。
3.如果当前熊猫比左邻居体重小,则喂200ml,然后继续比较,当从右到左比较也是一样的规则。

AC代码:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N=10010;
int hashT[N];

int main() {
	int n,val,ord1[N],ord2[N];
	cin>>n;
	vector<int> sq(n);
	for(int i=0; i<n; i++)
		scanf("%d",&sq[i]);
	ord1[0]=200;
	for(int i=1; i<n; i++) {
		if(sq[i]>sq[i-1]) ord1[i]=ord1[i-1]+100;
		else if(sq[i]==sq[i-1]) ord1[i]=ord1[i-1];
		else ord1[i]=200;
	}
	ord2[n-1]=200;
	for(int i=n-2; i>=0; i--) {
		if(sq[i]>sq[i+1]) ord2[i]=ord2[i+1]+100;
		else if(sq[i]==sq[i+1]) ord2[i]=ord2[i+1];
		else ord2[i]=200;
	}
	int ans=0;
	for(int i=0; i<n; i++) ans+=max(ord1[i],ord2[i]);
	cout<<ans;
	return 0;
}
posted @ 2021-03-10 21:39  coderJ_ONE  阅读(133)  评论(0编辑  收藏  举报