标签:
今天的文件夹:10月16日.zip
毕竟是第一天,题目比较简单,简单说下做法。
T1:对区间按左端点为第一关键字,右端点为第二关键字进行排序,然后计算可合并的区间,即前面区间的右端点不小于后面区间的左端点,这样合并后,新区间的右端点为二者右端点中的较大值。
T2:这题跪了一次。样例太有误导性,严重差评。题意是
询问在时间$[x,y]$内海浪高度第$K$小的单位时刻是那个时刻。 |
但由于样例太弱,错以为是
询问在时间$[x,y]$内海浪高度第$K$小的海浪高度值。 |
除了这个问题,别的都很简单了,抽出该区间(注意用传值的方式,不要改变原数组),进行一次“半快排”(每次递归调用时,只对包含第$K$个位置的一边进行操作,另半边不管),最后查找原数组中与构造出的这个数组的第$K$位相同的位置(即满足$a_j=a^{‘}_{K}$的$j$),输出。
T3:稳定版快排,加一个关键字表示读入顺序即可。注意实数的处理要求保留4位小数,最后输出时再取整。
代码:
1 program prz; 2 type 3 qujian=record 4 s,t:longint; 5 end; 6 var 7 x:array[1..50000] of qujian; 8 n:longint; 9 i,j,k:longint; 10 sum,left,right:longint; 11 procedure swap(var x,y:qujian); 12 var 13 ls:qujian; 14 begin 15 ls:=x; 16 x:=y; 17 y:=ls; 18 end; 19 function leq(a,b:qujian):boolean; 20 begin 21 if a.s<b.s then 22 exit(true); 23 if a.s>b.s then 24 exit(false); 25 if a.t<b.t then 26 exit(true) 27 else 28 exit(false); 29 end; 30 procedure qsort(l,r:longint); 31 var 32 i,j:longint; 33 p:qujian; 34 begin 35 i:=l; 36 j:=r; 37 p:=x[(l+r) div 2]; 38 repeat 39 while leq(x[i],p) do 40 inc(i); 41 while leq(p,x[j]) do 42 dec(j); 43 if i<=j then 44 begin 45 swap(x[i],x[j]); 46 inc(i); 47 dec(j); 48 end; 49 until i>j; 50 if l<j then 51 qsort(l,j); 52 if i<r then 53 qsort(i,r); 54 end; 55 begin 56 assign(input,‘prz.in‘); 57 reset(input); 58 assign(output,‘prz.out‘); 59 rewrite(output); 60 readln(n); 61 for i:=1 to n do 62 readln(x[i].s,x[i].t); 63 qsort(1,n); 64 sum:=0; 65 while sum<n do 66 begin 67 inc(sum); 68 left:=x[sum].s; 69 right:=x[sum].t; 70 while (right>=x[sum+1].s)and(sum<n) do 71 begin 72 inc(sum); 73 if right<x[sum].t then 74 right:=x[sum].t; 75 end; 76 writeln(left,‘ ‘,right); 77 end; 78 close(input); 79 close(output); 80 end. |
1 program server; 2 var 3 a,p:array[1..4000] of longint; 4 n,m:longint; 5 x,y,k:longint; 6 i:longint; 7 procedure swap(var x,y:longint); 8 var 9 ls:longint; 10 begin 11 ls:=x; 12 x:=y; 13 y:=ls; 14 end; 15 procedure halfqsort(l,r,k:longint); 16 var 17 i,j,x:longint; 18 begin 19 i:=l; 20 j:=r; 21 x:=p[(l+r) div 2]; 22 repeat 23 while p[i]<x do 24 inc(i); 25 while x<p[j] do 26 dec(j); 27 if i<=j then 28 begin 29 swap(p[i],p[j]); 30 inc(i); 31 dec(j); 32 end; 33 until i>j; 34 if (l<j)and(j>=k) then 35 halfqsort(l,j,k); 36 if (i<r)and(i<=k) then 37 halfqsort(i,r,k); 38 end; 39 function getkmax(left,right,k:longint):longint; 40 var 41 i:longint; 42 begin 43 fillchar(p,sizeof(p),0); 44 for i:=left to right do 45 p[i-left+1]:=a[i]; 46 halfqsort(1,right-left+1,k); 47 for i:=left to right do 48 if a[i]=p[k] then 49 exit(i); 50 end; 51 begin 52 assign(input,‘server.in‘); 53 reset(input); 54 assign(output,‘server.out‘); 55 rewrite(output); 56 readln(n); 57 for i:=1 to n do 58 read(a[i]); 59 readln(m); 60 for i:=1 to m do 61 begin 62 readln(x,y,k); 63 writeln(getkmax(x,y,k)); 64 end; 65 close(input); 66 close(output); 67 end. |
1 program find; 2 type 3 people=record 4 nam:ansistring; 5 dis:double; 6 num:longint; 7 end; 8 var 9 n,k:longint; 10 a:array[1..200000] of people; 11 i,j:longint; 12 x,y:double; 13 c:char; 14 now:longint; 15 l,r:longint; 16 procedure swap(var x,y:people); 17 var 18 ls:people; 19 begin 20 ls:=x; 21 x:=y; 22 y:=ls; 23 end; 24 function leq(a,b:people):boolean; 25 begin 26 if a.dis<b.dis then 27 exit(true); 28 if a.dis>b.dis then 29 exit(false); 30 if a.num<b.num then 31 exit(true) 32 else 33 exit(false); 34 end; 35 procedure qsort(l,r:longint); 36 var 37 i,j:longint; 38 p:people; 39 begin 40 i:=l; 41 j:=r; 42 p:=a[(l+r) div 2]; 43 repeat 44 while leq(a[i],p) do 45 inc(i); 46 while leq(p,a[j]) do 47 dec(j); 48 if i<=j then 49 begin 50 swap(a[i],a[j]); 51 inc(i); 52 dec(j); 53 end; 54 until i>j; 55 if l<j then 56 qsort(l,j); 57 if i<r then 58 qsort(i,r); 59 end; 60 begin 61 assign(input,‘find.in‘); 62 reset(input); 63 assign(output,‘find.out‘); 64 rewrite(output); 65 readln(n,k); 66 for i:=1 to n do 67 begin 68 read(c); 69 a[i].nam:=‘‘; 70 while c<>‘ ‘ do 71 begin 72 a[i].nam:=a[i].nam+c; 73 read(c); 74 end; 75 readln(x,y); 76 a[i].dis:=trunc(sqrt(x*x+y*y)*10000)/10000; 77 a[i].num:=i; 78 end; 79 qsort(1,n); 80 a[n+1].dis:=a[n].dis; 81 now:=0; 82 for i:=1 to k-1 do 83 begin 84 inc(now); 85 while (a[now].dis=a[now+1].dis)and(now<=n) do 86 inc(now); 87 if now>n then 88 begin 89 writeln(‘555...‘); 90 close(input); 91 close(output); 92 halt; 93 end; 94 end; 95 inc(now); 96 write(trunc(a[now].dis)); 97 l:=now; 98 r:=l; 99 while (a[r].dis=a[r+1].dis)and(r<n) do 100 inc(r); 101 writeln(‘ ‘,r-l+1); 102 for i:=l to r do 103 writeln(a[i].nam); 104 close(input); 105 close(output); 106 end. |
标签:
原文地址:http://www.cnblogs.com/changke/p/4886063.html