Boxes in a Line
You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands:
• 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ).
• 2 X Y : move box X to the right to Y (ignore this if X is already the right of Y ).
• 3 X Y : swap box X and Y.
• 4: reverse the whole line.
Commands are guaranteed to be valid, i.e. X will be not equal to Y .For example, if n = 6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing 2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1. Then after executing 4, then line becomes 1 3 5 4 6 2.
Input
There will be at most 10 test cases. Each test case begins with a line containing 2 integers n, m(1 ≤ n, m ≤ 100, 000). Each of the following m lines contain a command.
Output
For each test case, print the sum of numbers at odd-indexed positions. Positions are numbered 1 to nfrom left to right.
Sample Input
6 4
1 1 4
2 3 5
3 1 6
4
6 3
1 1 4
2 3 5
3 1 6
100000 1
4
Sample Output
Case 1: 12
Case 2: 9
Case 3: 2500050000
分析
數據量大,如果使用存粹的數組來表示某位置盒子的編號,會因爲頻繁的移動導致超時。就比如一直重複將第一個位置的盒子放到最後一個位置的右邊。這時我們需要移動n-1次。如果這樣操作m次也就是m*(n-1)次移動接近10^12次操作。肯定超時。
可以利用鏈表來表示某個位置關係。這時我們的盒子沒有移動,而是在盒子的上面貼上兩個標籤,一個表示這個盒子的左邊的盒子是哪個,一個是這個盒子的 右邊是哪個盒子。這樣我們的操作就是修改這些盒子上的標籤就能表示出所有盒子的位置。由於盒子不移動,所以我們能迅速定位某個盒子(移動操作針對的是盒子的編號而不是某個位置的盒子)。並且修改盒子上的標籤。
針對盒子標籤的修改我們只需要模擬一下移動操作中對標籤的修改就能模擬出這個過程。這裏不贅述。直接放代碼:
注意:兩個盒子相鄰與不相鄰的交換操作有區別,需要分別討論。
/*
problem: https://vjudge.net/problem/UVA-12657
author: zxz
time:
memory:
disadvantage:
*/
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <math.h>
#include <queue>
#include <string>
#include <sstream>
#include <vector>
#include <string.h>
#include <time.h>
using namespace std;
const int maxn = 1e5 + 5;
const int INF = 1e9;
int L[maxn];
int R[maxn];
//鏈接兩個盒子 其中i爲相對左邊的盒子, j爲相對右邊的盒子
void link(int i, int j) {
R[i] = j;
L[j] = i;
}
void solve() {
int n,m,kase = 1;
while(scanf("%d %d", &n, &m) == 2 && n) {
for(int i = 0; i <= n; i++) {
link(i, i+1);
}
int op, x,y, inv = 1;
while(m--) {
scanf("%d", &op);
if(op == 4) {
inv = !inv; //懶標記,因爲反轉一個鏈表需要將所有節點的左右兩個標籤交換。
//我們直接利用一個標誌來代表是否反轉。處理的時候注意處理這種情況就行
continue;
}
scanf("%d %d", &x, &y);
if(op == 3 && R[y] == x) //如果y在x的左邊 交換xy 方便後面代碼操作。
swap(x, y);
int lx = L[x], ly = L[y];
int rx = R[x], ry = R[y];
if(op != 3 && !inv) { // reversed 如果已經反轉,左邊就是右邊 右邊就是左邊
op = 3 - op;
}
if(op == 1 && ly == x)
continue;
if(op == 2 && ry == x)
continue;
if(op == 1) {
link(lx, rx);
link(ly, x);
link(x,y);
} else if(op == 2) {
link(lx,rx);
link(x,ry);
link(y,x);
} else if(op == 3) {
if(x == ly) { //如果兩個盒子相鄰,則交換兩個盒子的操作有點區別
link(lx,y);
link(x,ry);
link(y,x);
} else {
link(lx,y);
link(y,rx);
link(ly,x);
link(x,ry);
}
}
}
long long ans = 0;
int nxt = 0;
for(int i = 1; i <= n; i++) {
nxt = R[nxt];
if(i % 2 == 1)
ans += nxt;
}
if(!inv && n % 2 == 0)
ans = (long long )n*(n+1)/2 - ans;
printf("Case %d: %lld\n", kase++, ans);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("cin.txt", "r", stdin);
freopen("cout.txt", "w", stdout);
int mao = clock();
#endif
solve();
#ifndef ONLINE_JUDGE
cerr << "Time:" << clock() - mao << "ms" << endl;
#endif
return 0;
}