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 (≤104) 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
这道题读了好久没弄懂,还是小美女帮我理清思路了。
题目大意:
一排熊猫在喝“盆盆奶”,他们不打架的规则是:体重不同的熊猫喝的牛奶可以不同,但是体重相同的熊猫必须一样多。胖的熊猫喝的可以多一些。
思路:
①从左往右遍历:
第一个设为200ml,如果第二个比他多就+100,相等就相同,比他少就直接200;
②从右往左遍历:
倒数第一个设为200ml,同理;
③从左往右遍历取max:
这样,就可以保证所有的熊猫既不会和左边的打起来,也不会和右边的打起来。
代码:
// 03.12 20:00
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10010;
struct Panda {
int weight;
int m1; // 从左到右
int m2; // 从右到左
}panda[maxn];
int main() {
freopen("input.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &panda[i].weight);
}
// 从左到右
panda[0].m1 = 200;
for (int i = 1; i < n; i++) {
if (panda[i].weight > panda[i - 1].weight)
panda[i].m1 = panda[i - 1].m1 + 100;
else if (panda[i].weight == panda[i - 1].weight)
panda[i].m1 = panda[i - 1].m1;
else
panda[i].m1 = 200;
}
// 从右到左
panda[n - 1].m2 = 200;
for (int i = n - 2; i >= 0; i--) {
if (panda[i].weight > panda[i + 1].weight)
panda[i].m1 = panda[i + 1].m1 + 100;
else if (panda[i].weight == panda[i + 1].weight)
panda[i].m1 = panda[i + 1].m1;
else
panda[i].m1 = 200;
}
// 取两个序列的max,并累加到总牛奶sum
int sum = 0;
for (int i = 0; i < n; i++){
int ans = (panda[i].m1 > panda[i].m2) ? panda[i].m1 : panda[i].m2;
sum += ans;
}
printf("%d\n", sum);
return 0;
}