CodeForce 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.
InputThe 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.
OutputPrint?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 NoteIn 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個(gè)骰子,每個(gè)骰子有d[i]個(gè)面(點(diǎn)數(shù)為1~d[i]),通過搖n個(gè)骰子得到一個(gè)整數(shù)A。求每個(gè)骰子不可能出現(xiàn)的點(diǎn)數(shù)的個(gè)數(shù),即多少個(gè)點(diǎn)數(shù)不可能出現(xiàn)。
分析:對(duì)于一個(gè)骰子,先求出其余所有篩子能夠得到的最大值之和和最小值之和,然后根據(jù)最大值之和、最小值之和就能求出每個(gè)骰子不可能出現(xiàn)的點(diǎn)數(shù)。 如果其余所有的骰子都取最小值1,則最小值之和為n-1,當(dāng)前骰子應(yīng)取x=A - (n - 1),當(dāng)前骰子的最大取值就是x,若d[i]>x, 則從x+1~d[i]之間的數(shù)都不會(huì)取到,共有d[i]-x個(gè);如果其余所有的骰子都去最大值d[i],則最大值之和為sum - d[i],當(dāng)前骰子的應(yīng)取y=A - (sum - d[i]),最小取值就是y,若y > 0,則從1~y-1之間的數(shù)都不可能取到,共有y-1個(gè);二者加起來就是最終的答案。 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std;typedef long long LL; const int N = 2e5 + 10; LL d[N];int main() {LL n, A;while(cin >> n >> A) {LL sum = 0;for(int i = 0; i < n; i++) {cin >> d[i];sum += d[i];}bool flag = false;for(int i = 0; i < n; i++) {LL ans = 0;LL x = A + 1 - n; //其它骰子都取1,當(dāng)前骰子最大的取值為xif(d[i] > x) ans += (d[i] - x);LL y = A - (sum - d[i]); //其它骰子都取最大值,當(dāng)前骰子的最小取值為yif(y > 0) ans += (y - 1);if(flag) cout << " ";cout << ans;flag = true;}cout << endl;}return 0; }
總結(jié)
以上是生活随笔為你收集整理的CodeForce 534C Polycarpus' Dice (数学推理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 速看!上班后如何做好防护?这9点一定要知
- 下一篇: 2015年年度总结