[Algorithm] bidding

Source: Internet
Author: User

Bidding

Question: Time Limit: 1000 ms memory limit: 256 MB

Description

Alice and Bob both want to buy diamonds from the same merchant. The merchant has n diamonds in his hand and he will sell them to them. Alice and Bob decide the ownership of the diamond by bidding. The specific process is as follows: the merchant first specifies one of them to start the quotation, and the other two will take turns to make the quotation. The request must be higher than the price quoted by the other party. At any time, if a person is unwilling to make a bid or is unable to afford the price, he or she can revoke the offer, and his opponent will buy the diamond at the last quoted price. Of course, merchants will not sell diamond if they have no money. The first quotation must be at least 1 and only an integer price can be quoted. Alice and Bob are particularly fond of PAN, so they all want to buy more diamonds than the other party. Alice and bob each carry the CA and CB money for the diamond auction. In addition, Alice has a very good private relationship with the merchant, so the merchant always asks Alice to quote first. Who can buy more diamonds when Alice and Bob use the best strategy? Assume that both parties know the amount of cash in the opponent's hand and the number of diamonds that the merchant wants to auction n.

Input

The input file contains multiple groups of test data. The first line is an integer T, which is the number of data groups. Next, the test data of each group is given in sequence. Each group of data is a space-separated integer N, CA, CB, indicating the quantity of diamond and the amount of cash carried by both parties.

Output

For each group of test data, a line "case # X: Y" is output, where X indicates the test data number. The value of Y is {-1, 0, 1 }, -1 indicates that Alice buys less diamond than Bob, 0 indicates that two people can buy the same amount, and 1 indicates that Alice can buy more diamond. All data is numbered from 1 in the read order.

Data range

1 ≤ T ≤ 1000 small data: 0 ≤ n ≤ 10; 0 <ca, CB ≤ 10 big data: 0 ≤ n ≤ 105; 0 <ca, CB ≤ 106


Sample Input
2

43 5

7 4 7

Sample output
Case #1: 0

Case #2: 1

After thinking about this question for a long time, the idea is basically correct. Unfortunately, I finally gave up and did not submit the question. Now I have read others'Code. N indicates the number of stones, A indicates the number of Alice coins, and B indicates the number of Bob coins.

The basic thing is to find the rule. Every time you get a gem, Alice and Bob have to pay at least one coin. The condition for winning is to obtain half of the n s (n/2 + 1). Considering that Alice can obtain a s at the minimum cost, so can Alice win at the end? This depends on the size of N, so we can consider the following three situations based on the size of N:

1. n> 2 * A; that is, N must be at least 2 * A + 1. Naturally, we will think of comparing the sizes of A and B:

When B> A, that is, B is at least a + 1, Bob is bound to win, because even if all the previous a gem is given to Alice, bob can still get at least a + 1 remaining gem;

When B <A, Alice must acquire it, the same as above, because even if all the first B of the gems are given to Bob, Alice can still obtain a gem, which is always more than Bob's;

When B = A, the two bought as many gems as they did.

2. n = 2 * a, consider the same as above:

When B <A, Alice wins;

When B> = 2 * A + 2, Bob wins because Alice has a gem, alice's optimal strategy is to produce at least one coin each time. Therefore, Bob must use two coins each time to obtain a gemstone. Therefore, B must be at least 2 * (a + 1) bob wins;

When B> = A & B <2 * A + 2, the two finally obtain the same number of gems;

3. When n is less than 2 * a, the situation is the most complicated. I was stunned by the details here. There are two situations based on N's parity:

When N is an odd number, the number of gems required for winning is Winner = n/2 + 1. Because a> n/2, the number of A is at least equal to winner, alice's optimal strategy for winning (that is, getting winner's gem) is to use d = A/WINNER coins each time to get a gem, bob wins when he pays one more price than Alice (that is, D + 1)So Bob must at least (D + 1) * winner to win. So when B> = (D + 1) * winner, Bob will win. Otherwise, Alice will win.

When N is an even number, the number of winning gems winner2 = n/2 + 1, the draw (that is, the two get the same amount of gems) requires the number of stones winner1 = n/2, similarly, to get the winner2 gem Alice pays D2 = A/winner2 each time, to get the winner1 gem Alice pays d1 = A/winner1 each time, bob wins every time he pays (d1 + 1) to get winner2 gems. That is, when B> = (d1 + 1) * winner2, Bob wins; when Alice wins, B <(d2 + 1) * winner1 means Bob cannot get at least half of the gemstone. When B> = (d2 + 1) * When winner1 & B <(d1 + 1) * winner2, the two obtain the same number of gems.

After the problem is clearly analyzed, the code is very easy to write, and the small data and big data can be smoothly AC!

 

 1 # Include <iostream> 2   Using   Namespace  STD;  3   Int  Main (){  4       Int  T;  5 Cin>T;  6       Int  N, A, B;  7       Int K = 0  ;  8       While (T -- ){  9 Cin> N;  10 Cin> A;  11 Cin> B;  12           Int  Result;  13 Cout < "  Case #  "  ;  14 Cout <++ K;  15 Cout < "  :  "  ;  16           If (N> 2 * A ){  17               If (B> A)  18 Result =- 1  ;  19               If (B = A)  20 Result = 0  ;  21               If (B <A)  22 Result = 1  ;  23 Cout <result < Endl;  24               Continue  ;  25   }  26           If (N = 2 * A ){  27               If (B < A)  28 Result = 1  ;  29               If (B> = A & B <= 2 * A +1  )  30 Result = 0  ;  31               If (B> 2 * A + 1  )  32 Result =- 1  ;  33 Cout <result < Endl;  34               Continue  ;  35   }  36           If (N < 2 * A ){  37              If (N % 2 = 0  ){  38                   Int Temp1 = N/ 2  ;  39                   Int Temp2 = N/ 2 + 1  ;  40                   Int D1 =/ Temp1;  41                   Int D2 =/ Temp2;  42                   Int Smaller = (d2 + 1 )* Temp1;  43                   Int Bigger = (d1 +1 )* Temp2;  44                   If (B < Smaller)  45 Result = 1  ;  46                   If (B> = Bigger)  47 Result =- 1  ;  48                   If (B> = smaller & B < Bigger)  49 Result = 0  ;  50   }  51               Else {  52                   Int Temp = N/ 2 + 1  ;  53                   Int D =/ Temp;  54                   Int Bigger = (D + 1 )* Temp;  55                   If (B> = Bigger)  56 Result =- 1  ;  57                   Else   58 Result = 1  ;  59  }  60 Cout <result < Endl;  61   }  62   }  63       Return   0  ;  64 }

From: http://www.cnblogs.com/chasuner/archive/2013/04/13/3019536.html

Other: http://blog.csdn.net/blue_jjw/article/details/8797146

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.