1
+
2
+ /*
3
+ *Implementation 'Flappy Bird' problem from June CodeChef starters
4
+ *
5
+ *Problem name : Flappy Bird
6
+ *Problem code : FLAPPYBD
7
+ *Problem link : https://www.codechef.com/START5C/problems/FLAPPYBD/
8
+ *
9
+ */
10
+
11
+ import java .util .*;
12
+ import java .lang .*;
13
+ import java .io .*;
14
+
15
+ //In this problem we need to tell Chef if he can win the game (get the bird
16
+ //to cross all the obstacles) and the minimum number of clicks required to win in such a case
17
+
18
+ public class FlappyBird
19
+ {
20
+ public static void main ()
21
+ {
22
+ Scanner sc = new Scanner (System .in );
23
+ int t = sc .nextInt ();
24
+ while (t -->0 )
25
+ {
26
+ //Number of obstacles
27
+ int n = sc .nextInt ();
28
+
29
+ //The bird starts at a height H
30
+ int H = sc .nextInt ();
31
+ int position [] = new int [n ];
32
+ int heights [] = new int [n ];
33
+
34
+ //Position of obstacles
35
+ for (int i =0 ;i < n ;i ++)
36
+ position [i ]=sc .nextInt ();
37
+
38
+ //Height of obstacles
39
+ for (int i =0 ; i < n ; i ++)
40
+ heights [i ] = sc .nextInt ();
41
+
42
+ int i = 0 ;
43
+ int pos = 0 ;
44
+ int h = H ;
45
+ int nc = 0 ;
46
+ boolean win = true ;
47
+ int rem ;
48
+ int ex ;
49
+ int ans = 0 ;
50
+
51
+ //For each integer i (0≤i<xN), Chef has the option to click once when
52
+ //the bird is at x=i. Let's denote the bird's height (y-coordinate) at
53
+ //that point by j. If he clicks, the bird moves to x=i+1 and y=j+1.
54
+ //Otherwise, the bird moves to x=i+1 and y=j−1.
55
+ while (i < n )
56
+ {
57
+ int dist = position [i ] - pos ;
58
+ h -= dist ;
59
+ nc += dist ;
60
+ if (h <= heights [i ])
61
+ {
62
+ rem = heights [i ]+1 -h ;
63
+ if (2 * nc < rem )
64
+ {
65
+ //print −1 if it is impossible to cross all the obstacles
66
+ System .out .println ("-1" );
67
+ win = false ;
68
+ break ;
69
+ }
70
+ else
71
+ {
72
+ ex = rem /2 + rem %2 ;
73
+ nc -= ex ;
74
+
75
+ //the minimum number of clicks required to cross all the obstacles successfully
76
+ ans += ex ;
77
+ h = h + 2 * ex ;
78
+ pos = position [i ];
79
+ i ++;
80
+ continue ;
81
+ }
82
+ }
83
+ //the bird crosses the ith obstacle successfully if the bird's height at x=position[i] is strictly greater than heights[i]
84
+ else
85
+ {
86
+ pos = position [i ];
87
+ i ++;
88
+ }
89
+ }
90
+
91
+ //printing the minimum number of clicks required to cross all the obstacles successfully
92
+ if (win )
93
+ System .out .println (ans );
94
+ }
95
+ }
96
+ }
97
+
98
+ /*
99
+ * Example Input -
100
+
101
+ 3
102
+ 1 0
103
+ 2
104
+ 1
105
+ 2 1
106
+ 1 3
107
+ 1 1
108
+ 5 10
109
+ 1 2 3 4 5
110
+ 10 11 12 13 15
111
+
112
+ Example Output -
113
+
114
+ 2
115
+ 2
116
+ -1
117
+
118
+ */
0 commit comments