Repository navigation
Expand file tree
/
Copy pathcopy.cpp
More file actions
2541 lines (2313 loc) · 66.1 KB
/
Copy pathcopy.cpp
File metadata and controls
2541 lines (2313 loc) · 66.1 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 不再严格划分 size,有连续空位置就存放
// sublime 里那段代码要设计一种算法更加合理地分配空间,不能让 16 号出现在所有盘里
// 存储的时候先存 size 大的,再存 size 小的,石头 -> 沙子 -> 水
// cur_score 必须每一轮都计算!不然这个分数就是扯淡
// 上线段树维护 score 信息,每 UPDATE_DISK_SCORE_FREQUENCY 更新一次,直接 init
// 跳跃的时候,不能只跳那 BLOCK_NUM 个点,这绝对是不行的
// 锁机制,对于当前这个磁头即将读取的一部分 obj,我认为这就是它认定的东西,别人考虑分数的时候就不应该再计算这些 obj 的得分了!
// 存放 obj 的时候,优先放到当前磁头的前方?便于快速读取到
// 分块必须要改,或者就跳转到颜色初始,或者分一个块为 token / 16
// USE_SIZE 是非常重要的参数,要认真考虑
// 是否有必要每个盘都 16 种颜色?8 - 8 分会不会更好?
// 垃圾回收机制要认证改进
#include <bits/stdc++.h>
using namespace std;
// #define DEBUG
// #define GARBAGE_COLLECTION
// #define SHOW
#define SHOW_EPOCH (29075)
#ifdef DEBUG
#define UPDATE_DISK_SCORE_FREQUENCY (10000000)
#define MAX_DISK_SIZE (13157)
#define BLOCK_NUM (60)
const int BLOCK_SIZE = MAX_DISK_SIZE / BLOCK_NUM;
#define LOCK_UNITS (BLOCK_SIZE) // 很重要的一个参数,但不知道为啥!!!!!
// #define LOCK_TIMES (LOCK_UNITS / (272 / 16))
#define LOCK_TIMES (50)
#else
#define UPDATE_DISK_SCORE_FREQUENCY (10000000)
#define MAX_DISK_SIZE (16384)
#define BLOCK_NUM (110) // 这个参数调大一点可能会变好,比如 20
const int BLOCK_SIZE = MAX_DISK_SIZE / BLOCK_NUM;
#define LOCK_UNITS (BLOCK_SIZE)
// #define LOCK_TIMES (LOCK_UNITS / (340 / 16))
#define LOCK_TIMES (70)
#endif
#define MAX_TAG (16)
#define MAX_SIZE (5)
#define MAX_DISK_NUM (10)
// #define JUMP_FREQUENCY (8)
#define JUMP_BIAS (LOCK_TIMES)
#define MAX_QUERY_TIME (105)
#define MAX_HEAD_NUM (2)
#define MAX_REQUEST_NUM (30000000)
#define MAX_OBJECT_NUM (100000)
#define REP_NUM (3)
#define FRE_PER_SLICING (1800)
#define EXTRA_TIME (105)
#define MAX_EPOCH (50)
#define MAX_WRITE_LEN (100005)
#define INF (1000000000)
#define EPS (1e-6)
#define DROP_SCORE (0)
#define DECIDE_CONTINUE_READ (10)
#define TRASH_PERPORTION (0)
#define EXTRA_BLOCK (-300)
#define USE_SIZE (MAX_DISK_SIZE / 3 + EXTRA_BLOCK) // [0, USE_SIZE - 1]
#define PREDICT (13) // 当前位置往前看多少个颜色块的得分,这个参数要好好斟酌!!!!!
#define DANGEROUSE_TIME (200) // 废弃了
#define PUNISH_WEIGHT (100)
#define GIVE_UP_LIMIT (105)
#define BLOCK_BIAS (5)
#define SEED (11111111)
vector<int> haha_qid;
vector<float> query_times = {0, 3077, 3582, 4026, 3546, 3725, 2124, 3320, 5596, 3041, 5306, 3475, 2769, 3897, 2888, 3951, 2994, };
vector<float> weight(MAX_TAG + 1, 1);
vector<vector<vector<pair<int, int>>>> priority_pos(MAX_TAG + 1, vector<vector<pair<int, int>>>(MAX_SIZE + 1));
int T, M, N, V, G, K;
int fre_del[MAX_TAG + 1][MAX_EPOCH];
int fre_write[MAX_TAG + 1][MAX_EPOCH];
int fre_read[MAX_TAG + 1][MAX_EPOCH];
int g[MAX_EPOCH];
vector<vector<tuple<int, int, int>>> disk_manage; // 前提是必须有 10 块!
vector<vector<int>> disk_select;
int pre_cost[MAX_DISK_NUM][MAX_HEAD_NUM];
char pre_move[MAX_DISK_NUM][MAX_HEAD_NUM];
vector<vector<int>> pre_jump(MAX_DISK_NUM, vector<int>(MAX_HEAD_NUM, -JUMP_BIAS));
vector<vector<bool>> pre_decide(MAX_DISK_NUM, vector<bool>(MAX_HEAD_NUM, false));
int timestamp = 0; // 全局时间戳
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(SEED);
vector<int> random_color_sequence = {
8, 5, 2, 4, 9, 7, 3, 1, 0, 6,
};
vector<int> color_use(MAX_DISK_NUM, 3);
vector<vector<int>> color_sequence = {
{1, },
{2, 15, },
{3, 5, 7, 8, 10, 11, 13, 14, },
{4, 6, 9, 12, 16, },
};
vector<int> bel = {-1, 0, 1, 2, 3, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 1, 3, };
// vector<int> bel = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
#ifdef DEBUG
vector<vector<pair<int, int>>> color_init = {
{
{5, 103},
{4, 127},
{2, 129},
{6, 247},
{13, 160},
{8, 302},
{14, 314},
{15, 122},
{12, 143},
{9, 412},
},
{
{3, 380},
{1, 192},
{7, 145},
{11, 449},
{16, 441},
{10, 454},
},
{
{8, 310},
{5, 105},
{16, 402},
{1, 175},
{15, 126},
{2, 133},
{4, 130},
{6, 254},
{9, 424},
},
{
{10, 440},
{7, 141},
{11, 435},
{14, 344},
{13, 175},
{12, 157},
{3, 369},
},
{
{8, 293},
{9, 400},
{10, 390},
{5, 99},
{1, 165},
{3, 327},
{11, 386},
},
{
{2, 151},
{15, 143},
{7, 150},
{12, 167},
{16, 457},
{13, 187},
{4, 148},
{14, 367},
{6, 289},
},
{
{4, 132},
{5, 106},
{8, 313},
{14, 326},
{13, 166},
{11, 413},
{9, 428},
{1, 177},
},
{
{7, 139},
{10, 436},
{6, 268},
{3, 365},
{12, 155},
{15, 133},
{2, 140},
{16, 424},
},
{
{16, 442},
{2, 146},
{3, 381},
{7, 145},
{15, 138},
{9, 466},
{8, 341},
},
{
{1, 170},
{14, 313},
{13, 159},
{4, 127},
{11, 397},
{10, 401},
{5, 102},
{12, 143},
{6, 247},
},
{
{8, 298},
{3, 333},
{6, 244},
{13, 158},
{1, 168},
{12, 141},
{9, 408},
{14, 310},
},
{
{10, 460},
{4, 145},
{2, 148},
{15, 140},
{11, 455},
{7, 147},
{5, 117},
{16, 447},
},
{
{5, 104},
{6, 252},
{8, 307},
{1, 173},
{11, 405},
{16, 398},
{9, 420},
},
{
{4, 140},
{13, 177},
{7, 142},
{12, 158},
{15, 135},
{2, 143},
{3, 373},
{10, 444},
{14, 347},
},
{
{6, 265},
{9, 442},
{3, 361},
{11, 426},
{10, 431},
{4, 136},
},
{
{7, 135},
{16, 410},
{2, 136},
{5, 108},
{8, 317},
{15, 129},
{13, 168},
{1, 178},
{14, 329},
{12, 150},
},
{
{9, 447},
{12, 155},
{10, 436},
{2, 140},
{6, 268},
{5, 111},
{4, 137},
{3, 365},
},
{
{1, 177},
{13, 166},
{15, 127},
{7, 133},
{14, 326},
{8, 313},
{11, 413},
{16, 406},
},
{
{5, 110},
{11, 427},
{4, 136},
{16, 419},
{1, 182},
{12, 154},
{7, 138},
{3, 362},
{15, 131},
},
{
{8, 316},
{9, 432},
{14, 329},
{10, 421},
{2, 136},
{13, 167},
{6, 259},
},
};
#else
vector<vector<pair<int, int>>> color_init = {
{
{9, 5461},
},
{
{10, 5461},
},
{
{11, 5461},
},
{
{16, 5461},
},
{
{3, 5461},
},
{
{14, 5461},
},
{
{8, 3881},
{15, 1579},
},
{
{6, 3589},
{7, 1871},
},
{
{1, 2177},
{2, 1663},
{4, 1620},
},
{
{13, 2147},
{12, 1929},
{5, 1384},
},
};
#endif
vector<vector<int>> belong(MAX_TAG + 1);
vector<vector<int>> hot_tags = {
};
// ------------------------------------ 全局函数声明 ----------------------------------------
float Get_Pos_Score(int disk_id, int pos, int time); // 获取一个硬盘上一个位置 pos 的得分
void Pre_Process(); // 对总和输入数据的预处理
void total_init(); // 预处理乱七八糟的东西,比如 disk 的余量集合
bool Random_Appear(int p); // 判断概率 p% 是否发生
pair<float, int> Simulate(int disk_id, int idx, int time, char &pre_mov, int &pre_cos);
// ------------------------------------ 结构体定义 ----------------------------------------
struct Disk {
pair<int, int> d[MAX_DISK_SIZE];
bool flag[MAX_DISK_SIZE];
set<int> space[MAX_SIZE + 1];
vector<int> has_tag;
int color_tag[MAX_DISK_SIZE];
vector<int> color_type;
vector<int> head;
vector<pair<int, int>> range;
vector<pair<int, int>> block;
int start_write = 0;
vector<float> score;
int siz = 0;
int disk_id = 0;
vector<int> max_score_pos; // 每个 range 里的价值最大块位置
vector<float> max_score; // 每个 range 里的价值最大块价值
vector<float> cur_score; // 当前位置向前走 PREDICT 个块的价值
int use_size = 0;
vector<int> working;
vector<int> color_start;
Disk() { }
void init(int disk_id) {
this->disk_id = disk_id;
working.resize(MAX_HEAD_NUM, -1);
head.resize(MAX_HEAD_NUM, 0);
cur_score.resize(MAX_HEAD_NUM, 0);
range.resize(MAX_HEAD_NUM, {0, 0});
max_score_pos.resize(MAX_HEAD_NUM, 0);
max_score.resize(MAX_HEAD_NUM, 0);
for (int i = 0; i < MAX_DISK_SIZE; i++) {
d[i] = {-1, -1};
color_tag[i] = -1;
flag[i] = false;
}
static int idx = 0;
for (; color_type.size() < 3; idx = (idx + 1) % N) {
color_use[random_color_sequence[idx]]--;
color_type.emplace_back(random_color_sequence[idx]);
}
assert(color_type.size() == 3);
int tot = 0;
for (auto [tag, cnt] : color_init[disk_id]) {
tot += cnt;
}
int split = USE_SIZE;
range[0] = {0, split / 2 - 1};
range[1] = {split / 2, split - 1};
// 这里也用垃圾桶?把错误的标签优先放到最后 5%?
}
void Color() {
int idx = 0;
// int tot = 0;
// for (int i = 2 * disk_id; i <= 2 * disk_id + 1; i++) {
// range[i - 2 * disk_id].first = tot;
// for (auto [tag, cnt] : color_init[i]) {
// tot += cnt;
// while (cnt--) {
// color_tag[idx++] = tag;
// }
// }
// range[i - 2 * disk_id].second = tot - 1;
// }
// use_size = range[MAX_HEAD_NUM - 1].second + 1;
// return;
use_size = USE_SIZE;
int tot = accumulate(query_times.begin(), query_times.end(), 0);
for (int i = 0; i < (MAX_HEAD_NUM); i++) {
int start = range[i].first;
vector<int> pos(MAX_TAG + 1);
iota(pos.begin(), pos.end(), 0);
shuffle(pos.begin() + 1, pos.end(), rng);
// for (auto c : color_sequence) {
// for (auto i : c) {
// int cnt = (USE_SIZE / (MAX_HEAD_NUM)) * 1. * query_times[i] / tot;
// int c = cnt;
// while (cnt-- && idx < USE_SIZE) {
// color_tag[idx++] = i;
// }
// block.push_back({start, c});
// start += c;
// }
// }
// score.resize(block.size());
for (int j = 1; j <= MAX_TAG; j++) {
int i = pos[j];
int cnt = (USE_SIZE / (MAX_HEAD_NUM)) * 1. * query_times[i] / tot;
int c = cnt;
while (cnt-- && idx < USE_SIZE) {
color_tag[idx++] = i;
}
block.push_back({start, c});
start += c;
}
score.resize(block.size());
}
return;
// for (int i = 0; i < color_type.size(); i++) {
// int type = color_type[i];
// int tot = 0;
// for (auto [tag, cnt] : color_init[type]) {
// tot += cnt;
// }
// for (auto [tag, cnt] : color_init[type]) {
// if (i == 0) {
// cnt += EXTRA_BLOCK * 1. * cnt / tot;
// }
// // cnt += EXTRA_BLOCK;
// while (cnt-- && idx < V) {
// color_tag[idx++] = tag;
// }
// }
// }
// int tot = 0;
// for (auto tag : has_tag) {
// tot += query_times[tag];
// }
// int idx = 0;
// for (auto tag : has_tag) {
// color_start.push_back(idx);
// int len = 1. * query_times[tag] / tot * V * (1 - TRASH_PERPORTION);
// for (int cnt = len; idx < V * (1 - TRASH_PERPORTION) && cnt--; idx++) {
// color_tag[idx] = tag;
// }
// }
// int shift = rng() % V;
// auto y = color_tag;
// for (int i = 0; i < V; i++) {
// color_tag[i] = y[(i + shift) % V];
// }
}
int size() {
return siz;
}
bool capacity(int obj_tag, int obj_size, int is_limit) {
bool cap = 0;
if (is_limit == -1) {
if (start_write == 0) {
for (int i = 0; i + obj_size - 1 < use_size / MAX_HEAD_NUM; i++) {
if (color_tag[i] != obj_tag || d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
}
if (!ok) continue;
cap = 1;
break;
}
} else {
for (int i = use_size / MAX_HEAD_NUM; i + obj_size - 1 < use_size; i++) {
if (color_tag[i] != obj_tag || d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
}
if (!ok) continue;
cap = 1;
break;
}
}
} else if (is_limit == -2) {
int available = 0;
for (int i = use_size; i <= V - obj_size && available < obj_size; i++) {
if (d[i].first != -1) continue;
available++;
}
cap = available == obj_size;
} else if (is_limit == -3) {
for (int i = 0; i + obj_size - 1 < use_size; i++) {
if (d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (d[j].first != -1) ok = 0;
}
if (!ok) continue;
cap = 1;
break;
}
} else if (is_limit == -4) {
for (int i = 0; i + obj_size - 1 < use_size; i++) {
if (color_tag[i] != obj_tag || d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
}
if (!ok) continue;
cap = 1;
break;
}
}
// 可以搞个 -3 表示需要无色且 -1
return cap;
}
// 以及染色为什么不只考虑分配 query_times 个???
void Cal_Current_Score() { // 待修改------------------------------------------------------------------------------
for (int head_idx = 0; head_idx < MAX_HEAD_NUM; head_idx++) {
set<int> cnt;
cur_score[head_idx] = 0;
int i = head[head_idx];
// char pre_mov = pre_move[disk_id];
// int pre_cos = pre_cost[disk_id];
while (cnt.size() <= PREDICT) { // 当前位置往前走 n 种颜色的总价值 v.s. 某一种颜色的价值
cur_score[head_idx] += Get_Pos_Score(disk_id, i, timestamp);
cnt.insert(color_tag[i]);
i = (i + 1) % use_size;
}
// for (int t = timestamp; t < timestamp + PREDICT; t++) {
// // auto [score, idx] = Simulate(disk_id, i, t, pre_mov, pre_cos);
// // i = idx;
// // cur_score[head_idx] += score;
// for (int cnt = BLOCK_SIZE; cnt--; i = (i + 1) % V) {
// cur_score[head_idx] += Get_Pos_Score(disk_id, i, t);
// }
// }
}
}
void Cal_Max_Score() {
for (int head_id = 0; head_id < MAX_HEAD_NUM; head_id++) {
max_score[head_id] = -1;
max_score_pos[head_id] = -1;
for (int i = 0; i < block.size(); i++) {
auto [start, units] = block[i];
if (start < range[head_id].first || start + units - 1 > range[head_id].second) {
continue;
}
if (score[i] > max_score[head_id]) {
max_score[head_id] = score[i];
max_score_pos[head_id] = start;
}
}
// int block = -1;
// for (int i = 0; i < BLOCK_NUM; i++) {
// if (i * BLOCK_SIZE < range[head_id].first || (i + 1) * BLOCK_SIZE - 1 > range[head_id].second) {
// continue;
// }
// if (score[i] > max_score[head_id]) {
// max_score[head_id] = score[i];
// block = i;
// }
// }
// assert(block != -1);
// max_score_pos[head_id] = block * BLOCK_SIZE;
// while (d[max_score_pos[head_id]].first == -1) {
// max_score_pos[head_id] = (max_score_pos[head_id] + 1) % V;
// }
}
}
void Cal_Block_Score() {
for (int i = 0; i < block.size(); i++) {
score[i] = 0;
auto [start, units] = block[i];
for (int j = start; units--; j++) {
score[i] += Get_Pos_Score(disk_id, j, timestamp);
}
}
// for (int block = 0; block < BLOCK_NUM; block++) {
// int i = block * BLOCK_SIZE;
// float sc = 0;
// char pre_mov = 'j';
// int pre_cos = 0;
// for (int t = timestamp; t < timestamp + PREDICT; t++) {
// // auto [score, idx] = Simulate(disk_id, i, t, pre_mov, pre_cos);
// // i = idx;
// // sc += score;
// for (int cnt = BLOCK_SIZE; cnt--; i = (i + 1) % V) {
// sc += Get_Pos_Score(disk_id, i, t);
// }
// }
// score[block] = sc;
// // int l = block * BLOCK_SIZE;
// // int r = l + BLOCK_SIZE - 1;
// // float sc = 0;
// // for (int i = l; i <= r; i++) {
// // sc += Get_Pos_Score(disk_id, i, timestamp);
// // }
// // score[block] = sc;
// }
}
void Cal_Score() { // 计算走 PREDICT - 1 个块的价值
Cal_Block_Score();
Cal_Max_Score();
Cal_Current_Score();
}
vector<int> Write(int obj_id, int obj_size, int obj_tag, int is_limit) {
vector<int> write_idx;
siz += obj_size;
if (is_limit == -1) {
if (start_write == 0) {
for (int i = 0; i + obj_size - 1 < use_size / MAX_HEAD_NUM; i++) {
if (color_tag[i] != obj_tag || d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
}
if (!ok) continue;
write_idx.push_back(i);
break;
}
} else {
for (int i = use_size / MAX_HEAD_NUM; i + obj_size - 1 < use_size; i++) {
if (color_tag[i] != obj_tag || d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
}
if (!ok) continue;
write_idx.push_back(i);
break;
}
}
start_write = (start_write + 1) % MAX_HEAD_NUM;
} else if (is_limit == -2) {
for (int i = V - obj_size; i >= use_size && write_idx.size() < obj_size; i--) {
if (d[i].first != -1) continue;
write_idx.push_back(i);
}
reverse(write_idx.begin(), write_idx.end());
} else if (is_limit == -3) {
for (int i = 0; i + obj_size - 1 < use_size; i++) {
if (d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (d[j].first != -1) ok = 0;
}
if (!ok) continue;
write_idx.push_back(i);
break;
}
} else if (is_limit == -4) {
for (int i = 0; i + obj_size - 1 < use_size; i++) {
if (color_tag[i] != obj_tag || d[i].first != -1) continue;
bool ok = 1;
for (int j = i + 1; j < i + obj_size; j++) {
if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
}
if (!ok) continue;
write_idx.push_back(i);
break;
}
}
assert(write_idx.size() == 1 || write_idx.size() == obj_size);
flag[write_idx[0]] = true;
if (write_idx.size() == 1) {
for (int i = write_idx[0], size = 0; size < obj_size; i++, size++) {
d[i] = {obj_id, size};
}
} else {
int size = 0;
for (auto pos : write_idx) {
d[pos] = {obj_id, size++};
}
}
return write_idx;
{
// if (is_limit >= 0) {
// write_idx = is_limit;
// for (int i = write_idx, size = 0; size < obj_size; i++, size++) {
// d[i] = {obj_id, size};
// }
// write_idx = write_idx;
// // assert(space[obj_size].count(write_idx));
// // space[obj_size].erase(write_idx);
// return write_idx;
// }
// if (is_limit == -1) {
// for (int i = 0; i < V - obj_size; i++) {
// if (color_tag[i] != obj_tag || d[i].first != -1) continue;
// bool ok = 1;
// for (int j = i + 1; j < i + obj_size; j++) {
// if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
// }
// if (!ok) continue;
// int continue_white = 1;
// int j = i + 1;
// while (j < V && continue_white < BLOCK_BIAS) {
// continue_white++;
// j++;
// }
// j = i - 1;
// while (j >= 0 && continue_white < BLOCK_BIAS) {
// continue_white++;
// j--;
// }
// if (continue_white == BLOCK_BIAS) continue;
// write_idx = i;
// break;
// }
// } else {
// assert(is_limit == -2);
// for (int i = 0; i < V - obj_size; i++) {
// if (color_tag[i] != -1) continue;
// if (d[i].first != -1) continue;
// bool ok = 1;
// for (int j = i + 1; j < i + obj_size; j++) {
// if (d[j].first != -1) ok = 0;
// }
// if (!ok) continue;
// int continue_white = 1;
// int j = i + 1;
// while (j < V && continue_white < BLOCK_BIAS) {
// continue_white++;
// j++;
// }
// j = i - 1;
// while (j >= 0 && continue_white < BLOCK_BIAS) {
// continue_white++;
// j--;
// }
// if (continue_white == BLOCK_BIAS) continue;
// write_idx = i;
// break;
// }
// if (write_idx == -1) {
// for (int i = 0; i < V - obj_size; i++) {
// if (d[i].first != -1) continue;
// bool ok = 1;
// for (int j = i + 1; j < i + obj_size; j++) {
// if (d[j].first != -1) ok = 0;
// }
// if (!ok) continue;
// int continue_white = 1;
// int j = i + 1;
// while (j < V && continue_white < BLOCK_BIAS) {
// continue_white++;
// j++;
// }
// j = i - 1;
// while (j >= 0 && continue_white < BLOCK_BIAS) {
// continue_white++;
// j--;
// }
// if (continue_white == BLOCK_BIAS) continue;
// write_idx = i;
// break;
// }
// }
// }
// if (write_idx == -1) {
// if (is_limit == -1) {
// for (int i = 0; i < V - obj_size; i++) {
// if (color_tag[i] != obj_tag || d[i].first != -1) continue;
// bool ok = 1;
// for (int j = i + 1; j < i + obj_size; j++) {
// if (color_tag[j] != obj_tag || d[j].first != -1) ok = 0;
// }
// if (!ok) continue;
// write_idx = i;
// break;
// }
// } else {
// assert(is_limit == -2);
// for (int i = 0; i < V - obj_size; i++) {
// if (color_tag[i] != -1) continue;
// if (d[i].first != -1) continue;
// bool ok = 1;
// for (int j = i + 1; j < i + obj_size; j++) {
// if (d[j].first != -1) ok = 0;
// }
// if (!ok) continue;
// write_idx = i;
// break;
// }
// if (write_idx == -1) {
// for (int i = 0; i < V - obj_size; i++) {
// if (d[i].first != -1) continue;
// bool ok = 1;
// for (int j = i + 1; j < i + obj_size; j++) {
// if (d[j].first != -1) ok = 0;
// }
// if (!ok) continue;
// write_idx = i;
// break;
// }
// }
// }
// }
// assert(write_idx != -1);
// for (int i = write_idx, size = 0; size < obj_size; i++, size++) {
// d[i] = {obj_id, size};
// }
// return write_idx;
}
}
void erase(int erase_idx) {
assert(d[erase_idx].first != -1);
d[erase_idx] = {-1, -1};
siz--;
}
};
Disk disk[MAX_DISK_NUM]; // 硬盘
struct Request {
int query_time = 0;
int obj_id = -1;
int ned = 0;
int mask = -1;
bool is_give_up = false;
bool is_abort = false;
bool is_done() {
return ned == mask;
}
bool has_part(int part) {
return ned >> part & 1;
}
void set(int part) {
ned |= 1 << part;
}
void erase(int part) {
ned &= ~(1 << part);
}
};
vector<Request> requests(MAX_REQUEST_NUM + 1); // 使用 vector 是因为静态空间不够了
vector<queue<int>> query_per_time(MAX_SIZE + 1); // 必须 tmd 使用 vector,不然会莫名其妙莫名其妙的卡死
// queue<int> query_per_time[MAX_SIZE + 1];
queue<int> waste_qid;
struct Object {
vector<int> bel;
vector<int> unit[REP_NUM];
int size = 0;
int tag = -1;
bool is_delete = false;
pair<int, int> lock = {-1, -1};
int lock_time = 0;
int lock_last = 0;
};
Object objects[MAX_OBJECT_NUM + 1];
unordered_set<int> query[MAX_OBJECT_NUM + 1]; // 每个对象的查询
// ------------------------------------ 全局预处理 ----------------------------------------
void Pre_Process() {
// int batch_num = (T + FRE_PER_SLICING - 1) / FRE_PER_SLICING;
// vector<int> tag(MAX_TAG + 1);
// for (int i = 1; i <= MAX_TAG; i++) {
// for (int j = 1; j <= batch_num; j++) {
// cerr << fre_write[i][j] << ' ';
// tag[i] += fre_read[i][j];
// }
// cerr << endl;
// }
// for (int i = 1; i <= MAX_TAG; i++) {
// cerr << tag[i] << ", ";
// }
cout << "OK\n";
cout.flush();
}
// 60 : 40 : 35 : 18 : 8 sum = 161
void total_init() {
for (int i = 0; i < MAX_DISK_NUM; i++) {
// 染色,对应位置只能存储对应 tag
disk[i].Color();
}
for (int i = 0; i < MAX_DISK_NUM; i++) {
set<int> c;
for (int j = 0; j < V; j++) {
if (disk[i].color_tag[j] == -1) continue;
c.insert(disk[i].color_tag[j]);
}
for (auto t : c) {
belong[t].push_back(i);
}
}
}
void interrupt() {
sleep(1);
cout << "Hello" << endl;
}
bool Random_Appear(int p) {
return rng() % 100 + 1 <= p;
}
// 分数应该与 obj_size 正相关