本文主要是介绍CF1279 A. New Year Garland,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Polycarp is sad — New Year is coming in few days but there is still no snow in his city. To bring himself New Year mood, he decided to decorate his house with some garlands.
The local store introduced a new service this year, called “Build your own garland”. So you can buy some red, green and blue lamps, provide them and the store workers will solder a single garland of them. The resulting garland will have all the lamps you provided put in a line. Moreover, no pair of lamps of the same color will be adjacent to each other in this garland!
For example, if you provide 3 red, 3 green and 3 blue lamps, the resulting garland can look like this: “RGBRBGBGR” (“RGB” being the red, green and blue color, respectively). Note that it’s ok to have lamps of the same color on the ends of the garland.
However, if you provide, say, 1 red, 10 green and 2 blue lamps then the store workers won’t be able to build any garland of them. Any garland consisting of these lamps will have at least one pair of lamps of the same color adjacent to each other. Note that the store workers should use all the lamps you provided.
So Polycarp has bought some sets of lamps and now he wants to know if the store workers can build a garland from each of them.
Input
The first line contains a single integer 𝑡 (1≤𝑡≤100) — the number of sets of lamps Polycarp has bought.
Each of the next 𝑡 lines contains three integers 𝑟, 𝑔 and 𝑏 (1≤𝑟,𝑔,𝑏≤109) — the number of red, green and blue lamps in the set, respectively.
Output
Print 𝑡 lines — for each set of lamps print “Yes” if the store workers can build a garland from them and “No” otherwise.
Example
inputCopy
3
3 3 3
1 10 2
2 1 1
outputCopy
Yes
No
Yes
Note
The first two sets are desribed in the statement.
The third set produces garland “RBRG”, for example.
题意:
给出A,B,C的数目,求是否存在排列方式使得没有相邻两个相同。
思路:
我写的时候思路没到位,想的是先放三个的再放两个的,这是假算法。
贪心的算,肯定要先把最大的那个用完,假设是A ≤ B ≤ C。
C最大可以等于A + B + 1,如果C > A + B + 1,那肯定是放不下的(CACBCXCXC…)
如果C ≤ A + B + 1. 取完C后一定可以使得A = B或者A + 1 = B,那么剩下的就直接放就好了。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>using namespace std;int a[10];
int main()
{int T;scanf("%d",&T);while(T--){scanf("%d%d%d",&a[1],&a[2],&a[3]);sort(a + 1,a + 1 + 3);if(a[1] + a[2] + 1 >= a[3]){printf("Yes\n");}else{printf("No\n");}}return 0;
}
这篇关于CF1279 A. New Year Garland的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!