Đề bài: http://vn.spoj.com/problems/VOSMAXK/
Thuật toán:
- Code mình viết bên dưới rất dễ hiểu các bạn có thể tham khảo
Code:
const inp='';
out='';
maxn=1000000;
var a:array[1..maxn] of longint;
n,k,i:longint;
res,tmp,hold:int64;
begin
assign(input,inp); reset(input);
assign(output,out); rewrite(output);
readln(n,k);
for i:=1 to n do read(a[i]);
res:=0; hold:=0;
for i:=1 to n do
begin
if a[i]=k then
begin
res:=res+(i-hold);
tmp:=i-hold;
end
else
if a[i]>k then begin hold:=i; tmp:=0; end
else res:=res+tmp;
end;
writeln(res);
close(input); close(output);
end.
Speak Your Mind