1172 Panda and PP Milk (PAT甲级)

先贴一下柳婼的解法膜拜一下...

1172 Panda and PP Milk – PAT甲级真题 – 柳婼 の blog

24行。思路很简洁。

我写了一大坨.....搞得极其复杂....

代码如下:

#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
// #define debug

struct panda{
    int num = 1;
    int weight, id, amount, indegree = 0;
    panda(int _weight, int _id): weight(_weight), id(_id){}
};
int n, w, tot = 0;
std::vector<panda> vec;
std::queue<panda> q;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d\n", &n);
    for (int i = 0; i < n; ++i){
        scanf("%d", &w);
        if (i && w == vec.back().weight){
            vec.back().num++;
        } else{
            vec.push_back(panda(w, vec.size()));
        }
    }
    for (int i = 0; i < vec.size(); ++i){
        if (i && vec[i].weight > vec[i - 1].weight){
            vec[i].indegree++;
        }
        if (i + 1 < vec.size() && vec[i].weight > vec[i + 1].weight){
            vec[i].indegree++;
        }
        if (!vec[i].indegree){
            q.push(vec[i]);
        }
    }
    while (!q.empty()){
        panda temp = q.front();
        q.pop();
        temp.amount = 200;
        if (temp.id && temp.weight > vec[temp.id - 1].weight){
            temp.amount = vec[temp.id - 1].amount + 100;
        } else if (temp.id && temp.weight < vec[temp.id - 1].weight){
            vec[temp.id - 1].indegree--;
            if (!vec[temp.id - 1].indegree){
                q.push(vec[temp.id - 1]);
            }
        }
        if (temp.id + 1 < vec.size() && temp.weight > vec[temp.id + 1].weight){
            temp.amount = std::max(temp.amount, vec[temp.id + 1].amount + 100);
        } else if (temp.id + 1 < vec.size() && temp.weight < vec[temp.id + 1].weight){
            vec[temp.id + 1].indegree--;
            if (!vec[temp.id + 1].indegree){
                q.push(vec[temp.id + 1]);
            }
        }
        vec[temp.id].amount = temp.amount;
        tot += temp.amount * temp.num;
    }
    printf("%d\n", tot);
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

题目如下:

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值