(本题解按洛谷数据AC)
没有pascal的题解
我来发一波
考场里10分钟做完
然后第三题打码2个多小时,结果50。。。心塞
思路很简单
将书码快排
每个人的需求码读入后,寻找合适的书
如果找到,直接输出
这里用了字符串处理,下面有不用字符串的思路,自行阅读
下面是简(fan)单(suo)的代码(考场代码):
···pascal
```cpp
var a:array[0..1001] of longint;
n,q,i,j,m,k:longint;
s1,s2:string; f:boolean;
procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
repeat
while a[i]<x do
inc(i);
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i);
j:=j-1;
end;
until i>j;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;
begin
//assign(input,'librarian.in'); reset(input);
//assign(output,'librarian.out'); rewrite(output);//考场必备哦
read(n,q);
for i:=1 to n do read(a[i]);//读入书码
sort(1,n);//快排,记得从小到大哦
for i:=1 to q do
begin
read(m,k); //一个一个读入需求码
f:=true;
for j:=1 to n do
if k<=a[j] then//寻找可用的书
begin
str(k,s1);//需求码为s1
str(a[j],s2);//书码为s2
s2:=copy(s2,length(s2)-m+1,m);//截取书码末尾
if s1=s2 then//比较
begin
writeln(a[j]);//可行直接输出
f:=false;
break;//节约时间,退出循环(不过这道题目不需要节约时间)
end;
end;
if f then writeln(-1);//不可行输出-1
end;
//close(input); close(output);//考场必备哦
end.
···
```