版本比较
再谈随机化
正文
增加 13 行删除 13 行
12 行未改动131415161718192021222324252627282930313233343517 行未改动53545556575859606126 行未改动88899091929394959697989910010110210310410510610710810911011111211311411511611711811955 行未改动17517617717817918018118218354 行未改动23823924024124224324424524614 行未改动12 行未改动上文说:> 你看这下面的代码:>> ```cpp> srand(k); // 其中 k 是任意一个正整数> ```>> 就是我们的初始化操作。>> 也可以使用>> ```cpp> srand(time(0));> ```>> 这两个写法都可以:第一种方便调试,第二种随机性强。两种都可以使用。我们这里使用第一种。>> 当我们想要调用一个随机数的时候,直接调用 `rand()` 就可以了。17 行未改动在上文中说:> 而当我们想要打乱一个数组 a,其下标从 $1$ 到 $n$,我们可以使用这段代码:>> ```cpp> random_shuffle(a + 1, a + n + 1);> ```26 行未改动const int M = 5;const int INF = 0x3f3f3f;const double down = 0.996;int n, a[N], fri[N][M];signed main() {mt19937 rnd(time(NULL));cin >> n;for (int i = 1; i <= n; i++) {a[i] = i;}for (int i = 1; i <= n; i++) {cin >> fri[i][1] >> fri[i][2] >> fri[i][3];}int temp = 2000000;int ans = INF;while (temp--) {shuffle(a + 1, a + n + 1 , rnd);int tot = 0;for (int i = 1; i <= n; i++) {for(int j = 1; j <= 3; j++) {tot += abs(a[i] - a[fri[i][j]]);}}ans = min(ans, tot / 2);}cout << ans;return 0;}```55 行未改动}if (dy > h) {dy = h;}double now = energy(dx, dy);if (now > good) {tx = dx, ty = dy;good = now;54 行未改动}if (dy > h) {dy = h;}double now = energy(dx, dy);if (now > good) {tx = dx, ty = dy;good = now;14 行未改动12 行未改动131415161718192021222324252627282930313233343517 行未改动53545556575859606126 行未改动88899091929394959697989910010110210310410510610710810911011111211311411511611711811955 行未改动17517617717817918018118218354 行未改动23823924024124224324424524614 行未改动12 行未改动上文说:> 你看这下面的代码:>> ```cpp> srand(k); // 其中 k 是任意一个正整数> ```>> 就是我们的初始化操作。>> 也可以使用>> ```cpp> srand(time(0));> ```>> 这两个写法都可以:第一种方便调试,第二种随机性强。两种都可以使用。我们这里使用第一种。>> 当我们想要调用一个随机数的时候,直接调用 `rand()` 就可以了。17 行未改动在上文中说:> 而当我们想要打乱一个数组 a,其下标从 $1$ 到 $n$,我们可以使用这段代码:>> ```cpp> random_shuffle(a + 1, a + n + 1);> ```26 行未改动const int M = 5;const int INF = 0x3f3f3f;const double down = 0.996;int n, a[N], fri[N][M];signed main() {mt19937 rnd(time(NULL));cin >> n;for (int i = 1; i <= n; i++) {a[i] = i;}for (int i = 1; i <= n; i++) {cin >> fri[i][1] >> fri[i][2] >> fri[i][3];}int temp = 2000000;int ans = INF;while (temp--) {shuffle(a + 1, a + n + 1 , rnd);int tot = 0;for (int i = 1; i <= n; i++) {for(int j = 1; j <= 3; j++) {tot += abs(a[i] - a[fri[i][j]]);}}ans = min(ans, tot / 2);}cout << ans;return 0;}```55 行未改动}if (dy > h) {dy = h;}double now = energy(dx, dy);if (now > good) {tx = dx, ty = dy;good = now;54 行未改动}if (dy > h) {dy = h;}double now = energy(dx, dy);if (now > good) {tx = dx, ty = dy;good = now;14 行未改动