本文主要是介绍CodeForces 534C Polycarpus' Dice 【数学】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C. Polycarpus’ Dice
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp has n dice d1, d2, …, dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn’t see which dice showed what number, she knows only the sum A and the values d1, d2, …, dn. However, she finds it enough to make a series of statements of the following type: dice i couldn’t show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn’t show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).
For each dice find the number of values for which it can be guaranteed that the dice couldn’t show these values if the sum of the shown values is A.
Input
The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + … + dn.
The second line contains n integers d1, d2, …, dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show.
Output
Print n integers b1, b2, …, bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn’t show them.
Sample test(s)
input
2 8
4 4
output
3 3
input
1 3
5
output
4
input
2 3
2 3
output
0 1
Note
In the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn’t show values 1, 2 or 3.
In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn’t show 1, 2, 4 or 5.
In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That’s why the first dice doesn’t have any values it couldn’t show and the second dice couldn’t show 3.
题意:有n个筛子,每个筛子都有di个面,可以表示1~di,现在给你一个数A(表示某一状态的n个筛子的表示的数的和,例如有两个筛子(d1=2 ,d2 = 3)某一状态为(1, 2),则A = 3),问n个筛子的和为A的时候,每个筛子不可能会出现面的数目。
例如:
2 3
1 100
那么从4~100都不可能出现使这两个筛子的和为3;
思路:
找出每个筛子满足条件最大的和最小值找出来即可。
找出小于最小值的数目 就是让其他的筛子取最大值,再让他们相加得一和,再用A减去上面的结果就是满足条件的最小的值,之后再减1(因为还满足。。。)
找出大于最大值的数目 用A减去n(n个筛子的最小值),之后再用di减去上面的结果,之后再减1(因为还满足。。。)
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
const int M = 2e5+5;LL n, a, d[M], m = 0;int main(){while(scanf("%I64d%I64d", &n, &a) == 2){LL sum = 0;for(int i = 0; i < n; ++ i){scanf("%I64d", &d[i]);sum += d[i];}for(int i = 0; i < n; ++ i){LL temp1 = d[i]-(a-n)-1;LL temp2 = a-(sum-d[i])-1;printf("%I64d%c", max(temp1, m)+max(temp2, m), i+1 == n?'\n':' ');}}return 0;
}
这篇关于CodeForces 534C Polycarpus' Dice 【数学】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!