什么是抽屉原理:建议看这篇博客,好详细的。
B. Combinatorics Homework
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given four integer values aa, bb, cc and mm.
Check if there exists a string that contains:
- aa letters 'A';
- bb letters 'B';
- cc letters 'C';
- no other letters;
- exactly mm pairs of adjacent equal letters (exactly mm such positions ii that the ii-th letter is equal to the (i+1)(i+1)-th one).
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.
Each of the next tt lines contains the description of the testcase — four integers aa, bb, cc and mm (1≤a,b,c≤1081≤a,b,c≤108; 0≤m≤1080≤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
input
Copy
3 2 2 1 0 1 1 1 1 1 2 3 2output
Copy
YES NO YESNote
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,b个B,c个C,能不能搞出一个字符串使其有m个两两相邻的字母。
maxx=a[1]+a[2]+a[3]-3 是最多的情况。
minn=max(0,a[3]-a[2]-a[1]-1)是最少的。
maxx和minn是两个极端。
刚开始想的很简单,a,b,c,只要大于等于2,就能组成相邻的,然后把相邻的个数++,最后和m比大小,结果第二个点就wa。
//#include<bits/stdc++.h>
//using namespace std;
//int t,a,b,c,m,k;
//int main(){
// cin>>t;
// while(t--){
// cin>>a>>b>>c>>m;
// if(a>=2){
// k++;
// }if(b>=2){
// k++;
// }if(c>=2){
// k++;
// }if(k>=m){
// cout<<"YES"<<endl;
// }else{
// cout<<"NO"<<endl;
// }k=0;
// }
//}
#include<bits/stdc++.h>
using namespace std;
int a[5],i,j,k,m;
int main(){
cin>>k;
while(k--){
cin>>a[1]>>a[2]>>a[3]>>m;
sort(a+1,a+4);
int maxx=a[1]+a[2]+a[3]-3;
int minn=max(0,a[3]-a[2]-a[1]-1);
if(maxx>=m&&minn<=m){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
}