Codeforces534C: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.

题意:输入n,A,一共有n个色子,他们向上的点数和为A,然后给出n个数,分别代表每个色子最大的掷出点数,要求出每个色子有几个值是不可能掷出的

思路:对于一个色子,我们先求出其他色子能这出的最大值与最小值之和,那么我们就能得到这个色子所可能掷出的区间,然后就得到了不能掷出的个数

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 1000006
#define mod 1000000007
const int INF = 0x3f3f3f3f;

LL n,sum,a[300000],cnt;

int main()
{
    LL i,j,ss;
    w(~scanf("%I64d%I64d",&n,&sum))
    {
        ss = 0;
        up(i,1,n)
        {
            scanf("%I64d",&a[i]);
            ss+=a[i];
        }
        up(i,1,n)
        {
            cnt = 0;
            LL minn = n-1,maxn = ss-a[i];
            LL l = sum-maxn,r = sum-minn;
            if(l>0)
                cnt += l-1;
            if(r<=a[i])
                cnt+=(a[i]-r);
            if(i!=1)
                printf(" ");
            printf("%I64d",cnt);
        }
        printf("\n");
    }

    return 0;
}

Codeforces534C:Polycarpus' Dice

时间: 2024-10-24 15:38:16

Codeforces534C:Polycarpus' Dice的相关文章

C. Polycarpus&#39; Dice

在每个位置讨论一下最大值最小值的取值范围就行 1 #include<cstdio> 2 #include<iostream> 3 #define maxn 200003 4 using namespace std; 5 typedef long long LL; 6 LL a[maxn],b[maxn]; 7 8 int main() 9 { 10 LL n,s; 11 while(scanf("%I64d %I64d",&n,&s)!=EOF)

CodeForces 534C Polycarpus&#39; Dice (数学)

题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子掷不出的数的个数. 析:我们只要考虑两个极端就好,考由其他骰子投出的最大值和最小值,还有自身在最大值和最小值,作一个数学运算就OK了.公式如下: 骰子的最大值-能投的最大值+能投的最小值-1. 代码如下: #include <cstdio> #include <string> #inc

Codeforces 534C Polycarpus&#39; Dice 构造

题意:给你n个筛子,第 i 个筛子有 可以表示范围 1-a[i]的数,给你最后筛子和,问你每个筛子不可能的值有多少个. 解题思路:得到每个筛子的取值范围. 解题代码: 1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月13日 星期一 00时38分58秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #includ

CodeForces 534C - Polycarpus&#39; Dice(思路)

题意:给定n (1 <= n <= 2*10^5) 个骰子,给定每个骰子最大可以掷出的最大数(最小数始终为1),给定所有骰子掷出的点数和A,求每个骰子不可能掷出的点数个数. 考虑最大和最小情况,作差即可(详情见代码注释) #include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream&g

#298 (div.2) C. Polycarpus&#39; Dice

1.题目描述:点击打开链接 2.解题思路:本题是一道数学题,很可惜在比赛时候没有注意到最大数的范围,然后被Hack了,瞬间rating变得不忍直视==.还是耐心总结,好好准备下一场比赛吧.本题要求找每个筛子不可能出现的数字的个数.可以通过确定可能值的边界来解决.假设所有筛子出现的数字之和是tot,那么每个筛子的最大范围是min(A-(n-1),num[i]),即当其他筛子都取1时的情况和筛子i自身的最大值的较小者.同理不难得到最小范围是max(1,A-(tot-num[i])).这样以来,不可能

Codeforces Round #298 (Div. 2)

A - Exam 构造 1 #include <cstdio> 2 3 int ans[5000 + 5]; 4 5 int main() { 6 int n, cnt = 1; 7 scanf("%d", &n); 8 for (int i = 1; i <= n; i++) { 9 if (i&1) ans[i] = cnt; 10 else { 11 ans[i] = n+1-cnt; 12 cnt++; 13 } 14 } 15 ans[0]

20170906

水题 T1 Arpa and a research in Mexican wave CodeForces - 851A 1 #include<cstdio> 2 int n,k,t; 3 int main() 4 { 5 scanf("%d%d%d",&n,&k,&t); 6 if(t>=k&&t<=n) 7 printf("%d",k); 8 else if(t<k) 9 printf(&quo

Codeforces Round #298 (Div. 2) A、B、C题

题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side

ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)

Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A.