File tree 3 files changed +15
-11
lines changed
3 files changed +15
-11
lines changed Original file line number Diff line number Diff line change @@ -285,12 +285,19 @@ class Solution {
285
285
ans = Math . max(ans, cur);
286
286
return cur;
287
287
}
288
- void swap (int [] arr , int i , int j ) {
289
- int c = arr[i];
290
- arr[i] = arr[j];
291
- arr[j] = c;
288
+ void shuffle (int [] nums ) {
289
+ for (int i = n; i > 0 ; i-- ) {
290
+ int idx = random. nextInt(i);
291
+ swap(nums, idx, i - 1 );
292
+ }
293
+ }
294
+ void swap (int [] nums , int a , int b ) {
295
+ int c = nums[a];
296
+ nums[a] = nums[b];
297
+ nums[b] = c;
292
298
}
293
299
void sa () {
300
+ shuffle(ws);
294
301
for (double t = hi; t > lo; t *= fa) {
295
302
int a = random. nextInt(n), b = random. nextInt(n);
296
303
int prev = calc();
Original file line number Diff line number Diff line change 4
4
5
5
Tag : 「DFS」、「模拟退火」、「启发式搜素」、「随机化」
6
6
7
-
8
-
9
-
10
7
给你一个整数数组 ` jobs ` ,其中 $jobs[ i] $ 是完成第 $i$ 项工作要花费的时间。
11
8
12
9
请你将这些工作分配给 $k$ 位工人。所有工作都应该分配给工人,且每项工作只能分配给一位工人。
@@ -195,7 +192,7 @@ class Solution {
195
192
int ans = 0x3f3f3f3f ;
196
193
Random random = new Random (20210508 );
197
194
// 最高温/最低温/变化速率(以什么速度进行退火,系数越低退火越快,迭代次数越少,落入「局部最优」(WA)的概率越高;系数越高 TLE 风险越大)
198
- double hi = 1e5 , lo = 1e-5 , fa = 0.90 ;
195
+ double hi = 1e8 , lo = 1e-4 , fa = 0.90 ;
199
196
// 迭代次数,与变化速率同理
200
197
int N = 400 ;
201
198
@@ -230,6 +227,7 @@ class Solution {
230
227
arr[j] = c;
231
228
}
232
229
void sa () {
230
+ shuffle(jobs);
233
231
for (double t = hi; t > lo; t *= fa) {
234
232
int a = random. nextInt(n), b = random. nextInt(n);
235
233
int prev = calc(); // 退火前
@@ -244,7 +242,6 @@ class Solution {
244
242
jobs = _jobs;
245
243
n = jobs. length;
246
244
k = _k;
247
- shuffle(jobs);
248
245
while (N -- > 0 ) sa();
249
246
return ans;
250
247
}
Original file line number Diff line number Diff line change @@ -116,7 +116,7 @@ class Solution {
116
116
int [] ms;
117
117
int n, k;
118
118
Random random = new Random (20220601 );
119
- double hi = 1e4 , lo = 1e-4 , fa = 0.98 ;
119
+ double hi = 1e9 , lo = 1e-4 , fa = 0.95 ;
120
120
int N = 400 ;
121
121
boolean ans = false ;
122
122
int calc () {
@@ -130,6 +130,7 @@ class Solution {
130
130
return diff;
131
131
}
132
132
void sa () {
133
+ shuffle(ms);
133
134
for (double t = hi; t > lo && ! ans; t *= fa) {
134
135
int a = random. nextInt(n), b = random. nextInt(n);
135
136
if (a == b) continue ;
@@ -147,7 +148,6 @@ class Solution {
147
148
for (int i : ms) sum += i;
148
149
k = sum / 4 ;
149
150
if (k * 4 != sum) return false ;
150
- shuffle(ms);
151
151
while (! ans && N -- > 0 ) sa();
152
152
return ans;
153
153
}
You can’t perform that action at this time.
0 commit comments