Connection Status:
Competition Arena > BinaryFlips
SRM 443 · 2009-06-23 · by gojira_tc · Greedy, Simple Math
Class Name: BinaryFlips
Return Type: int
Method Name: minimalMoves
Arg Types: (int, int, int)
Problem Statement

Problem Statement

You are playing a game where you initially have A zeros and B ones. Your goal is to end up with all ones. In each move, you must choose exactly K of the numbers and flip their values (zeros change to ones, and vice-versa). You can choose any K numbers each time, regardless of their current values or whether you have flipped them before. Return the minimal number of moves required to win the game, or -1 if it is impossible.

Constraints

  • A will be between 0 and 100,000, inclusive.
  • B will be between 0 and 100,000, inclusive.
  • K will be between 1 and 100,000, inclusive.
Examples
0)
3
0
3
Returns: 1

You initially have 3 zeros and 0 ones, and you must flip 3 numbers in each move. Your only possible move is to flip every number. After the first move, you end up with all ones and win the game.

1)
4
0
3
Returns: 4

This is similar to the previous example, but this time, you have 4 zeros. Here's one minimal sequence of moves that will lead to a win: 0. 0000 (the initial state) 1. 1110 (first three numbers flipped) 2. 1001 (last three numbers flipped) 3. 0100 (first, second and fourth numbers flipped) 4. 1111 (first, third and fourth numbers flipped)

2)
4
1
3
Returns: 2
3)
3
2
5
Returns: -1
4)
100000
100000
578
Returns: 174

Submissions are judged against all 283 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class BinaryFlips with a public method int minimalMoves(int A, int B, int K) · 283 test cases · 2 s / 256 MB per case

Submitting as anonymous