"蔚来杯"2022牛客暑期多校训练营5 K-Headphones
问题描述
One day, NIO's home is out of power. So Nio and his sister, Yasa, wanted to take some headphones from the drawer. In the dark, If they randomly took some headphones. There are N pairs in total. Yasa is very lucky, so she had taken out exactly k distinct pairs of headphones (k×2 headphones and all of them are in pairs). How many headphones NIO should take to make sure that he gets more pairs than his sister, i.e.,k+1 pairs of headphones under the worst case. Assume that there are N pairs of headphones in the drawer, and each pair is different from another. NIO takes one headphone once.
输入格式
The first line has two integers N and k , representing the number of the total number of pairs in the drawer and the number of pairs Yasa had taken.
If it cannot guarantee that NIO will get more pairs of headphones than his sister, output −1.
1≤k≤N≤109
输出格式
Output the number of headphones NIO should get.
样例输入
3 1
样例输出
4
提示
3 pairs of headphones = 6 headphones
4 headphones = 4 headphones
题解
最坏的情况下,剩下n-k对耳机各拿一只,之后每次拿到的都能和前n-k次中的一只凑成一对,要凑出多于k对耳机,则应该再拿k+1次,即总共拿n-k+k+1=n+1次,若拿走k对后,剩下的耳机对数不超过k则剩下的全部拿完也不可能超过k对,此时输出-1
1 #include <cstdio> 2 int n,k; 3 int main() 4 { 5 scanf("%d%d",&n,&k); 6 printf("%d",n-k<=k?-1:n+1); 7 return 0; 8 }