PAT(甲級)2020年秋季考試 7-1 Panda and PP Milk

7-1 Panda and PP Milk (20分)

panda.jpg

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.測試

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.this

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.spa

Sample Input:

10
180 160 100 150 145 142 138 138 138 140`

Sample Output:

3000

Hint:

The distribution of milk is the following:code

400 300 200 500 400 300 200 200 200 300

題目限制:

image.png

題目大意:

給定N只熊貓的體重,每一隻熊貓最少喝奶200ml,體重大的喝奶多,相差100ml熊貓就會察覺到和其餘熊貓奶量的差距,如今要求你給出全部熊貓所需的最低奶量。blog

算法思路:

本人認爲這是要求根據體重的分佈,給出奶量變化趨勢和體重同樣的分佈,求解方法就是,先對第一隻熊貓的奶量設置爲最小,而後後面的熊貓體重若是大於它,就加100,相等,則和它同樣,不然就直接出現降低趨勢直接賦值爲200,可是因爲這樣有可能會致使前面的奶量分佈出現和體重分佈不一致的狀況,那麼就須要進行調整,主要調整兩種類型,第一種就是當前熊貓體重比後面的重,可是奶量卻和它相等,就須要給它加100(不會出現比後面少的可能,由於初始狀態下
,前面的熊貓的奶量原先是比後面多的,是由於後面的熊貓進行了調整,加了100,纔會致使和它相等,在原先存在差距的狀況只會相等,不會超過) 。第二種類型就是當前熊貓體重和後面的同樣,可是喝的奶量卻比後面的少(一樣是由於後面的熊貓調整過了,並且也不存在體重同樣喝的奶會同樣的狀況,由於到達當前的j的時候,j+1必定調整過),那麼對於第一次出現j+1爲波峯的時候,也就是當期出現降低趨勢,而且也沒有違背右邊的熊貓的奶量關係的時候就調整完畢了。
最後統計全部奶量而後輸出便可。ci

注意點:

  • 一、仔細揣摩那兩個須要調整的類型和等號的取值,以前讓我最後一個測試點沒有過。

提交結果:

image.png

AC代碼:

#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;
}
相關文章
相關標籤/搜索