记录编号 21754 评测结果 AAAAAAAAAA
题目名称 矩形分割 最终得分 100
用户昵称 Gravatarybh 是否通过 通过
代码语言 Pascal 运行时间 0.006 s
提交时间 2010-11-15 11:45:38 内存使用 0.14 MiB
显示代码纯文本
  1. {矩形分割
  2. 贪心算法
  3. Author: yangbohua
  4. Time: 2010-11-15 09:13}
  5.  
  6. program cut;
  7. var
  8. a:array[1..4000] of longint;
  9. b:array[1..4000] of integer;
  10. n,m,i,x,y,ans:longint;
  11.  
  12. procedure sort(l,r:longint);
  13. var
  14. i,j,mid,temp:longint;
  15. begin
  16. i:=l;
  17. j:=r;
  18. mid:=a[(l+r) div 2];
  19. repeat
  20. while a[i]>mid do
  21. inc(i);
  22. while mid>a[j] do
  23. dec(j);
  24. if not(i>j) then
  25. begin
  26. temp:=a[i]; a[i]:=a[j]; a[j]:=temp;
  27. temp:=b[i]; b[i]:=b[j]; b[j]:=temp;
  28. inc(i);
  29. j:=j-1;
  30. end;
  31. until i>j;
  32. if l<j then
  33. sort(l,j);
  34. if i<r then
  35. sort(i,r);
  36. end;
  37.  
  38. begin
  39. assign(input,'cut.in');
  40. reset(input);
  41. assign(output,'cut.out');
  42. rewrite(output);
  43. readln(n,m);
  44. for i:=1 to n+m-2 do
  45. begin
  46. read(a[i]);
  47. if i<=n-1
  48. then b[i]:=1
  49. else b[i]:=2;
  50. end;
  51. sort(1,n+m-2);
  52. x:=1;
  53. y:=1;
  54. for i:=1 to n+m-2 do
  55. begin
  56. if b[i]=1 then
  57. begin
  58. ans:=ans+a[i]*y;
  59. x:=x+1;
  60. end
  61. else
  62. begin
  63. ans:=ans+a[i]*x;
  64. y:=y+1;
  65. end;
  66. end;
  67. writeln(ans);
  68. close(input);
  69. close(output)
  70. end.