【SSL】20210816B

【SSL】20210816B


题目描述

有10个小球,小球的编号从0~9。
初始状态按照从左到右编号为0,1,2,3…9的顺序摆在了桌子上,

有一个长度大小为n的操作序列。
操作序列的每一行表示一次操作都有两个非负整数a,b,
表示本次操作将会交换位置a,b的2个小球(下标从0编号)

一共有m次询问,
每次询问时,将杯子中的小球重置为初始状态。
给出[li,ri],连续从操作li进行到ri,
连续操作完后依次回答位置0到9,对应位置的小球编号。

鉴于输出量太大,会出事,采用输入输出优化,
void read(int &x) {
int f = 1; x = 0; char s = getchar();
while (s < ‘0’ || s > ‘9’) { if (s == ‘-’) f = -1; s = getchar(); }
while (s >= ‘0’ && s <= ‘9’) { x = x10+(s-‘0’); s = getchar(); }
x = x
f;
}

void write(int x) {
if (x < 10) { putchar(‘0’+x); return; }
write(x/10);
putchar(‘0’+x%10);
}

输入格式

给出n,m
接下来n行,给出第i个操作交换小球的位置a ,b
接下来m行对应m个询问,给出li,ri,回答询问

输出格式

m行,每行对应一个询问

输入样例

5 3
0 1
1 2
2 3
0 1
9 0
1 5
5 5
3 5

输出格式

9 1 3 0 4 5 6 7 8 2
9 1 2 3 4 5 6 7 8 0
9 0 3 2 4 5 6 7 8 1

解题思路

这道题由于数据过水,我直接暴力模拟过了,正解还在做。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define getchar() ((S==T&&(T=(S=buffer)+fread(buffer,1,1<<15,stdin),S==T))?EOF:*S++)
char buffer[1<<15],*S=buffer,*T=buffer;
inline int read()
{
    int m=0,n=1; 
	char ch;
	ch=getchar();
    while(ch<'0'||ch>'9')
	{  
	    if(ch=='-') 
		 n=-1;
	    ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
	    m=m*10+ch-'0';
	    ch=getchar();
	}
    return m*n;
}
void write(int x) 
{
    if(x<10) 
	{ 
	    putchar('0'+x); 
		return; 
	}
    write(x/10);
    putchar('0'+x%10);
}
int main()
{
	int a[20]={0,1,2,3,4,5,6,7,8,9},b[10010],c[10010];
	int n,m,x,y;
	n=read();m=read();
	for(int i=1;i<=n;i++)
	{
		b[i]=read();c[i]=read();
	}
	for(int i=1;i<=m;i++)
	{
		for(int j=0;j<=9;j++) a[j]=j;
		x=read();y=read();
		for(int j=x;j<=y;j++)
		 swap(a[b[j]],a[c[j]]);
		for(int j=0;j<=9;j++)
		 write(a[j]),cout<<" ";
		cout<<endl;
	}
	return 0;
} 

谢谢阅读

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值