Đề bài: http://vn.spoj.com/problems/NKGOLF/
Thuật toán:
- (đang cập nhập)
Code:
#include
#include
#include
#include
#include
#include
#include
#include
#include
Just another WordPress site
Đề bài: http://vn.spoj.com/problems/NKGOLF/
Thuật toán:
Code:
#include
#include
#include
#include
#include
#include
#include
#include
#include
Đề bài: http://vn.spoj.com/problems/CTNOWN/
Thuật toán:
Code:
uses math;
const fi='';
fo='';
maxn=350;
var n,i,j :longint;
dem :longint;
nto :array[1..maxn] of longint;
cx :array[1..maxn] of boolean;
f :array[0..maxn,0..maxn] of qword;
t,tt,k :longint;
res,tam :qword;
procedure sangnto;
var i,j :longint;
begin
for i:=2 to trunc(sqrt(maxn)) do
if cx[i]=false then
begin
j := i*i;
while (j<=maxn) do
begin
cx[j]:=true;
j := j+i;
end;
end;
for i:=2 to maxn do
if cx[i]=false then
begin
inc(dem);
nto[dem] := i;
end;
end;
function max(x,y : qword) : qword;
var tg : qword;
begin
if x>y then exit(x) else exit(y);
end;
procedure main;
begin
assign(input,fi);reset(input);
assign(output,fo);rewrite(output);
sangnto;
read(t);
for i:=0 to dem do
for j:=0 to maxn do f[i,j] := 1;
for i:=1 to dem do
begin
for j:=1 to maxn do
begin
f[i,j] := f[i-1,j];
tam:=1;
while tam*nto[i]<=j do
begin
tam := tam * nto[i];
f[i,j] := max(f[i,j], f[i-1,j-tam]*tam );
end;
end;
end;
for tt := 1 to t do
begin
read(n);
writeln(f[dem,n]);
end;
close(input);close(output);
end;
procedure __;
begin
end;
begin
__ ;
main;
__ ;
end.
Đề bài: http://vn.spoj.com/problems/DHFRBUS/
Thuật toán:
Code:
{$inline on}
const fi ='';
fo ='';
maxN =trunc(1e5)*2;
maxM =trunc(1e5)*10;
maxK =5;
oo =2*trunc(1e12);
type Tadj =record
v,w,link:int64;
end;
THeap =record
v1,v2:int64;
end;
Arr1 =array[0..maxN] of int64;
Arr2 =array[0..maxM] of Tadj;
Arr4 =array[0..maxN*maxK] of THeap;
Arr5 =array[0..maxN,0..maxK] of int64;
var n, m, k :longint;
Adj :arr2;
Head :arr1;
s, t :longint;
d :arr5;
Pos :arr5;
H :arr4;
nHeap :longint;
c :longint;
//heapmin;
procedure hv(var a, b:int64);
var tg :int64;
begin
tg:=a;a:=b;b:=tg;
end;
procedure UpHeap(i:longint);
begin
if (i<=1) or (d[H[i div 2].v1,H[i div 2].v2]<=d[H[i].v1,H[i].v2]) then exit;
hv(Pos[H[i div 2].v1,H[i div 2].v2],Pos[H[i].v1,H[i].v2]);
hv(H[i div 2].v1,H[i].v1);
hv(H[i div 2].v2,H[i].v2);
UpHeap(i div 2);
end;
procedure DownHeap(i:longint);
var j :longint;
begin
j:=i*2;
if j>nHeap then exit;
if (j0 do
begin
v:=adj[i].v;
w:=adj[i].w;
if d[v,x]>d[u,x]+w then
begin
d[v,x]:=d[u,x]+w;
Update(v,x);
end;
if (xd[u,x]) then
begin
d[v,x+1]:=d[u,x];
Update(v,x+1);
end;
i:=adj[i].link;
end;
until nHeap=0;
ans:=oo;
ans:=d[t,k];
{for x:=0 to k do
if d[t,x]
Đề bài: http://vn.spoj.com/problems/DHLOCK/
Thuật toán:
Code:
const fi='';
fo='';
maxn=300;
var i,j,n,k:longint;
kiluc,kq,tam:longint;
a,b,c:array[1..maxn] of longint;
procedure xuly;
var kk:longint;
tmp:longint;
begin
for kk:=0 to n-1 do
begin
for i:=1 to n do
begin
if i+kk>n then tam:=(i+kk) mod n else tam:=i+kk;
if b[tam]>=a[i] then c[i]:=b[tam]-a[i]
else c[i]:=b[tam]+k-a[i]+1;
end;
for i:=1 to n do
begin
kq:=0;
tam:=c[i];
for j:=1 to n do
if c[j]>=tam then inc(kq,c[j]-tam)
else inc(kq,c[j]+k-tam+1);
if kq+kk+tam=a[i] then inc(kiluc,b[i]-a[i])
else inc(kiluc,b[i]+k-a[i]+1);
end;
begin
assign(input,fi);reset(input);
assign(output,fo);rewrite(output);
readln(n,k);
for i:=1 to n do read(a[i]);
for j:=1 to n do read(b[j]);
init;
xuly;
writeln(kiluc);
close(input);close(output);
end.
Đề bài: http://vn.spoj.com/problems/NKPALIN/
Thuật toán:
Gọi F[i][j] là độ dài xâu đối xứng dài nhất trong đoạn S[i..j]. Khi đó:
Bạn có thể đọc code bên dưới để hiểu hơn.
Code:
Program Xaucon;
Var s,st,st1:ansistring;
i,j:integer;
F: Array[1..2000,1..2000] Of Integer;
Procedure nhap;
Begin
Readln(s);
For i:=1 to length(s) do F[i,i]:=1;
Writeln;Writeln;
end;
Function max(a,b:integer):integer;
Begin
If a>b then exit(a) else exit(b);
End;
Procedure Solve;
Begin
For i:=length(s) downto 1 do
For j:=i+1 to length(s) do
If s[i]=s[j] then
F[i,j]:=F[i+1,j-1]+2
else F[i,j]:= max(F[i+1,j],F[i,j-1]);
End;
Procedure PrintResult;
Begin
i:=1;
j:=length(s);
While (i<=j) do
Begin
If s[i]=s[j] then
Begin
st:=st+s[i];
st1:=s[j]+st1;
inc(i);
dec(j);
end
Else
If F[i+1,j]=F[i,j] then inc(i) else dec(j);
End;
If F[1,length(s)] mod 2 =1 then delete(st1,1,1);
st:=st+st1;
Writeln(st);
end;
Begin
nhap;
Solve;
PrintResult;
readln
end.
đề bài: http://vn.spoj.com/problems/CREC01/
Thuật toán:
Code:
Pascal:
const fi ='';
fo ='';
oo =1000;
var a :array[0..oo,0..oo] of 0..1;
l,h,dem:array[0..oo+1] of int64;
m, n :longint;
procedure Optimize;
var i, j :longint;
ans :int64;
begin
fillchar(h, sizeof(h),0);
ans:=0;
for i:=1 to m do
begin
for j:=1 to n do
begin
if a[i,j]=1 then begin
l[j]:=j;
h[j]:=h[j]+1;
while h[l[j]-1]>h[j] do
l[j]:=l[l[j]-1];
dem[j]:=h[j]*(j-l[j]+1)+dem[l[j]-1];
ans:=ans+dem[j];
//writeln(ans,'==');
//if (i=3) and (j=2) then writeln(dem[1]);
end
else begin
dem[j]:=0;
l[j]:=0;
h[j]:=0;
end;
end;
end;
writeln(ans);
end;
procedure run;
var i, j, tg :longint;
s :ansistring;
begin
assign(input,fi);assign(output,fo);
reset(input);rewrite(output);
readln(m, n);
for i:=1 to m do
begin
readln(s);
for j:=1 to n do
val(s[j],a[i,j]);
end;
Optimize;
close(input);close(output);
end;
begin
run;
end.
C++:
using namespace std;
#include
#define BG begin()
#define ED end()
#define st first
#define nd second
#define PB push_back
#define PF push_front
#define FOR(i,a,b) for (long long i=a;i=b; i--)
#define ri(n)({\
int neg=0;\
n=0;\
char ch;\
for(ch=getchar(); ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') neg=1-neg;\
n=ch-48;\
for(ch=getchar(); ch>='0' && ch<='9'; ch=getchar()) n=(n<<3)+(n<<1)+ch-48;\
if (neg) n=-n;\
})
typedef long long ll;
typedef unsigned long long ull;
typedef pair II;
typedef pair LL;
const ll INF=1000000000+7;
const double esp=1e-13;
const double pi=3.141592653589;
ll m, n, a[1001][1001], H[1001], dem[1001], L[1001];
int main()
{
//freopen("CREC01.inp", "r", stdin);
//freopen("CREC01.out", "w", stdout);
cin>>m>>n;
char ch;
FORE(i, 1, m)
FORE(j, 1, n) {
cin>>ch;
a[i][j] = ch - '0';
}
memset(H, 0, sizeof(H));
memset(L, 0, sizeof(L));
memset(dem, 0, sizeof(dem));
long long ans = 0;
FORE(i, 1, m)
FORE(j, 1, n) {
//cout< 1) && (a[i][k-1] == 1) && (H[ k - 1 ] >= H[j]) ) k = L[k - 1];
L[j] = k;
dem[j] = H[j]*(j - L[j] + 1) + dem[L[j] - 1];
ans += dem[j];
//cout<
Copyright © 2026 · Genesis Framework · WordPress · Log in
Recent Comments