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.ide
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.测试
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.this
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.spa
10 180 160 100 150 145 142 138 138 138 140`
3000
The distribution of milk is the following:code
400 300 200 500 400 300 200 200 200 300
给定N只熊猫的体重,每一只熊猫最少喝奶200ml,体重大的喝奶多,相差100ml熊猫就会察觉到和其余熊猫奶量的差距,如今要求你给出全部熊猫所需的最低奶量。blog
本人认为这是要求根据体重的分布,给出奶量变化趋势和体重同样的分布,求解方法就是,先对第一只熊猫的奶量设置为最小,而后后面的熊猫体重若是大于它,就加100,相等,则和它同样,不然就直接出现降低趋势直接赋值为200,可是因为这样有可能会致使前面的奶量分布出现和体重分布不一致的状况,那么就须要进行调整,主要调整两种类型,第一种就是当前熊猫体重比后面的重,可是奶量却和它相等,就须要给它加100(不会出现比后面少的可能,由于初始状态下
,前面的熊猫的奶量原先是比后面多的,是由于后面的熊猫进行了调整,加了100,才会致使和它相等,在原先存在差距的状况只会相等,不会超过) 。第二种类型就是当前熊猫体重和后面的同样,可是喝的奶量却比后面的少(一样是由于后面的熊猫调整过了,并且也不存在体重同样喝的奶会同样的状况,由于到达当前的j的时候,j+1必定调整过),那么对于第一次出现j+1为波峰的时候,也就是当期出现降低趋势,而且也没有违背右边的熊猫的奶量关系的时候就调整完毕了。
最后统计全部奶量而后输出便可。ci
#include<cstdio> using namespace std; /* 题目大意:给定N只熊猫的体重,每一只熊猫最少喝奶200ml,体重大的喝奶多,相差100ml熊猫就会察觉到和其余熊猫 奶量的差距,如今要求你给出全部熊猫所需的最低奶量。 算法思路:本人认为这是要求根据体重的分布,给出奶量变化趋势和体重同样的分布,求解方法就是,先对第一只熊猫 的奶量设置为最小,而后后面的熊猫体重若是大于它,就加100,相等,则和它同样,不然就直接出现降低趋势直接赋值 为200,可是因为这样有可能会致使前面的奶量分布出现和体重分布不一致的状况,那么就须要进行调整,主要调整两种 类型,第一种就是当前熊猫体重比后面的重,可是奶量却和它相等,就须要给它加100(不会出现比后面少的可能,由于初始状态下 ,前面的熊猫的奶量原先是比后面多的,是由于后面的熊猫进行了调整,加了100,才会致使和它相等,在原先存在差距的状况只会相等,不会超过) 。 第二种类型就是当前熊猫体重和后面的同样,可是喝的奶量却比后面的少(一样是由于后面的熊猫调整过了,并且也不存在体重同样喝的奶会同样的状况,由于 到达当前的j的时候,j+1必定调整过),那么对于第一次出现j+1为波峰的时候,也就是当期出现降低趋势,而且也没有违背右边的熊猫的奶量关系的时候就调整完毕了。 最后统计全部奶量而后输出便可。 */ int main(){ int N; scanf("%d",&N); int weight[N]; for(int i=0;i<N;++i){ scanf("%d",&weight[i]); } int milk[N]; milk[0] = 200;//初始奶量为200 for(int i=1;i<N;++i){ if(weight[i]>weight[i-1]){ milk[i] = milk[i-1] + 100; }else if(weight[i]==weight[i-1]){ milk[i] = milk[i-1]; }else{ // 前面的奶量为200了,而且这里仍是降低趋势 // 向前搜寻波峰,而且每个奶量加100 milk[i] = 200; for(int j=i-1;j>=0;--j){// 每个须要调整奶量的熊猫 if(weight[j+1]<weight[j]&& milk[j+1]==milk[j]){ // 当前熊猫比后面的重,可是喝的奶和它同样,须要调整 milk[j] = milk[j] + 100;//每个奶量加100 }else if(weight[j+1]==weight[j]&&milk[j+1]>milk[j]) { // 当前的熊猫和后面的同样重,可是喝的奶比它少,须要调整 milk[j] = milk[j] + 100;//每个奶量加100 }else{ // j+1为波峰,直接退出便可,无需再调整,由于调整后就不在是最低奶量了 break; } } } } int total = 0; for(int i=0;i<N;++i){ total += milk[i]; } printf("%d",total); return 0; }