-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk402.java
174 lines (148 loc) · 4.64 KB
/
wk402.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class wk402 {
// 哈希
public long countCompleteDayPairs(int[] hours) {
int[] count = new int[24];
for (int hour : hours) {
count[hour % 24]++;
}
long ans = 0;
for (int i = 0; i <= count.length / 2; i++) {
int sub = 24 - i;
if (sub == 24 || sub == 12) {
ans += (long) count[i] * (count[i] - 1) / 2;
} else {
ans += (long) count[i] * (count[sub]);
}
}
return ans;
}
// 排序+dp
static public long maximumTotalDamage(int[] power) {
Arrays.sort(power);
Map<Integer, Long> map = new HashMap<>();
for (int i : power) {
map.put(i, map.getOrDefault(i, 0L) + i);
}
List<Integer> list = new ArrayList<>();
list.add(-100);
for (int i = 0; i < power.length; i++) {
if (i == 0 || power[i] != power[i - 1]) {
list.add(power[i]);
}
}
long[][] dp = new long[list.size()][2];
for (int i = 1; i < list.size(); i++) {
int left = i;
while (left >= 0 && list.get(left) >= list.get(i) - 2) {
left--;
}
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = Math.max(dp[left][1], dp[left][0]) + map.get(list.get(i));
}
return Math.max(dp[list.size() - 1][0], dp[list.size() - 1][1]);
}
public static void main(String[] args) {
wk402 w = new wk402();
w.countOfPeaks(new int[]{4, 9, 4, 10, 7}, new int[][]{
{2, 3, 2}, {2, 1, 3}, {1, 2, 3}}
);
}
//树状数组
public List<Integer> countOfPeaks(int[] nums, int[][] queries) {
FenwickTree f = new FenwickTree(nums.length + 1);
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
help(i, nums, f);
}
for (int i = 0; i < queries.length; i++) {
int[] query = queries[i];
if (query[0] == 1) {
if (query[2] - query[1] < 2) {
ans.add(0);
continue;
}
ans.add(f.query(query[2]) - f.query(query[1] + 1));
} else {
int index = query[1];
nums[index] = query[2];
help(index, nums, f);
help(index + 1, nums, f);
help(index - 1, nums, f);
}
}
return ans;
}
void help(int index, int[] nums, FenwickTree f) {
if (index <= 0 || index >= nums.length - 1) return;
// 之前是峰值元素
if (f.query(index + 1) - f.query(index) == 1) {
//依旧是峰值
if (nums[index] > nums[index - 1] && nums[index] > nums[index + 1]) {
} else {
//不是峰值
f.update(index + 1, -1);
}
} else {
//之前不是
// 现在是了
if (nums[index] > nums[index - 1] && nums[index] > nums[index + 1]) {
f.update(index + 1, 1);
} else {
//现在也不是
}
}
}
//树状数组板子
public class FenwickTree {
/**
* 预处理数组
*/
private int[] tree;
private int len;
public FenwickTree(int n) {
this.len = n;
tree = new int[n + 1];
}
/**
* 单点更新
*
* @param i 原始数组索引 i
* @param delta 变化值 = 更新以后的值 - 原始值
*/
public void update(int i, int delta) {
// 从下到上更新,注意,预处理数组,比原始数组的 len 大 1,故 预处理索引的最大值为 len
while (i <= len) {
tree[i] += delta;
i += lowbit(i);
}
}
//区间更新
void update(int x, int y, int k) {
update(x, k);
update(y + 1, -k);
}
/**
* 查询前缀和
*
* @param i 前缀的最大索引,即查询区间 [0, i] 的所有元素之和
*/
public int query(int i) {
// 从右到左查询
int sum = 0;
while (i > 0) {
sum += tree[i];
i -= lowbit(i);
}
return sum;
}
public int lowbit(int x) {
return x & (-x);
}
}
}