1
+ package weekly ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .Arrays ;
5
+ import java .util .HashMap ;
6
+ import java .util .List ;
7
+ import java .util .Map ;
8
+
9
+ public class wk387 {
10
+
11
+ //模拟
12
+ /* public int[] resultArray(int[] nums) {
13
+ List<Integer> arr1 = new ArrayList<>();
14
+ List<Integer> arr2 = new ArrayList<>();
15
+ arr1.add(nums[0]);
16
+ arr2.add(nums[1]);
17
+ for (int i = 2; i < nums.length; i++) {
18
+ int num = nums[i];
19
+ if (arr1.get(arr1.size() - 1) > arr2.get(arr2.size() - 1)) {
20
+ arr1.add(num);
21
+ } else {
22
+ arr2.add(num);
23
+ }
24
+
25
+ }
26
+ int[] res = new int[nums.length];
27
+ int i = 0;
28
+ for (Integer integer : arr1) {
29
+ res[i++] = integer;
30
+ }
31
+ for (Integer integer : arr2) {
32
+ res[i++] = integer;
33
+ }
34
+ return res;
35
+
36
+ }*/
37
+
38
+ //二维前缀和
39
+ public int countSubmatrices (int [][] grid , int k ) {
40
+ int [][] dp = new int [grid .length + 1 ][grid [0 ].length + 1 ];
41
+ int ans = 0 ;
42
+ for (int i = 0 ; i < grid .length ; i ++) {
43
+ int pre = 0 ;
44
+ for (int j = 0 ; j < grid [0 ].length ; j ++) {
45
+ pre += grid [i ][j ];
46
+ dp [i + 1 ][j + 1 ] = dp [i ][j + 1 ] + pre ;
47
+ if (dp [i + 1 ][j + 1 ] <= k ) {
48
+ ans ++;
49
+ }
50
+ }
51
+ }
52
+ return ans ;
53
+ }
54
+
55
+ //模拟
56
+ public int minimumOperationsToWriteY (int [][] grid ) {
57
+ int [] all = new int [3 ];
58
+ for (int [] ints : grid ) {
59
+ for (int anInt : ints ) {
60
+ all [anInt ]++;
61
+ }
62
+ }
63
+ int [] Y = check (grid );
64
+ int sumY = 0 ;
65
+ for (int i : Y ) {
66
+ sumY += i ;
67
+ }
68
+ int sumLeft = grid .length * grid [0 ].length - sumY ;
69
+ int ans = Integer .MAX_VALUE ;
70
+ int [] left = new int [3 ];
71
+ for (int i = 0 ; i < left .length ; i ++) {
72
+ left [i ] = all [i ] - Y [i ];
73
+ }
74
+ for (int i = 0 ; i < Y .length ; i ++) {
75
+ for (int j = 0 ; j < left .length ; j ++) {
76
+ if (i == j ) continue ;
77
+ //把Y都变成i
78
+ int t = sumY - Y [i ];
79
+ //把非Y都变成j
80
+ t += sumLeft - left [j ];
81
+ ans = Math .min (t , ans );
82
+ }
83
+ }
84
+ return ans ;
85
+ }
86
+
87
+ int [] check (int [][] grid ) {
88
+ int [] ans = new int [3 ];
89
+ int n = grid .length ;
90
+ for (int i = 0 ; i <= n / 2 ; i ++) {
91
+ ans [grid [i ][i ]]++;
92
+ }
93
+
94
+ for (int i = 0 ; i < n / 2 ; i ++) {
95
+ ans [grid [i ][n - i - 1 ]]++;
96
+ }
97
+
98
+ for (int i = n / 2 + 1 ; i < n ; i ++) {
99
+ ans [grid [i ][n / 2 ]]++;
100
+ }
101
+ return ans ;
102
+ }
103
+
104
+ public class FenwickTree {
105
+
106
+ /**
107
+ * 预处理数组
108
+ */
109
+ private int [] tree ;
110
+ private int len ;
111
+ int all =0 ;
112
+
113
+ public FenwickTree (int n ) {
114
+ this .len = n ;
115
+ tree = new int [n + 1 ];
116
+ }
117
+
118
+ /**
119
+ * 单点更新
120
+ *
121
+ * @param i 原始数组索引 i
122
+ * @param delta 变化值 = 更新以后的值 - 原始值
123
+ */
124
+ public void update (int i , int delta ) {
125
+ // 从下到上更新,注意,预处理数组,比原始数组的 len 大 1,故 预处理索引的最大值为 len
126
+ while (i <= len ) {
127
+ tree [i ] += delta ;
128
+ i += lowbit (i );
129
+ }
130
+ all ++;
131
+ }
132
+
133
+ //区间更新
134
+ void update (int x , int y , int k ) {
135
+ update (x , k );
136
+ update (y + 1 , -k );
137
+ }
138
+
139
+ /**
140
+ * 查询前缀和
141
+ *
142
+ * @param i 前缀的最大索引,即查询区间 [0, i] 的所有元素之和
143
+ */
144
+ public int query (int i ) {
145
+ // 从右到左查询
146
+ int sum = 0 ;
147
+ while (i > 0 ) {
148
+ sum += tree [i ];
149
+ i -= lowbit (i );
150
+ }
151
+ return all -sum ;
152
+ }
153
+
154
+ public int lowbit (int x ) {
155
+ return x & (-x );
156
+ }
157
+ }
158
+
159
+ /* public int[] resultArray(int[] nums) {
160
+ List<Integer> arr1 = new ArrayList<>();
161
+ List<Integer> arr2 = new ArrayList<>();
162
+ arr1.add(nums[0]);
163
+ arr2.add(nums[1]);
164
+
165
+ int[] copy = Arrays.copyOf(nums, nums.length);
166
+ Arrays.sort(copy);
167
+ Map<Integer, Integer> Index = new HashMap<>();
168
+ for (int i = 0; i < copy.length; i++) {
169
+ Index.put(copy[i], i+1);
170
+ }
171
+
172
+ FenwickTree f1 = new FenwickTree(nums.length+1);
173
+ FenwickTree f2 = new FenwickTree(nums.length+1);
174
+ f1.update(Index.get(nums[0]), 1);
175
+ f2.update(Index.get(nums[1]), 1);
176
+
177
+ for (int i = 2; i < nums.length; i++) {
178
+ int num = nums[i];
179
+ Integer index = Index.get(num);
180
+ int q1 = f1.query(index);
181
+ int q2 = f2.query(index);
182
+ if(q1>q2){
183
+ arr1.add(num);
184
+ f1.update(index,1);
185
+ }else if(q2>q1){
186
+ arr2.add(num);
187
+ f2.update(index,1);
188
+ }else {
189
+ if(arr1.size()>arr2.size()){
190
+ arr2.add(num);
191
+ f2.update(index,1);
192
+ }else {
193
+ arr1.add(num);
194
+ f1.update(index,1);
195
+ }
196
+ }
197
+
198
+ }
199
+ int[] res = new int[nums.length];
200
+ int i = 0;
201
+ for (Integer integer : arr1) {
202
+ res[i++] = integer;
203
+ }
204
+ for (Integer integer : arr2) {
205
+ res[i++] = integer;
206
+ }
207
+ return res;
208
+
209
+ }*/
210
+
211
+ // 离散化树状数组
212
+ public int [] resultArray (int [] nums ) {
213
+ List <Integer > arr1 = new ArrayList <>();
214
+ List <Integer > arr2 = new ArrayList <>();
215
+ arr1 .add (nums [0 ]);
216
+ arr2 .add (nums [1 ]);
217
+
218
+ int max =0 ;
219
+ for (int num : nums ) {
220
+ max =Math .max (num ,max );
221
+ }
222
+
223
+
224
+ FenwickTree f1 = new FenwickTree (max +1 );
225
+ FenwickTree f2 = new FenwickTree (max +1 );
226
+ f1 .update (nums [0 ]+1 , 1 );
227
+ f2 .update (nums [1 ]+1 , 1 );
228
+
229
+ for (int i = 2 ; i < nums .length ; i ++) {
230
+ int num = nums [i ];
231
+ Integer index =num +1 ;
232
+ int q1 = f1 .query (index );
233
+ int q2 = f2 .query (index );
234
+ if (q1 >q2 ){
235
+ arr1 .add (num );
236
+ f1 .update (index ,1 );
237
+ }else if (q2 >q1 ){
238
+ arr2 .add (num );
239
+ f2 .update (index ,1 );
240
+ }else {
241
+ if (arr1 .size ()>arr2 .size ()){
242
+ arr2 .add (num );
243
+ f2 .update (index ,1 );
244
+ }else {
245
+ arr1 .add (num );
246
+ f1 .update (index ,1 );
247
+ }
248
+ }
249
+
250
+ }
251
+ int [] res = new int [nums .length ];
252
+ int i = 0 ;
253
+ for (Integer integer : arr1 ) {
254
+ res [i ++] = integer ;
255
+ }
256
+ for (Integer integer : arr2 ) {
257
+ res [i ++] = integer ;
258
+ }
259
+ return res ;
260
+
261
+ }
262
+
263
+ public static void main (String [] args ) {
264
+ wk387 w = new wk387 ();
265
+ w .resultArray (new int []{5 ,14 ,3 ,1 ,2 });
266
+ }
267
+ }
0 commit comments