第十五题 UVA12657 移动盒子 Boxes in a Line

116 篇文章 1 订阅

PDF

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 n
from 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

链表的第二份例题,比上一个好理解多了…就是开两个链表罢了

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define Maxn 100005
using namespace std;
int lef[Maxn],rig[Maxn];

inline void Link (int L,int R) {
	lef[R] = L; rig[L] = R;
}

/*
双向链表 分别存储左边是谁,右边是谁 
Link函数的想法  参考紫书 刘老师的处理   
我本来每次都在程序里面写 lef和rig的交换  那样倒是不错,不过太麻烦了................... 
*/

int main(int argc,char *argv[])  {
	int n,m,op,x,y,Cas = 0;
	bool flag = 0;
	while(scanf("%d %d",&n,&m) == 2) {
		flag = 0;
		for(int i=1; i<=n; i++) 
			lef[i] = i-1,rig[i] = i + 1;
		rig[n] = 0; rig[0] = 1;lef[0] = n;
		while(m--) {
			scanf("%d",&op);
			if(op == 4) flag = !flag;
			else {
				scanf("%d %d",&x ,&y);
				if(op == 3 && rig[y] == x) swap(x,y);
				if(op != 3 && flag) op = 3 - op ;
				if(op == 1 && rig[x] == y) continue;//x就在y的左侧 
				if(op == 2 && rig[y] == x) continue;//x就在y的右侧 
				int LX = lef[x],RX = rig[x],LY = lef[y],RY = rig[y];
				if(op == 1) {
					Link(LX,RX); Link(LY,x);Link(x,y);
				}
				else if(op == 2) {
					Link(LX,RX); Link(y,x); Link(x,RY);
				}
				else if(op == 3) {// 交换xy的位置 
					if(lef[y] == x){ Link(LX,y); Link(y,x); Link(x,RY); }//在交换之前 x。y是相邻的 
					else {
						Link(LX,y); Link(y,RX);
						Link(LY,x); Link(x,RY);
					}
				}
			}
		}
		int num = 0;
		long long Ans = 0;
		for(int i=1; i<=n; i+=2) {
			num = rig[num];
			Ans += num;
			num = rig[num];
		}
		if(flag && n % 2 == 0) Ans = (long long)n * (n+1) / 2 - Ans;
		// n是奇数时 整条链反转多少次都没有用 原来奇数位置的还在奇数位置上
		//n 是偶数时 整条链翻转奇数次(flag == true)原来奇数位置上的元素菜有所改变 
		printf("Case %d: %lld\n",++Cas,Ans);
	}
	
	
	
	return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define Maxn 100005
#define LL long long
using namespace std;

int lef[Maxn],rig[Maxn];

inline void Link(int L,int R) { lef[R] = L; rig[L] = R; }

int main(int argc,char *argv[]) {
    int n,m,kase = 0;
    while(scanf("%d %d",&n,&m) == 2) {
        for(int i=1; i<=n; i++){
            lef[i] = i - 1; rig[i] = i + 1;
        }
        rig[n] = 0; lef[0] = n; rig[0] = 1;
        int op,X,Y,flag = 0;
        while(m--) {
            scanf("%d",&op);
            if(op == 4) flag = !flag;
            else {
                scanf("%d %d",&X,&Y);
                if(op == 3 && rig[Y] == X) swap(X,Y);// 方便下面统一处理op==3的情况
                if(op != 3 && flag) op = 3 - op;
                if(op == 1 && lef[Y] == X) continue;
                if(op == 2 && rig[Y] == X) continue;
                int LX = lef[X],RX = rig[X],LY = lef[Y],RY = rig[Y];
                if(op == 1) { Link(LX,RX); Link(LY,X); Link(X,Y); }
                else if(op == 2){ Link(LX,RX); Link(Y,X); Link(X,RY); }
                else {
                    if(rig[X] == Y) { Link(LX,Y); Link(Y,X); Link(X,RY); }
                    else { Link(LX,Y); Link(Y,RX); Link(LY,X); Link(X,RY); }//x,y本来不相邻
                }
            }
        }

        int Num = 0;
        LL Ans = 0;
        for(int i=1; i<=n; i++) {
            Num = rig[Num];
            if(i % 2 == 1) Ans += Num;
        }
        if(flag && n % 2 == 0) Ans = (LL)n*(n + 1)/2 - Ans;
        printf("Case %d: %lld\n",++kase,Ans);
    }



    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值