版本比较
题解:P3212 [HNOI2011] 任务调度
正文
增加 14 行删除 14 行
18 行未改动19202122232425262717 行未改动4546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394954 行未改动18 行未改动int t, a, b;} tsk[25];int ans = INF;int calc(int p[]) {vector<task> A, B;//分别存储现在A上执行和先在B上执行的任务for (int i = 1; i <= n; i++) {int id = p[i];if (tsk[id].t == 1) A.push_back(tsk[id]);else if (tsk[id].t == 2) B.push_back(tsk[id]);17 行未改动if (tb > ta) tb += x.b;else tb = ta + x.b;}int res2 = max(ta, tb);//res1和res2其实是两种方案,要么先把A的任务执行完,要么先把B的任务执行完return max(res1, res2);}signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);mt19937 rand(time(NULL));//随机函数cin >> n;for (int i = 1; i <= n; i++) cin >> tsk[i].t >> tsk[i].a >> tsk[i].b;int mp[25], p[25], id[25];for (int i = 1; i <= n; i++) mp[i] = i;//mp表示当前较优解的执行顺序random_shuffle(mp + 1, mp + n + 1);//打乱while ((double)clock() / CLOCKS_PER_SEC < 0.95) {for (int i = 1; i <= n; i++) p[i] = mp[i];for (int i = 1; i <= 3; i++) swap(p[rand() % n + 1], p[rand() % n + 1]);//在较优解附近搜索int now = calc(p);//计算当前最少总时间if (now < ans) {ans = now;for (int i = 1; i <= n; i++) mp[i] = p[i];//如果找到了更优解自然要更新}//下面是局部搜索 尝试交换当前较优解的每一对位置for (int i = 1; i <= n; i++) id[i] = i;random_shuffle(id + 1, id + n + 1);//初始化bool flg = 1;while (flg) {flg = 0;for (int i = 1; i < n; i++) {for (int j = i + 1; j <= n; j++) {int x = id[i], y = id[j];swap(p[x], p[y]);//交换int nxt = calc(p);if (nxt < ans) {ans = now = nxt;for (int k = 1; k <= n; k++) mp[k] = p[k];flg = 1;} else if (nxt < now) {now = nxt;flg = 1;//无论是比ans小还是now小,都代表这次交换一定是不劣的//所以flg=1还可以尝试继续交换。} else swap(p[x], p[y]);//否则就换回去}}}}4 行未改动18 行未改动19202122232425262717 行未改动4546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394954 行未改动18 行未改动int t, a, b;} tsk[25];int ans = INF;int calc(int p[]) {vector<task> A, B;//分别存储现在A上执行和先在B上执行的任务for (int i = 1; i <= n; i++) {int id = p[i];if (tsk[id].t == 1) A.push_back(tsk[id]);else if (tsk[id].t == 2) B.push_back(tsk[id]);17 行未改动if (tb > ta) tb += x.b;else tb = ta + x.b;}int res2 = max(ta, tb);//res1和res2其实是两种方案,要么先把A的任务执行完,要么先把B的任务执行完return max(res1, res2);}signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);mt19937 rand(time(NULL));//随机函数cin >> n;for (int i = 1; i <= n; i++) cin >> tsk[i].t >> tsk[i].a >> tsk[i].b;int mp[25], p[25], id[25];for (int i = 1; i <= n; i++) mp[i] = i;//mp表示当前较优解的执行顺序random_shuffle(mp + 1, mp + n + 1);//打乱while ((double)clock() / CLOCKS_PER_SEC < 0.95) {for (int i = 1; i <= n; i++) p[i] = mp[i];for (int i = 1; i <= 3; i++) swap(p[rand() % n + 1], p[rand() % n + 1]);//在较优解附近搜索int now = calc(p);//计算当前最少总时间if (now < ans) {ans = now;for (int i = 1; i <= n; i++) mp[i] = p[i];//如果找到了更优解自然要更新}//下面是局部搜索 尝试交换当前较优解的每一对位置for (int i = 1; i <= n; i++) id[i] = i;random_shuffle(id + 1, id + n + 1);//初始化bool flg = 1;while (flg) {flg = 0;for (int i = 1; i < n; i++) {for (int j = i + 1; j <= n; j++) {int x = id[i], y = id[j];swap(p[x], p[y]);//交换int nxt = calc(p);if (nxt < ans) {ans = now = nxt;for (int k = 1; k <= n; k++) mp[k] = p[k];flg = 1;} else if (nxt < now) {now = nxt;flg = 1;//无论是比ans小还是now小,都代表这次交换一定是不劣的//所以flg=1还可以尝试继续交换。} else swap(p[x], p[y]);//否则就换回去}}}}4 行未改动