多米诺骨牌
Time Limit:10000MS Memory Limit:65536K
Total Submit:232 Accepted:75
Case Time Limit:1000MS
Description
Input
输入文件的第一行是一个正整数n(1≤n≤1000),表示多米诺骨牌数。接下来的n行表示n个多米诺骨牌的点数。每行有两个用空格隔开的正整数,表示多米诺骨牌上下方块中的点数a和b,且1≤a,b≤6。
Output
输出文件仅一行,包含一个整数。表示求得的最小旋转次数。
Sample Input
4
6 1
1 5
1 3
1 2
Sample Output
1
-
var a,b:array[1..1000] of longint; f:array[-6001..6001] of longint; n,i,j,max1,max2,maxn:longint; function min(a,b:longint):longint; begin if a<b then exit(a) else exit(b); end; function max(a,b:longint):longint; begin if a<b then exit(b) else exit(a); end; begin readln(n); for i:=1 to n do readln(a[i],b[i]); for i:=1 to n do begin max1:=max1+a[i]; max2:=max2+b[i]; end; for i:=1 to n do maxn:=maxn+max(a[i],b[i]); for i:=-6001 to 6001 do f[i]:=maxlongint-1; f[max1-max2]:=0; for i:=1 to n do if a[i]>b[i] then for j:=-maxn to maxn do f[j]:=min(f[j],f[j+2*(a[i]-b[i])]+1) else for j:=maxn downto -maxn do f[j]:=min(f[j],f[j+2*(a[i]-b[i])]+1); for i:=0 to maxn do begin if f[i]<>maxlongint-1 then break; if f[-i]<>maxlongint-1 then break; end; write(min(f[i],f[-i])); end.