• UVa 12657 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 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

    思路:采用双向链表:用left[i]和right[i]分别表示编号为i的盒子左边和右边的盒子编号(如果是0,表示不存在),再使用link函数将两个节点连接

    void link(int L, int R)
    {
    	Right[L] = R; Left[R] = L;
    }
    

    但操作4需另外讨论,为避免一次修改所有元素的指针,可增加一标记来表示是否执行过4.

    此题与CSUOJ上的一题比较相似  点击打开链接
    #include<stdio.h>
    #include<algorithm>
    #include<iostream>
    #include<string.h>
    using namespace std;
    #define MAXN 100010
    int Left[MAXN], Right[MAXN];
    int m, n, cas;
    
    void link(int L, int R)
    {
    	Right[L] = R; Left[R] = L;
    }
    int main()
    {
    	while (cin >> n >> m)
    	{
    		for (int i = 1; i <= n; i++)
    		{
    			Left[i] = i - 1;
    			Right[i] = (i + 1) % (n + 1);
    		}
    		Right[0] = 1; Left[0] = n;
    
    		int op, x, y, inv = 0;
    		while (m--)
    		{
    			cin >> op;
    			if (op == 4)
    				inv = !inv;
    			else
    			{
    				cin >> x >> y;
    				if (op == 3 && Right[y] == x)swap(x, y);
    				if (op != 3 && inv)op = 3 - op;
    				if (op == 1 && x == Left[y])continue;
    				if (op == 2 && x == Right[y])continue;
    
    				int lx = Left[x], rx = Right[x], ly = Left[y], ry = Right[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)
    				{
    					if (Right[x] == y){
    						link(lx, y); link(y, x); link(x, ry);
    					}
    					else
    					{
    						link(lx, y); link(y, rx); link(ly, x); link(x, ry);
    					}
    				}
    			}
    		}
    
    		int b = 0;
    		long long ans = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			b = Right[b];
    			if (i % 2 == 1)
    				ans += b;
    		}
    		if (inv&&n % 2 == 0)
    			ans = (long long)n*(n + 1) / 2 - ans;
    		printf("Case %d: %lld
    ", ++cas, ans);
    	}
    	return 0;
    }

  • 相关阅读:
    方法重写
    百度地图(5)-添加标注
    百度地图(3)-添加地图控件
    百度地图(2)-初始化地图
    GIS系统开发流程
    百度地图(1)- JavaScript API V3.0 对比 JavaScript GL API 1.0
    通过QGIS下载OSM数据
    深入理解 Spring 之源码剖析IOC
    FastDFS安装教程
    FastDFS简介
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9189202.html
Copyright © 2020-2023  润新知