本场比赛其他题目题解
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 1≤t≤104) — 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 1≤a,b,c≤108; 0 ≤ m ≤ 1 0 8 0 \le m \le 10^8 0≤m≤108).
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} mmin≤m≤mmax,那么输出 “YES”,否则输出 “NO”。
所以转化为两个问题:
- 如何求极限
- 如何证明 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 AA…AABB…BBCC…CC 即可,所以 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,a−1)+max(0,b−1)+max(0,c−1)
m m i n m_{min} mmin 比较难想一点,我们不妨设 a ≤ b ≤ c a \le b \le c a≤b≤c (谁大谁小无所谓,无非就是轮换一下)。那么我们想象一下,如果 a > b + c + 1 a \gt b+c+1 a>b+c+1 ,那么根>据鹊巢原理, m m i n ≤ 1 m_{min} \le 1 mmin≤1。所以一个比较容易想的是: 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
AA…AABB…BBCC…CC 及其类似形式。也即一定
存在
(
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+1…j−1],那么我们不妨将
a
[
i
+
1
…
j
−
1
]
a[i+1\dots j-1]
a[i+1…j−1] 挪到
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;
}