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
思路:将所有点奶量初始化为200,然后所有点用两个函数来走,一个向左,一个向右
trail函数:如果不严格有向,按照要求假想增加,如果当前值比假想值小,更新当前值
AC代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,sum=0;
struct node{
int weight;
int milk=200;
}Node[10000];
void trailleft(int index){
if(index-1<0) return;
if(Node[index-1].weight>Node[index].weight){
if(Node[index-1].milk<Node[index].milk+100){
Node[index-1].milk=Node[index].milk+100;
}
}else if(Node[index-1].weight==Node[index].weight){
if(Node[index-1].milk<Node[index].milk){
Node[index-1].milk=Node[index].milk;
}
}else{
return;
}
trailleft(index-1);
}
void trailright(int index){
if(index+1>n) return;
if(Node[index+1].weight>Node[index].weight){
if(Node[index+1].milk<Node[index].milk+100){
Node[index+1].milk=Node[index].milk+100;
}
}else if(Node[index+1].weight==Node[index].weight){
if(Node[index+1].milk<Node[index].milk){
Node[index+1].milk=Node[index].milk;
}
}else{
return;
}
trailleft(index+1);
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
scanf("%d",&Node[i].weight);
}
for(int i=0;i<n;i++){
trailleft(i);
trailright(i);
}
for(int i=0;i<n;i++){
sum+=Node[i].milk;
}
printf("%d\n",sum);
return 0;
}