【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 = xf;
}
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;
}