本文主要是介绍12657 - Boxes in a Line (UVA),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接如下:
Online Judge
代码如下:
#include <cstdio>
#include <utility>
// #define debug
const int maxx = 100010;int n, m, kase, op, a, b;
int prev[maxx], next[maxx];int main(){#ifdef debugfreopen("0.txt", "r", stdin);freopen("1.txt", "w", stdout);#endifkase = 0;while (scanf("%d %d", &n, &m) == 2){bool flag = true;for (int i = 0; i <= n + 1; ++i){next[i] = i + 1;prev[i] = i - 1;}while (m--){scanf("%d", &op);if (op == 4){flag = !flag;} else {scanf("%d %d", &a, &b);if ((op == 1 && ((flag && next[a] == b) || (!flag && prev[a] == b))) || (op == 2 && ((flag && prev[a] == b) || (!flag && next[a] == b)))){continue;}if ((op == 1 && flag) || (op == 2 && !flag)){prev[next[a]] = prev[a];next[prev[a]] = next[a];next[prev[b]] = a;prev[a] = prev[b];next[a] = b;prev[b] = a;} else if ((op == 1 && !flag) || (op == 2 && flag)){prev[next[a]] = prev[a];next[prev[a]] = next[a];prev[next[b]] = a;next[a] = next[b];next[b] = a;prev[a] = b;} else if (op == 3){if (next[a] == b){next[prev[a]] = b;prev[b] = prev[a];next[a] = next[b];prev[next[b]] = a;prev[a] = b;next[b] = a;} else if (prev[a] == b){next[prev[b]] = a;prev[a] = prev[b];prev[next[a]] = b;next[b] = next[a];prev[b] = a;next[a] = b;} else{prev[next[a]] = b;next[prev[a]] = b;prev[next[b]] = a;next[prev[b]] = a;std::swap(next[a], next[b]);std::swap(prev[a], prev[b]);}}}}long long ans = 0;if (flag){for (int i = next[0]; i > 0 && i <= n; i = next[i]){ans += i;i = next[i];}} else {for (int i = prev[n + 1]; i > 0 && i <= n; i = prev[i]){ans += i;i = prev[i];}}printf("Case %d: %lld\n", ++kase, ans);}#ifdef debugfclose(stdin);fclose(stdout);#endifreturn 0;
}
这篇关于12657 - Boxes in a Line (UVA)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!