fork(2) download
  1. program ideone;
  2. var
  3. AList, BList: TArray<Integer>;
  4. APairs, BPairs: TArray<TPoint>;
  5. ia, ib, Res, CntA, CntB: Integer;
  6. s: string;
  7. begin
  8. AList := [1,2,2,3,4,5,6,7];
  9. BList := [1,5,2,3,4,7,5,7];
  10. // AList := [1,2,2,3];
  11. // BList := [2,2,2,3];
  12. SetLength(APairs, Length(AList));
  13. SetLength(BPairs, Length(BList));
  14.  
  15. for ia := 0 to High(AList) do
  16. if Odd(ia) then
  17. APairs[ia] := Point(AList[ia], -1)
  18. else
  19. APairs[ia] := Point(AList[ia], 1);
  20. TArray.Sort<TPoint>(APairs, TDelegatedComparer<TPoint>.Construct(
  21. function(const Left, Right: TPoint): Integer
  22. begin
  23. Result := 3 * Left.X - Left.Y - 3 * Right.X + Right.Y;
  24. end));
  25.  
  26. for ib := 0 to High(BList) do
  27. if Odd(ib) then
  28. BPairs[ib] := Point(BList[ib], -1)
  29. else
  30. BPairs[ib] := Point(BList[ib], 1);
  31. TArray.Sort<TPoint>(BPairs, TDelegatedComparer<TPoint>.Construct(
  32. function(const Left, Right: TPoint): Integer
  33. begin
  34. Result := 3 * Left.X - Left.Y - 3 * Right.X + Right.Y;
  35. end));
  36.  
  37. CntA := 0;
  38. CntB := 0;
  39. Res := 0;
  40. ia := 0;
  41. ib := 0;
  42. while (ia < Length(APairs)) and (ib < Length(BPairs)) do begin
  43. Writeln(Format('%d:%d (%d) %d:%d (%d)',
  44. [APairs[ia].X, APairs[ia].Y, CntA, BPairs[ib].X, BPairs[ib].Y, CntB]));
  45. if 3 * APairs[ia].X - APairs[ia].Y <= 3 * BPairs[ib].X - BPairs[ib].Y then
  46. begin
  47. CntA := CntA + APairs[ia].Y;
  48. s := 'move A';
  49. if APairs[ia].Y < 0 then begin
  50. Res := Res + CntB;
  51. s := s + Format(' addB %d', [CntB]);
  52. end;
  53. Inc(ia);
  54. end
  55. else begin
  56. CntB := CntB + BPairs[ib].Y;
  57. s := 'move B';
  58. if BPairs[ib].Y < 0 then begin
  59. Res := Res + CntA;
  60. s := s + Format(' addA %d', [CntA]);
  61. end;
  62. Inc(ib);
  63. end;
  64. Writeln(s);
  65. end;
  66. Writeln(Format('Result = %d', [Res]));
  67. end.
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Free Pascal Compiler version 2.6.4+dfsg-6 [2015/05/31] for i386
Copyright (c) 1993-2014 by Florian Klaempfl and others
Target OS: Linux for i386
Compiling prog.pas
prog.pas(3,23) Error: Identifier not found "TArray"
prog.pas(3,23) Error: Error in type definition
prog.pas(3,23) Fatal: Syntax error, ";" expected but "<" found
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
stdout
Standard output is empty