[CodeForces] [651A] Joysticks

Problem Description

Loading...

Problem Analysis

The gist: two joysticks and one charger
Each minute you can charge only one: the charged one gains +1 charge, the other loses 2
Note that
when one of them has 1 charge, it must be charged
otherwise the game ends
Of course, the game also ends when either charge reaches 0

So we can just simulate it
and just handle the special cases

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
int main() {
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF) {
		int cnt=0;
		while(n&&m) {
			if(n==1&&m>1) {
				n++;
				m-=2;
				cnt++;
			} else if(m==1&&n>1) {
				m++;
				n-=2;
				cnt++;
			} else if(n==1&&m==1) {
				n=0;
				m=0;
			} else {
				if(n>m) {
					int t=(n-m)/2;
					if((n-m)&1)
						t++;
					n-=2*t;
					m+=t;
					cnt+=t;
				} else if(m>n) {
					int t=(m-n)/2;
					if((m-n)&1)
						t++;
					m-=2*t;
					n+=t;
					cnt+=t;
				} else {
					n-=2;
					m++;
					cnt++;
				}
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}

Problem link:【CodeForces】[651A]Joysticks

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...