1934. 移动小球

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2)。其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y右边。 指令保证合法,即X不等于Y。 例如,初始状态1,2,3,4,5,6的小球执行1 1 4后,小球1被移动到小球4的左边,即2,3,1,4,5,6。如果再执行2 3 5,结点3将会移到5的右边,即2,1,4,5,3,6。 

Input

第一行为一个整数t(0<t<10),表示测试用例个数。每个测试用例的第一行为两个整数n(1<n<=500000)和m(0<m<100000),n表示小球的个数,m为指令条数,以下m行每行为一条指令。 

Output

为每个测试用例单独输出一行,从左到右输出最后序列,每个数字后面跟一个空格。 

Sample Input

2 
6 2 
1 1 4 
2 3 5 
5 1 
2 1 5 

Sample Output

2 1 4 5 3 6  
2 3 4 5 1  
// Problem#: 1934
// Submission#: 5041862
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<algorithm>

using namespace std;

struct Node{
    int light;
    int right;
};

Node n[500050];
int main()
{
    int t, nn, m;
    int op, a, b;
    cin >> t;
    while(t--){
        cin >> nn >> m;
        for(int i=0; i<=nn; i++){
            n[i].light = i-1;
            n[i].right = i+1;
        }
        
        
        
        for(int i=0; i<m; i++){
            cin >> op >> a >> b;
            n[n[a].light].right  = n[a].right ;//a的左边的右边  = a的右边 
            n[n[a].right].light  = n[a].light ;
            if(op==1){// a在b左边  a.r = b a.l = b. l  
                n[a].right = b;
                n[a].light = n[b].light ;
                n[n[b].light].right = a;
                n[b].light = a;
            }
            else{ // a在b右边   
                n[a].light = b;
                n[a].right = n[b].right ;
                n[n[b].right ].light = a;
                n[b].right = a;
            }
        }
        
        int x = 0;
        for(int i=0; i<nn; i++){
            printf("%d ",n[x].right );
            x = n[x].right ;
        }
        printf("\n");
    }
}                                 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用tkinter实现小球上下左右移动可以采用Canvas组件,其中创建一个圆形对象作为小球,然后使用bind函数来绑定事件,最后使用move函数来实现移动。 以下是一个示例代码,实现了小球在窗口上上下左右移动: ```python import tkinter as tk class Ball: def __init__(self, canvas, x, y, r, color): self.canvas = canvas self.id = canvas.create_oval(x-r, y-r, x+r, y+r, fill=color) self.dx = 5 self.dy = 5 self.width = canvas.winfo_width() self.height = canvas.winfo_height() def move(self): pos = self.canvas.coords(self.id) if pos <= 0 or pos >= self.width: self.dx = -self.dx if pos <= 0 or pos >= self.height: self.dy = -self.dy self.canvas.move(self.id, self.dx, self.dy) class Application: def __init__(self): self.root = tk.Tk() self.root.geometry("400x400") self.canvas = tk.Canvas(self.root, bg="white") self.canvas.pack(fill="both", expand=True) self.ball = Ball(self.canvas, 50, 50, 20, "red") self.canvas.bind("<Button-1>", self.change_direction) self.root.after(30, self.animate) def animate(self): self.ball.move() self.root.after(30, self.animate) def change_direction(self, event): self.ball.dx *= -1 self.ball.dy *= -1 if __name__ == '__main__': app = Application() app.root.mainloop() ``` 在这个示例中,我们创建了一个Ball类,用于表示一个小球,其中包含了小球的坐标、半径、颜色等属性,以及move函数,用于移动小球。在Application类中,我们创建了一个Canvas对象,并在其中创建了一个小球。我们使用bind函数来绑定鼠标单击事件,当用户单击窗口时,会改变小球移动方向。最后,我们使用after函数来实现动画效果,每隔30毫秒调用一次animate函数,使得小球不断地移动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值