codefoces 1574 B. Combinatorics Homework

本场比赛其他题目题解

A. Regular Bracket Sequences
B. Combinatorics Homework
C. Slay the Dragon
D. The Strongest Build

B. Combinatorics Homework

开启传送门

题目描述

You are given four integer values a a a, b b b, c c c and m m m.

Check if there exists a string that contains:

  • a a a letters ‘A’;
  • b b b letters ‘B’;
  • c c c letters ‘C’;
  • no other letters;
  • exactly m m m pairs of adjacent equal letters (exactly m m m such positions i i i that the i i i-th letter is equal to the ( i + 1 ) (i+1) (i+1)-th one).

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of testcases.

Each of the next t t t lines contains the description of the testcase — four integers a a a, b b b, c c c and m m m ( 1 ≤ a , b , c ≤ 1 0 8 1 \le a, b, c \le 10^8 1a,b,c108; 0 ≤ m ≤ 1 0 8 0 \le m \le 10^8 0m108).

Output

For each testcase print “YES” if there exists a string that satisfies all the requirements. Print “NO” if there are no such strings.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example

input1
3
2 2 1 0
1 1 1 1
1 2 3 2
output1
YES
NO
YES

Note

In the first testcase strings “ABCAB” or “BCABA” satisfy the requirements. There exist other possible strings.

In the second testcase there’s no way to put adjacent equal letters if there’s no letter that appears at least twice.

In the third testcase string “CABBCC” satisfies the requirements. There exist other possible strings.

题意

多组样例,每次给你四个整数 a a a, b b b, c c c, m m m。让你找到一个字符串 s s s 满足:

  • a a a 个字符 ‘A’;
  • b b b 个字符 ‘B’;
  • c c c 个字符 ‘C’;
  • 没有除上述三个字符外的其他字符;
  • 恰好有 m m m 个相邻的字符一样(也即,恰好有 m m m 个位置 i i i 满足 s [ i ] = = s [ i + 1 ] s[i] == s[i+1] s[i]==s[i+1]

分析

先说结论:找到最小的 m m i n m_{min} mmin 和 最大的 m m a x m_{max} mmax ,如果 m m i n ≤ m ≤ m m a x m_{min} \le m \le m_{max} mminmmmax,那么输出 “YES”,否则输出 “NO”。

所以转化为两个问题:

  1. 如何求极限
  2. 如何证明 m ∈ [ m m i n , m m a x ] m \in [m_{min}, m_{max}] m[mmin,mmax] 就有解。

先来求 1 1 1

m m a x m_{max} mmax 显然比较好求,显然我只需要构成这样的字符串 A A … A A B B … B B C C … C C AA\dots AABB\dots BBCC\dots CC AAAABBBBCCCC 即可,所以 m m a x = m a x ( 0 , a − 1 ) + m a x ( 0 , b − 1 ) + m a x ( 0 , c − 1 ) m_{max} = max(0, a-1)+max(0, b-1) + m ax(0, c-1) mmax=max(0,a1)+max(0,b1)+max(0,c1)

m m i n m_{min} mmin 比较难想一点,我们不妨设 a ≤ b ≤ c a \le b \le c abc (谁大谁小无所谓,无非就是轮换一下)。那么我们想象一下,如果 a > b + c + 1 a \gt b+c+1 a>b+c+1 ,那么根>据鹊巢原理, m m i n ≤ 1 m_{min} \le 1 mmin1。所以一个比较容易想的是: m m i n = m a x ( 0 , a − ( b + c + 1 ) ) m_{min} = max(0, a-(b+c+1)) mmin=max(0,a(b+c+1))

然后证明 2 2 2

从上述的推导中,我们证明了 m m a x m_{max} mmax m m i n m_{min} mmin 是可达的。

也即现在需要证明 ∀   i ∈ [ m m i n , m m a x ) \forall\ i \in[m_{min}, m_{max})  i[mmin,mmax) 如果 m m m 可以等于 i i i ,那么一定可以构造使得 m m m 可以等于 i + 1 i+1 i+1

这显然就是个数学归纳法了。

我们证明了初始条件 m m i n m_{min} mmin 可达,现在假设 i i i 可达,证明 i + 1 i+1 i+1 可达。

我们不妨设 i i i 对应的字符串为 s i s_i si。因为 i ≠ m m a x i \ne m_{max} i=mmax ,所以 s i s_i si 一定不是 A A … A A B B … B B C C … C C AA\dots AABB\dots BBCC\dots CC AAAABBBBCCCC 及其类似形式。也即一定
存在 ( i , j ) (i, j) (i,j) 满足 a [ i ] = = a [ j ] & & a [ i ] ≠ a [ i + 1 … j − 1 ] a[i] == a[j] \&\& a[i] \ne a[i+1\dots j-1] a[i]==a[j]&&a[i]=a[i+1j1],那么我们不妨将 a [ i + 1 … j − 1 ] a[i+1\dots j-1] a[i+1j1] 挪到 a j a_j aj 后面,显然,这样做会造成 m + + m++ m++ 。命题得证。

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0),cin.tie(0);
  int t; cin >> t; while (t--) {
    int a, b, c, m;
    cin >> a >> b >> c >> m;
    int mn = max({c - (a + b + 1),
                  b - (c + a + 1),
                  a - (b + c + 1)}),
        mx = (a - 1) + (b - 1) + (c - 1);
    if (mn <= m && m <= mx) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值