Polycarpus' Dice CodeForces

来源:互联网 发布:吾爱免流流控源码 编辑:程序博客网 时间:2024/06/04 19:54

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.

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.

Examples

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和A,表示有n个骰子,它们掷出后的点数和为A,接下来有n个数表示每个骰子所能掷出的最大点数(最小点数都是1),然后问每个骰子有多个值是不可能掷出的。

思路
对于每一个骰子,我们可以先求出除它以外剩下其它骰子所能掷出的值的总和的区间,然后如果A减去这个骰子的某个值不在这个区间内,则说明这个值不可能出现,统计个数输出即可。

下面是代码

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<string>#define ll long long intusing namespace std;ll a[200005];ll max(ll a,ll b){    if(a>b) return a;    else return b;}ll min(ll a,ll b){    if(a>b) return b;    else return a;}int main(){    std::ios::sync_with_stdio(false);    int n;    ll A,sum=0,l,r;    cin>>n>>A;    for(int i=1;i<=n;i++)    {        cin>>a[i];        sum+=a[i];     }      l=n-1;     for(int i=1;i<=n;i++)     {        r=sum-a[i];        ll l1,r1;        r1=A-l;        l1=A-r;        l1=max(l1,1);        r1=min(r1,a[i]);        if(l1<=r1)        {            cout<<a[i]-(r1-l1+1)<<' ';         }         else         cout<<a[i]<<' ';     }    return 0;}
0 0