NC221661. 煎饼
描述
输入描述
一共输入s+1行。第一行输入三个整数n,m,s,表示煎锅一共有n个位置,煎饼有m种类型,牛牛一共执行s次操作。
第二行到第s+1行每行2-3个整数,具体操作见题目描述。
输出描述
对于每次4号操作,输出一个整数,表示牛牛偷吃的煎饼类型。中间用换行隔开。
示例1
输入:
5 8 8 1 1 1 1 1 2 1 3 3 1 3 4 3 3 2 1 3 4 2 4 1
输出:
-1 3
C++ 解法, 执行用时: 467ms, 内存消耗: 40596K, 提交时间: 2022-06-18 19:57:13
#include "bits/stdc++.h" using namespace std; const int MAXN = 1e6 + 6; struct node { bool flag; list<int>cake; }a[MAXN]; int main(void) { // freopen("t2_10.in","r",stdin); // freopen("t2_10.out","w",stdout); int n, m, s; cin >> n >> m >> s; for (int i = 0; i < s; ++i) { int key, x; scanf("%d %d", &key, &x); if (key == 1) { int k; scanf("%d", &k); a[x].cake.push_back(k); }else if (key == 2) { int y; scanf("%d", &y); //cout << "merge y to x before: "; for (auto it : a[x].cake) cout << it << ' '; cout << endl; if (a[y].flag && a[x].flag) { a[x].cake.merge(a[y].cake, greater<int>()); }else if (!a[y].flag && !a[x].flag) { a[x].cake.merge(a[y].cake); }else if (!a[y].flag && a[x].flag) { while (!a[y].cake.empty()) { a[x].cake.push_front(a[y].cake.back()); a[y].cake.pop_back(); } }else { while (!a[y].cake.empty()) { a[x].cake.push_back(a[y].cake.back()); a[y].cake.pop_back(); } } //cout << "merge end: "; for (auto it : a[x].cake) cout << it << ' '; cout << endl; a[y].cake.clear(); a[y].flag = false; }else if (key == 3) { a[key].flag = !a[key].flag; }else { if (a[x].cake.empty()) { printf("-1\n"); }else if (a[x].flag) { printf("%d\n", a[x].cake.front()); //a[key].cake.pop_front(); }else { printf("%d\n", a[x].cake.back()); //a[key].cake.pop_back(); } } } //for (int i = 0; i <= n; ++i) { cout << i << ": "; for (auto it : a[i].cake) cout << it << ' '; cout << endl;} return 0; }