Skip to content

Commit bc15c12

Browse files
committed
Add contest/2020-fall
1 parent ed5bbf4 commit bc15c12

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

contest/2020-fall/2.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 魔术排列
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
bool isMagic(vector<int>& target) {
7+
int n = target.size(), r = -1;
8+
vector<int> a(n), b(n);
9+
iota(ALL(a), 1);
10+
for (int i = 0; i < n; ) {
11+
int j = i;
12+
for (int k = i+1; k < n; k += 2)
13+
b[j++] = a[k];
14+
for (int k = i; k < n; k += 2)
15+
b[j++] = a[k];
16+
a.swap(b);
17+
for (j = i; j < n && a[j] == target[j]; j++);
18+
if (!i && j) r = j;
19+
if (j < n && j-i != r) return false;
20+
i = j;
21+
}
22+
return true;
23+
}
24+
};

contest/2020-fall/3.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 数字游戏
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
4+
class Solution {
5+
public:
6+
vector<int> numsGame(vector<int>& nums) {
7+
int n = nums.size();
8+
long s0 = nums[0], s1 = 0;
9+
priority_queue q0{less{}, vector{nums[0]}};
10+
priority_queue q1{greater{}, vector<int>{}};
11+
vector ret{0};
12+
FOR(i, 1, n) {
13+
int x = nums[i]-i;
14+
if (x < q0.top()) {
15+
q0.push(x);
16+
s0 += x;
17+
if (q0.size() == q1.size()+2) {
18+
q1.push(q0.top());
19+
s1 += q0.top();
20+
s0 -= q0.top();
21+
q0.pop();
22+
}
23+
} else {
24+
q1.push(x);
25+
s1 += x;
26+
if (q1.size() == q0.size()+1) {
27+
q0.push(q1.top());
28+
s0 += q1.top();
29+
s1 -= q1.top();
30+
q1.pop();
31+
}
32+
}
33+
ret.push_back((i%2 ? s1-s0 : s1-s0+q0.top()) % 1000000007);
34+
}
35+
return ret;
36+
}
37+
};

0 commit comments

Comments
 (0)