Sunday, 12 April 2015

                                                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.
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.
 ###################################EDITORIAL#########################
  NOTE ANY DICE CAN  HAVE TO SHOW MINIMU 1 AND ANY DICE CAN MAXIMIU SHOW UP TO  (M-N+1)(SINCE IF REST ALL THE DICE SHOW 1 TAH THIS DICE CAN SHOW M-N+1)
  NOTE -- minum is not 1 always  if all dice cant fill requirement then  min=  m-sum+arr[i] .. example    2 11 6 6   in this case min for both dice is 5 . and max in 11-1=10..

  now if the valu of ith dice is <min than ith dice will hide min-1 element  ie 0 ellements if min=1else if   min<= arr[i <=max in this case also dice can have to hoide min-1 ..
but if(arr[i]>max) than ith dice have to hide   min-1+arr[i]-max..................
(try to     test some case than it will be clear )



######################################CODE##############
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define first ff
#define second ss
typedef long long int lli;
 lli arr[1000000];
int  main()
 
 {
 long long int n,m;
  cin>>n>>m;
  lli sum=0;
  
    for(lli i=0;i<n;i++)
     {
       cin>>arr[i];
        sum+=arr[i];
      
     }
     if(n==1)
      {
       if(m<=arr[0]) cout<<arr[0]-1<<endl;
       else cout<<arr[0]<<endl;
      }
      else
      {
       
      
    //  cout<<sum<<endl;
       for(lli i=0;i<n;i++)
        {
           
           lli min;
           if(m-sum+arr[i]<=0) min=1;
           else min=m-sum+arr[i];
           lli  max=m-n+1;
          // lli req=m-max;
           
           if(arr[i]<min) cout<<arr[i]<<" ";
           else if(arr[i]>=min && arr[i]<=max)
            {
             if(min)
               cout<<min-1<<" ";
               
            }
            else
            {
              cout<<min-1+arr[i]-max<<" ";
              
            }
          
        }
}
   
   
 }

No comments:

Post a Comment