很久之前WA的一道题目,现在翻出来,虽然还是不能理解题目所描述的一个位置到另一个位置是怎么回事,但是可以模拟出他所给出的描述中的样例和样例中的样例了。然后竟然过了,真是不容易。
描述里面给出的样例:
Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8
If the following queue operations were applied:
2 to 6; 6 to 3; 4 to 5; 5 to 2; 7 to 4; 8 to 1
then the resulting queue would be:
Item8 Item5 Item6 Item7 Item4 Item2 Item1 Item3
最初状态:
Item :1 2 3 4 5 6 7 8
position:X X X X X X X X
执行完之后:
Item :1 X 3 X X X X X
position:8 5 6 7 4 2 X X
这样,把 Item 中没有被定位的 Item 顺次插入Position 中就可以得到结果了。
于是,代码:
#include <iostream>
using namespace std;
int p[25], t, n, q, a, b, i, j;
string s[25];
int main()
{
ios::sync_with_stdio(false);
cin >> t;
while (t --)
{
cin >> n >> q;
for (i = 1; i <= n; ++ i)
{
cin >> s[i];
p[i] = 0;
}
while (q --)
{
cin >> a >> b;
p[a] = b;
}
for (i = 1; i <= n; ++ i)
{
for (j = 1; j <= n; ++ j)
{
if (p[j] == i)
{
cout << s[j];
break;
}
}
if (j == n+1)
{
for (j = 1; j <= n; ++ j)
{
if (p[j] == 0)
{
cout << s[j];
p[j] = -1;
break;
}
}
}
if (i == n) cout << endl;
else cout << " ";
}
}
}