You are on page 1of 7

1/5

.

Ti : t
. a .
, . .
, .



2/5

I.
:
[i] (1<=i<=n) |a[i]|<=10^8. (k<=n)
C (|C|<=2*(10^9)) .
.
).
)
0,1

.
Var n:byte; C:longint;
A:array[1..18]of longint;
x,bestSelection:array[1..18]of byte;
Procedure try(i:byte);
Var
j,k:byte;
Test:longint;
Begin
For j:=0 to 1 do begin
x[i]:=j;
if (i<n) then try(I+1)
else begin
test=0;
for k:=1 to n do inc(test,x[k]*a[k]);
if ((test<=C)and (test>bestsum)) then begin
//luu lai kt qua ti uu.
bestsum:=test;
bestselection:=x;
end;
end;
End;
End;
Var n:byte; bestsum,C:longint;
bestselection:longword;
A:array[1..18]of longint;
Procedure duyet;
Var I,k:byte; Test:longint; S,last:longword;
Begin
Bestsum:=0;
Bestselection:=0; // trang thi rng
last:=(1 shl n)- 1; // trang thi chon tt ca n phn tu.
For s:=1 to last do begin
Test:=0;
For i:=0 to n-1 do
inc(Test,((s shr I) and 1)*a[i+1]);
If (Test>bestsum) and (Test <=C) then begin
bestsum:= Test;
bestselection:=s;
End;
End;
End;
(n*(2^n) , ,
, th hi c
ng O(n*(2^n)) mt cch r rng nht.



3/5
.
Var n:byte;
C,bestsum:longint;
A:array[1..18]of longint;
bestselection:longword;
Procedure duyettrangthaicocchan;
Var I,k:byte;
T : array[0..1 shl 18]of longint; //Goi T[s] l gi tri cua trang thi s.
S,p,last:longword;
Begin
last:=(1 shl n)- 1; // trang thi chon tt ca n phn tu.
Bestsum:=0; Bestselection:=0; T[0]:=0; // Trang thi rng

//Tin hnh duyt cc trang thi.
For s:=1 to last do begin
For i:=0 to n-1 do //duyt tm bit 1 cua s
If (((s shr i) and 1)=1) then break;
P:= s and (not (1 shl i));
//trang thi P l trang thi S sau khi loai bo di a[i+1]. Ta d thy l p<s, v p da duoc tnh truc s.
// hay ni cch khc l p l gi tri cua S sau khi tt di bit i cua S. (tt bit vi tr i v gi tri 0)
T[s]:=T[p] + a[i+1];
If (T[s]>bestsum) and (T[s]<=C) then begin
bestsum:=t[s];
bestselection:=s;
End;
End;
End;

(i*(2^n)) i . N
. 1) i ngay
khi i=0.



4/5

II.
Xt bi ton sau: CHESSCBG (https://vn.spoj.pl/problems/CHESSCBG/)
Mt bn c th l mt bng gm 4 dng, 4 ct. Mi th c l mt cch sp xp 8 qun c, hai qun khc nhau hai khc nhau. Bi
ton t ra l cho hai th c 1 v 2, hy tm mt s t nht bc di chuyn qun chuyn t th 1 sang th 2; mt bc di chuyn
qun l mt ln chuyn qun c sang trng k cnh vi qun c ang ng.
D liu vo
T file vn bn gm 8 dng, mi dng l mt xu nh phn di 4 m s 1/0 tng ng vi v tr c hoc khng c qun c. Bn
dng u l th c 1, bn dng sau l th c 2.
D liu ra
Gm 1 dng duy nht l s bc chuyn qun t nht
V d
Input:
1111
0000
1110
0010
1010
0101
1010
0101

Output:
4

Thut ton: c v 1 dy 16bit, v chuyn thnh 1 s hi th. Sau , n hay BFS t nh ny t nh kia v
m s bi ton th ng.


5/5
III. Quy H :
sau:
. .
. *n. Output: . Sau khi
.
: (n!) . =20 i
/s y trong .

s .
:
. . .
j t j+1.
T[S S S.
{a,b,c, d i ,b,c,d.

T[ {1,2,5} = 10011= 19 :
- T[ {2,5} = 10010 =18 } + a[3,1];
- T[ {1,5} = 10001 =17 } + a[3,2];
- T[ {1,2} = 00011 = 3 } + a[3,5];
3. => T[19 .
t: T[s] = max (T[p] + a[k,j : s k = 1) j S.
[ { = (1 shl n)-1 ]. . 1 giy.
.



6/5
cho thu trn:
const
fi='maxmatrix.inp';
fo='maxmatrix.out';
maxn=20;
var n:byte; sum:int64; lasts:longword;
a:array[1..maxn,0..maxn]of longint;
status:array[0..(1 shl maxn)] of int64;
truy:array[0..(1 shl maxn)] of byte;
procedure doc;
var f:text; i,j:byte;
begin
assign(f,fi); reset(f);
readln(f,n);
for i:=1 to n do begin
for j:=1 to n do
read(f,a[i,j]);
readln(f);
end;
close(f);
end;

function bitoff(s:longword;i:byte):longword; begin
bitoff:=s and (not (1 shl i));
end;
function getbit(s:longword;i:byte):byte; begin
getbit:=(s shr i) and 1;
end;

procedure ghi;
var row:array[1..maxn]of byte;
i,j:byte; s:longword; f:text;
begin
assign(f,fo); rewrite(f);
writeln(f,sum);
s:=lasts;
{row[i] mang ngha l hng i, ta s chn ct row[i]}
for i:=n downto 1 do begin
row[i]:=truy[s];
s:=bitoff(s,truy[s]-1);
end;
for i:=1 to n do
writeln(f,a[i,row[i]],'[',i,',',row[i],']');
close(f);
end;

procedure xuli;
var
i,j,k:byte;
list1:array[1..maxn]of byte;
s,p:longword;
begin
status[0]:=0;
lasts:=(1 shl n) - 1; //trng thi hon thnh

{Tnh cc trng thi}
for s:=1 to lasts do begin
{gii m trng thi s: list1 s lu cc v tr bit=1,
k l s lng bit=1, ng thi cng l dng ta ang xt}
k:=0;
for i:=0 to n-1 do
if getbit(s,i)=1 then begin
inc(k);
list1[k]:=i;
end;

{gi s chn ct u tin}
j:=list1[1]+1;
p:=bitoff(s,j-1);
truy[s]:=j;
status[s]:=status[p]+a[k,j];

{xt cc ct cn li}
for i:=2 to k do begin
j:=list1[i]+1;
p:=bitoff(s,j-1);
if status[s]<status[p]+a[k,j] then
begin
status[s]:=status[p]+a[k,j];
truy[s]:=j;
end;
end;
end;
sum:=status[lasts];
end;

begin
doc; xuli; ghi;
end

L Do s hiu ct l 1 i din cho ct i+1, Ta c th li cc ct 0n- K , i
din cho chnh ct i.


7/5


(n*(2^n))


IV.
:
QBSELECT, QBGAME, CHESSCBG, LEM2, LEM3, MIXUP2, LASCALE, CHNREST (*), VMMTFIVE

You might also like