Use a batch processing program in Windows

Source: Internet
Author: User

In progressAlgorithmWhen it comes to competition-related questions, it is easy to think of a simple algorithm that can ensure that it is completely correct, but it will time out. Efficient algorithms cannot be fully written. At this time, you can write a simple algorithm and generate a data.ProgramVerify the correctness of the efficient algorithm with a file Comparison Program.

In Windows, the FC command provides the file comparison function. Although batch processing is not as powerful as Bash in Linux, it is enough to write an automatic Comparison Program.

The following uses reverse order as an example. The question is similar to poj3067, and the reverse order is obtained after sorting. Data volume n = 300000. It is easy to think of enumeration Reverse Order pairs similar to Bubble sorting and write them first. Name it standard. Pas

 Const 
Maxn = 500000 ;
Type
Car = Record X, V: longint End ;

VaR
A:Array [ 0 .. Maxn] Of CAR;
N: longint;
I, J: longint;
Sum: longint;

Procedure Swap ( VaR A, B: Car );
VaR
Temp: car;
Begin
Temp: =;
A: = B;
B: = temp;
End ;

Begin
Readln (N );
For I: = 1 To N Do
Readln (A [I]. X, a [I]. V );
For I: = 1 To N- 1 Do
For J: = I + 1 To N Do
If A [I]. x> A [J]. x Then
Swap (A [I], a [J]);
For I: = 1 To N- 1 Do
For J: = I + 1 To N Do
If A [I]. V> A [J]. vThen
INC (SUM );
Writeln (SUM );
End .

Only by using advanced algorithms such as Merge Sorting to implement nlogn can this problem be avoided. The name is overtaking. Pas. The program uses the merge sort to sort the order, and then copies the merge sort to change it to the statistical process of reverse order.

 Const 
Maxn = 500000 ;
Type
Car = Record X, V: longint End ;

VaR
Temp,:Array [ 0 .. Maxn] Of CAR;
N: longint;
I: longint;
Sum: int64;

Procedure Swap ( VaR A, B: Car );
VaR
Temp: car;
Begin
Temp: =;
A: = B;
B: = temp;
End ;

Procedure Qsort (L, R: longint );
VaR
I, j, M: longint;
Begin
I: = L; J: = r; M: = A [(L + r) Div 2 ]. X;
Repeat
While (A [I]. x <m) Do INC (I );
While (A [J]. x> m) Do Dec (j );
If I <= J Then
Begin
Swap (A [I], a [J]);
INC (I );
Dec (j );
End ;
Until I> J;
If L <j Then
Qsort (L, J );
If I <r Then
Qsort (I, R );
End ;

Procedure Merge (left, mid, right: longint );
VaR
I, j, P, K: longint;
Begin
K: = left;
I: = left;
J: = Mid + 1 ;
While (I <= mid) And (J <= right) Do
Begin
If A [I]. x <= A [J]. x Then
Begin
Temp [k]: = A [I];
INC (I );
INC (k );
End
Else Begin
Temp [k]: = A [J];
INC (j );
INC (k );
End ;
End ;
For P: = I To Mid Do
Begin
Temp [k]: = A [p];
INC (k );
End ;
For P: = J To Right Do
Begin
Temp [k]: = A [p];
INC (k );
End ;
For I: = left To Right Do
A [I]: = temp [I];
End ;

Procedure Merge_sort (left, right: longint );
VaR
Mid: longint;
Begin
If Left> = right Then
Exit;
Mid: = (left + right) SHR1 ;
Merge_sort (left, mid );
Merge_sort (Mid + 1 , Right );
Merge (left, mid, right );
End ;



Procedure Merge2 (left, mid, right: longint );
VaR
I, j, P, K: longint;
Begin
K: = left;
I: = left;
J: = Mid + 1 ;
While (I <= mid) And (J <= right) Do
Begin
If A [I]. v <= A [J]. v Then
Begin
Temp [k]: = A [I];
INC (I );
INC (k );
End
Else Begin
INC (sum, mid-I + 1 );
Temp [k]: = A [J];
INC (j );
INC (k );
End ;
End ;
For P: = I To Mid Do
Begin
Temp [k]: = A [p];
INC (k );
End ;
For P: = J To Right Do
Begin
Temp [k]: = A [p];
INC (k );
End ;
For I: = left To Right Do
A [I]: = temp [I];
End ;

Procedure Merge_sort2 (left, right: longint );
VaR
Mid: longint;
Begin
If Left> = right Then
Exit;
Mid: = (left + right) SHR 1 ;
Merge_sort2 (left, mid );
Merge_sort2 (Mid + 1 , Right );
Merge2 (left, mid, right );
End ;


Procedure Select_sort;
VaR
I, J: longint;
Begin
For I: = 1 To N- 1 Do
For J: = I + 1 To N Do
If A [I]. x> A [J]. x Then
Swap (A [I], a [J]);
End ;

Begin

Readln (N );
For I: = 1 To N Do
Begin
Readln (A [I]. X, a [I]. V );
End ;
Merge_sort ( 1 , N );
Merge_sort2 (1 , N );
Writeln (SUM );


End .

Then write a data generator. The data created here is simple. Just generate a random number.

 
Const
INF =199303170;
Maxn =3000;

VaR
I, n: longint;
Begin
Randomize;
N: = random (maxn );
Writeln (N );
ForI: =1 ToNDo
Writeln (random (INF ),'', Random (INF ));
End.

Then, write a batch of processes and automatically redirect the input and output of these three programs from the screen navigation keyboard to the file. Then, compare the data generated by standard.exe and overtaking.

 
: Loop
Make.exe> data.txt
Standard.exe <data.txt> std.txt
Overtaking.exe <data.txt> ans.txt
FC std.txt ans.txt
If Errorlevel1Goto End
GotoLoop
: End

The command line interface keeps prompting you that you cannot find the file difference, and you can submit it with confidence, provided that your simple algorithm is correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.