一、题目
You have a statistic of price changes for one product represented as an array of n positive integers p0,p1,…,pn−1, where p0 is the initial price of the product and pi is how the price was increased during the i-th month.
Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase pi to the price at the start of this month (p0+p1+⋯+pi−1).
Your boss said you clearly that the inflation coefficients must not exceed k %, so you decided to increase some values pi in such a way, that all pi remain integers and the inflation coefficients for each month don’t exceed k %.
You know, that the bigger changes — the more obvious cheating. That’s why you need to minimize the total sum of changes.
What’s the minimum total sum of changes you need to make all inflation coefficients not more than k %?
基准价格p0,后面是每个月相对p0上涨的价格pi。
要求:每个月上涨的价格pi比上p0 ~ pi-1 价格之和小于k%
变化幅度越大越容易被发现,所以要求最小的变化幅度之和sum
二、输入
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains two integers n and k (2≤n≤100; 1≤k≤100) — the length of array p and coefficient k.
The second line of each test case contains n integers p0,p1,…,pn−1 (1≤pi≤1e9) — the array p.
一个整数t,表示测试案例的数量。
每个测试案例的第一行有两个整数n, k(2≤n≤100; 1≤k≤100) 表示数组p的长度和系数k
每个测试案例的第二行有n个整数,p0,p1,…,pn−1 (1≤pi≤1e9)
三、输出
For each test case, print the minimum total sum of changes you need to make all inflation coefficients not more than k %.
对每个测试案例,输出最小的改变之和。
四、样例
input:
2
4 1
20100 1 202 202
3 100
1 1 1
output:
99
0
In the first test case, you can, for example, increase p0 by 50 and p1 by 49 and get array [20150,50,202,202]. Then you get the next inflation coefficients:
50/20150≤1100;
202/(20150+50)≤1/100;
202/(20200+202)≤1/100;
In the second test case, you don’t need to modify array p, since the inflation coefficients are already good:
1/1≤100/100;
1/(1+1)≤100/100;
五、思路 + 代码
题意用一个表达式表示:pi* 100 <= (p0 + p1 + … + pi-1 ) * k
就是pi 和截止到pi-1的前缀和之间的关系。
如果某个pi不满足不等式,那就要通过增加(p0 + p1 + … + pi-1 )来使不等式成立。(题目规定了只能增加)
既然p0这一项在所有(p0 + p1 + … + pi-1 )(0<i<n)中都存在,那我直接增加p0不就完事了?
那要增加多少呢?取所有[pi* 100] - [(p0 + p1 + … + pi-1 ) * k] 为非负数的值,其中最大的那个就是需要增加的总值。别忘了,pi每增加1,不等式就增加k,所以实际答案是总值 / k , 别忘了向上取整。
代码如下:
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<iomanip>
using namespace std;
typedef long long ll;
ll Max(ll a, ll b) {
return a > b ? a : b;
}
ll s[110];
ll p[110];
int main() {
int t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> p[i];
s[i] = p[i];
if (i > 0) s[i] += s[i - 1];
}
ll ans = 0;
for (int i = 1; i < n; i++) {
ll tmp = 100 * p[i] - s[i - 1] * k;
if (tmp > 0) ans = Max(ans, tmp);
}
cout << ans / k + (ans % k != 0) << endl;
}
return 0;
}