cf 534C. Polycarpus' Dice
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.
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.
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.
2 8
4 4
3 3
1 3
5
4
2 3
2 3
0 1
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个骰子,第I个筛子一共有di面...现在知道这些骰子的点数之和,问对于每一个骰子不能取得值有多少个。
乍一看有点不明觉厉...稍微再想下,求取值范围即可。
先把所有di相加,得到所有骰子点数之和的最大值...然后点数之和的最小值当然就是N
对于每个骰子,将最大值和最小值减去这个骰子的对应数值...然后与总和A进行比较。
注意要开long long !!!
比赛的时候我明明写了typedef。。。结果后面还是忘记了。。。真是悲伤。
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <cstdio> typedef long long LL; const int N=2E5+7; LL n,d[N],ans[N]; LL A,MAX,MIN,TMAX,TMIN; using namespace std; int main() { scanf("%I64d %I64d",&n,&A); memset(ans,0,sizeof(ans)); for ( int i = 1; i <= n ;i ++ ) scanf("%I64d",&d[i]); MAX = 0; MIN = n; for ( int i = 1; i <= n ; i++ ) MAX = MAX + d[i]; for ( int i = 1 ; i <= n ; i++ ) { TMAX = MAX - d[i]; TMIN = MIN - 1; if (d[i]>=(A-TMIN)) { ans[i] =ans[i]+d[i]-(A-TMIN); } if (A>=TMAX+1) { ans[i] =ans[i]+A-TMAX-1; } } for ( int i = 1; i < n ; i++) cout<<ans[i]<<" "; cout<<ans[n]; return 0; }