Test instructions: N Number of pairs (A[i],b[i])
Each time you can turn (x, y) into (x+a[i],y+b[i]) or (X+b[i],x+a[i]), which is called swapping once
Minimum switching times for abs (X-y)
n<=1000 1<=a[i],b[i]<=6
Idea: Set DP[I,J] for the first number of pairs, so that the minimum number of times x-y=j transfer can be
1 Constoo=100000;2 varDp:Array[0..1,-6100..6100] ofLongint;3B:Array[1..1100] ofLongint;4 N,i,v,j,t,ans:longint;5 6 functionmin (x,y:longint): Longint;7 begin8 ifX<y Thenexit (x);9 exit (y);Ten End; One A begin - - READLN (n); the fori:=1 toN Doread (a[i],b[i]); -v:=0; - Fillchar (dp[v],sizeof (Dp[v]), $1f); -dp[0,0]:=0; + fori:=0 toN-1 Do - begin +v:=1-v; Fillchar (dp[v],sizeof (Dp[v]), $1f); A forj:=-5*i to 5*i Do at ifdp[1-v,j]<oo Then - begin -t:=j+a[i+1]-b[i+1]; -Dp[v,t]:=min (dp[v,t],dp[1-v,j]); -t:=j+b[i+1]-a[i+1]; -Dp[v,t]:=min (dp[v,t],dp[1-v,j]+1); in End; -//Print; to End; +ans:=Oo; - fori:=0 to 5*n Do the begin *Ans:=min (Ans,min (dp[v,i],dp[v,-i])); $ ifAns<oo ThenBreak ;Panax Notoginseng End; - writeln (ans); the + End.
"Vijos1222" equivalent Ramen (DP)