Skip to content

Commit 236b2eb

Browse files
committed
202209031856
1 parent eb59c20 commit 236b2eb

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

independent/202209031856/0.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[2,1,2,3,2]
2+
[9,6,2,1,7]
3+
[1,3,0,5,2]
4+
1

independent/202209031856/1.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[2,1,1,1,2]
2+
[9,6,2,1,107]
3+
[1,3,0,5,2]
4+
1

independent/202209031856/2.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[2,1,2,2]
2+
[9,6,2,1]
3+
[1,3,1,5]
4+
1

independent/202209031856/3.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[1,2,1,1]
2+
[1,8,16,128]
3+
[2,4,32,64]
4+
1

independent/202209031856/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
一个人每天会被给予一个任务,这个任务需要在城市c完成,如果他当前不在该城市,获得的收益为b,在该城市则获得收益为a,给定a b c的数组,初始城市在k,每天可以决定接收或放弃该天的任务。求最大收益。数组长度或城市个数n最大为50000。

independent/202209031856/main.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInputTemplate.hpp"
7+
#include "solution.cpp"
8+
using namespace std;
9+
10+
typedef vector<int> vi;
11+
typedef vector<vector<int>> vvi;
12+
13+
int main()
14+
{
15+
LeetCodeInput li("3.in");
16+
17+
auto l0 = li.get<vi>(0);
18+
auto l1 = li.get<vi>(1);
19+
auto l2 = li.get<vi>(2);
20+
auto l3 = li.get<int>(3);
21+
22+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
23+
Solution solution;
24+
auto output = solution.solution(l0, l1, l2, l3);
25+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
26+
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
27+
28+
cout << output << endl;
29+
30+
// for (int i: output){
31+
// cout << i << ' ';
32+
// }
33+
34+
// for (auto i: output){
35+
// for (int j: i){
36+
// cout << j << ' ';
37+
// }
38+
// cout << '\n';
39+
// }
40+
41+
return 0;
42+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime:
7+
Memory Usage:
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution {
19+
public:
20+
int solution(vi jobs, vi a, vi b, int k) {
21+
uint16_t n = size(jobs);
22+
/// score_by_pos[pos] = score
23+
unordered_map<uint16_t, int> score_by_pos;
24+
score_by_pos.emplace(k, 0);
25+
26+
int max_new_score = 0;
27+
int second_max_new_score = 0;
28+
int last_pos = k;
29+
bool last_lazy;
30+
for (int day=0; day<n; ++day){
31+
32+
if (jobs[day] == last_pos){
33+
max_new_score = second_max_new_score + b[day];
34+
} else {
35+
second_max_new_score = max_new_score;
36+
max_new_score += b[day];
37+
}
38+
39+
auto it = score_by_pos.find(jobs[day]);
40+
if (it != score_by_pos.end()){
41+
int lazy_new_score = (*it).second + a[day];
42+
max_new_score = max(max_new_score, lazy_new_score);
43+
}
44+
45+
last_pos = jobs[day];
46+
score_by_pos[jobs[day]] = max_new_score;
47+
}
48+
49+
return max_new_score;
50+
}
51+
};
52+
53+
const static auto initialize = [] {
54+
std::ios::sync_with_stdio(false);
55+
std::cin.tie(nullptr);
56+
std::cout.tie(nullptr);
57+
return nullptr;
58+
}();

0 commit comments

Comments
 (0)