Skip to content

Commit 71b28d0

Browse files
committed
srm 587 div 1 250
1 parent 48798b8 commit 71b28d0

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

JumpFurther.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <vector>
2+
#include <string>
3+
using namespace std;
4+
5+
class JumpFurther {
6+
public:
7+
int furthest(int N,
8+
int badStep) {
9+
10+
int first = 1, t = 0;
11+
for(int i = 0; i <= N; i++){
12+
t += i;
13+
if(t == badStep) first = 0;
14+
}
15+
if(first) return t;
16+
return t-1;
17+
}
18+
};

JumpFurther.html

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<html>
2+
<head>
3+
<title>JumpFurther</title>
4+
</head>
5+
<body>
6+
<h1><a href="http://community.topcoder.com/tc?module=ProblemDetail&amp;rd=15699&amp;pm=12300">JumpFurther</a></h1>
7+
8+
<p><em>Single Round Match 587 Round 1 - Division I, Level One</em></p>
9+
10+
<h2>Statement</h2>
11+
12+
<p>Little Fox Jiro is standing at the bottom of a long flight of stairs.
13+
The bottom of the stairs has number 0, the bottommost step has number 1, the next step has number 2, and so on.
14+
The staircase is so long that Jiro is guaranteed not to reach its top.</p>
15+
16+
<p>Jiro will now perform <em>N</em> consecutive actions.
17+
The actions are numbered 1 through <em>N</em>, in order.
18+
When performing action X, Jiro chooses between two options: either he does nothing, or he jumps exactly X steps up the stairs.
19+
In other words, if Jiro starts performing action X standing on step Y, he will end it either on step Y, or on step Y+X.</p>
20+
21+
<p>For example, if <em>N</em>=3, Jiro will make three consecutive choices: whether or not to jump 1 step upwards, 2 steps upwards, and then 3 steps upwards.</p>
22+
23+
<p>One of the steps is broken.
24+
The number of this step is <em>badStep</em>.
25+
Jiro cannot jump onto this step.</p>
26+
27+
<p>You are given the ints <em>N</em> and <em>badStep</em>.
28+
Compute and return the number of the topmost step that can be reached by Jiro.</p>
29+
30+
<h2>Definitions</h2>
31+
32+
<ul>
33+
<li><em>Class</em>: <code>JumpFurther</code></li>
34+
<li><em>Method</em>: <code>furthest</code></li>
35+
<li><em>Parameters</em>: <code>int, int</code></li>
36+
<li><em>Returns</em>: <code>int</code></li>
37+
<li><em>Method signature</em>: <code>int furthest(int N, int badStep)</code></li>
38+
</ul>
39+
40+
41+
<h2>Constraints</h2>
42+
43+
<ul>
44+
<li><em>N</em> will be between 1 and 2,000, inclusive.</li>
45+
<li><em>badStep</em> will be between 1 and 4,000,000, inclusive.</li>
46+
</ul>
47+
48+
49+
<h2>Examples</h2>
50+
51+
<h3>Example 1</h3>
52+
53+
<h4>Input</h4>
54+
55+
<p><c>2,<br />2</c></p>
56+
57+
<h4>Output</h4>
58+
59+
<p><c>3</c></p>
60+
61+
<h4>Reason</h4>
62+
63+
<p>The optimal strategy is to jump upwards twice: from step 0 to step 1, and then from step 1 to step 3. This trajectory avoids the broken step.</p>
64+
65+
<h3>Example 2</h3>
66+
67+
<h4>Input</h4>
68+
69+
<p><c>2,<br />1</c></p>
70+
71+
<h4>Output</h4>
72+
73+
<p><c>2</c></p>
74+
75+
<h4>Reason</h4>
76+
77+
<p>In this case step 1 is broken, so Jiro cannot jump upwards as his first action. The optimal strategy is to first stay on step 0, and then to jump from step 0 to step 2.</p>
78+
79+
<h3>Example 3</h3>
80+
81+
<h4>Input</h4>
82+
83+
<p><c>3,<br />3</c></p>
84+
85+
<h4>Output</h4>
86+
87+
<p><c>5</c></p>
88+
89+
<h3>Example 4</h3>
90+
91+
<h4>Input</h4>
92+
93+
<p><c>1313,<br />5858</c></p>
94+
95+
<h4>Output</h4>
96+
97+
<p><c>862641</c></p>
98+
99+
<h3>Example 5</h3>
100+
101+
<h4>Input</h4>
102+
103+
<p><c>1,<br />757065</c></p>
104+
105+
<h4>Output</h4>
106+
107+
<p><c>1</c></p>
108+
109+
</body>
110+
</html>

0 commit comments

Comments
 (0)