Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 17176a0

Browse files
committed
Changed format of time-traveler answer to one line and updated prob.md
1 parent 3d1540f commit 17176a0

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

competition/time-traveler/prob.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,44 @@ To avoid raising suspicions from ASIC or impacting the market significantly, you
2525

2626
## Input
2727

28-
- The first line contains `T` - the number of testcases
29-
- For the next `T` pairs of lines (each representing a seperate testcase):
30-
- The first line contains an integer `N` - the number of days
31-
- The second line contains `N` integers `price[1], price[2], ..., price[n]` - the stock prices for the next `N` days, seperated by space.
28+
- The first line contains `T` - the number of test cases
29+
- For the next `T` pairs of lines (each representing a separate test case):
30+
- The first line contains an integer `N` - the number of days
31+
- The second line contains `N` integers `price[1], price[2], ..., price[n]` - the stock prices for the next `N` days, separated by space.
3232

3333
## Output
3434

35-
Output `T` lines of number, each contains a single integer representing the maximum profit you can make for each test case.
35+
Output a single line, containing `T` space-separated integers.
36+
Each integer will represent the maximum profit that you can make in each test case,
37+
corresponding in the order by which the test cases were provided.
3638

3739
## Constraints
3840

39-
- `2 <= N <= 3 * 10^5`
40-
- `1 <= price[i] <= 10^6`
41-
- The sum of `N` over all test cases will not exceed `3*10^5`
41+
- `2 <= N <= 3 * 10^5`,
42+
- `1 <= price[i] <= 10^6`,
43+
- The sum of `N` over all test cases will not exceed `3*10^5`.
4244

4345
## Example
4446

45-
#### Input
47+
### Input
4648

49+
In this example, `T` is `1`.
50+
The next two lines are thus part of the one and only test case in this example.
51+
Here, `N` is `9`, followed by a single line with 9 space-separated integers.
4752
```
4853
1
49-
9
54+
9
5055
10 5 4 7 9 12 6 2 10
5156
```
5257

53-
#### Output
58+
### Output
5459

60+
Here, the solution is a single line containing the solution of the one test case we are provided.
5561
```
5662
20
5763
```
5864

59-
#### Explanation
65+
### Explanation
6066

6167
To get the maximum profit, we will make these transactions:
6268
- Buy a share at day `2` at price `5`
@@ -66,4 +72,4 @@ To get the maximum profit, we will make these transactions:
6672
- Buy a share at day `8` at price `2`
6773
- Sell a share at day `9` at price `10`
6874

69-
The total profit is `0 - 5 - 4 + 9 + 12 - 2 + 10 = 20`
75+
The total profit is `0 - 5 - 4 + 9 + 12 - 2 + 10 = 20`.

competition/time-traveler/src/main.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,31 @@ int main(int argc, char* argv[]){
149149
// To retrieve submitted answers, query each line one by one until no more lines are found.
150150
std::vector<long long> submittedAnswers;
151151

152+
std::stringstream ss;
152153
std::string line;
153154
std::getline(std::cin, line);
154-
for (int i = 0; !line.empty(); ++i, std::getline(std::cin, line)){
155+
ss << line << ' ';
156+
157+
std::getline(ss, line, ' ');
158+
while (!line.empty()) {
155159
try {
156160
submittedAnswers.push_back(std::stoll(line));
157161
} catch (const std::invalid_argument& error){
158-
std::cerr << "Line " << (i + 1) << " of your answer is not a valid integer.\n";
162+
std::cerr << "Your solution contains an invalid integer: " << line << "\n";
159163
return 1;
160164
} catch (const std::out_of_range& error){
161-
std::cerr << "Line " << (i + 1) << " contains an invalid 64-bit integer.\n";
165+
std::cerr << "Your solution contains an out-of-range value for 64-bit integers: " << line << "\n";
162166
return 1;
163167
}
168+
std::getline(ss, line, ' ');
164169
}
165170

166171
// Compare the submitted answers with the expected answers.
167172
if (submittedAnswers == answers){
168173
return 0;
169174
}
170175
else if (submittedAnswers.size() != answers.size()){
171-
std::cerr << "Wrong solution format: there should be " << NUM_TESTCASES << " lines.\n";
176+
std::cerr << "There should be " << NUM_TESTCASES << " entries, but got " << submittedAnswers.size() << ".\n";
172177
return 1;
173178
}
174179
else{

0 commit comments

Comments
 (0)