1172 Panda and PP Milk——PAT甲级

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

 solution:
 

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int main()
{
	int n;cin>>n;
	vector<int>a(n+2);
	int sum=0;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		int l=0,r=0;
		for(int j=i-1;j>=1;j--)
		{
			if(a[j]<a[j+1])l++;
			else if(a[j]>a[j+1])break;
		}
		for(int j=i+1;j<=n;j++)
		{
			if(a[j-1]>a[j])r++;
			else if(a[j-1]<a[j])break;
		}
		sum+=200+100*max(l,r);
	}
	cout<<sum;
}

### 关于1172 Panda and PP Milk 的测试点分析 此问题的核心在于模拟分配逻辑并满足特定条件下的约束。以下是对此类题目涉及的关键测试点及其解决方法的详细说明。 #### 1. **输入规模** 输入数据量较大,`n<=1e4`意味着程序需要具备较高的时间效率和空间复杂度控制能力[^2]。因此,在实现过程中应优先考虑线性算法或接近线性的解决方案,以避免超时风险。 #### 2. **边界情况处理** 边界条件往往是此类问题中的易错点之一。例如: - 当仅有单只熊猫 (`n=1`) 或两只熊猫 (`n=2`) 存在时,如何合理分配牛奶使得总需求最小化? 这种情况下无需比较邻居体重差异即可直接得出结论。 - 如果所有熊猫具有相同重量,则每只熊猫都需获得最低限度 (即200ml),因为任何额外增加都会违反“更胖者获得更多”的原则[^1]。 #### 3. **差值敏感度设定** 题目特别指出只有当两头相邻熊猫所获牛奶数量差距超过100毫升时才会被感知到不公允现象的发生 。这意味着即使存在轻微偏差(小于等于100ml),只要不超过这个阈值就不会引发争斗事件发生. #### 4. **数据类型选择** 考虑到最终结果可能会超出标准整数范围(`int`型最大约为2*10^9),所以建议采用更大容量的数据存储形式如C++里的`long long int`, Python则天然支持大整数运算故无须特殊声明. ```python def min_milk_distribution(weights): n = len(weights) dp = [200]*n for i in range(n): if i>0 and weights[i]>weights[i-1]: diff = max(dp[i-1]-dp[i]+1,0) # Ensure heavier gets more by at least one unit considering sensitivity. dp[i]=max(dp[i],diff+dp[i]) if i<n-1 and weights[i]>weights[i+1]: future_diff=max(dp[i+1]-dp[i]+1,0)# Look ahead adjustment based on next element's requirement. dp[i]=max(dp[i],future_diff+dp[i]) return sum(dp) # Example usage: print(min_milk_distribution([80,60,90])) # ``` 上述代码片段展示了动态规划思路来解决问题的方法论框架结构设计过程。 ####
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值