T1
题目描述
经过六年的努力,小明终于被一所知名中学录取。优秀的小明总是对一些奇奇怪怪的事情感兴趣,这次他想知道谁在这所新学校拥有的朋友最多,由于大家都才刚报到,所以小明只知道大家两两之间是否是朋友关系。
思路:直接统计个数。。。
代码:
var a:array[-10..10011,-10..10011] of integer;
n,m,i,x,y,ans,maxn:longint;
begin
assign(input,'friend.in');
assign(output,'friend.out');
reset(input);
rewrite(output);
read(n,m);
for i:=1 to m do
begin
read(x,y);
inc(a[x,y]); inc(a[y,x]);
inc(a[x,0]); inc(a[y,0]);
end;
for i:=1 to n do
begin
if a[i,0]>ans then
begin
ans:=a[i,0];
maxn:=i;
end;
end;
for i:=1 to n do if a[maxn,i]<>0 then write(i,' ');
close(input);
close(output);
end.
T2
题目描述
在统计完朋友情况之后,小明又对大家的毕业学校产生兴趣,但是他觉得单纯的统计人数是一件非常无聊的事情,于是他设计了一个算法,同一所学校毕业的学生,第1个将获得1分,第2个获得2分,第3个获得4分…,第i个将获得2^i-1分,总分就是这所小学的得分,小明想知道得分最高的学校有多少分。
思路:
并查集。
注意用路径压缩。
代码:
uses math;
var ans,f,a:array[0..10011] of longint;
n,m,x:longint;
function getfather(x:longint):longint;
begin
if f[x]=0 then exit(x);
getfather:=getfather(f[x]);
f[x]:=getfather;
end;
procedure cheng;
var t,i,j:longint;
begin
ans[1]:=1;
t:=1;
for i:=1 to x do
begin
for j:=1 to t do
begin
ans[j]:=ans[j]*2;
end;
for j:=1 to t do
begin
ans[j+1]:=ans[j+1]+ans[j] div 10;
ans[j]:=ans[j] mod 10;
end;
if ans[t+1]>0 then inc(t);
if t>100 then t:=100;
end;
ans[1]:=ans[1]-1;
for i:=t downto 1 do write(ans[i]);
end;
procedure main;
var i,y:longint;
begin
read(n,m);
for i:=1 to m do
begin
read(x,y);
if x>y then f[x]:=y else f[y]:=x;
end;
for i:=1 to n do
begin
inc(a[getfather(f[i])]);
end;
x:=0;
for i:=1 to n do x:=max(x,a[i]);
inc(x);
cheng;
end;
begin
assign(input,'score.in');
assign(output,'score.out');
reset(input);
rewrite(output);
main;
close(input);
close(output);
end.
T3
题目描述
小明迷恋上了一个新的跳棋游戏,游戏规则如下:棋盘是一排从0开始,顺序编号的格子,游戏开始时你位于0号格子,你每次只能往编号大的格子跳,而且你每次至少需要跳过L个格子,至多只能跳过R个格子。每个格子都有一个给定的伤害值,显然你希望得到的伤害值越少越好。
你能告诉小明他当他跳到最后一个格子时受到的累积伤害值最小为多少吗?
如果无论如何小明都无法跳到最后一个格子,这个时候你需要输出”-1”。
注:从i号格子跳过x个格子表示从i号格子跳到第i+x+1号格子。
思路:明显用dp。用单调队列维护,不然TLE。
代码:
uses math;
var a,f:array[-10..1000010] of int64;
n,l,r,i,j:longint;
begin
assign(input,'jump.in');
assign(output,'jump.out');
reset(input);
rewrite(output);
read(n,l,r);
if r>=n then r:=n-1;
for i:=1 to n do begin read(a[i]); f[i]:=maxlongint+1; end;
for i:=0 to n-l-1 do
for j:=l to min(r,n-i-1) do f[i+j+1]:=min(f[i+j+1],f[i]+a[i+j+1]);
if f[n]>=maxlongint then write(-1) else
write(f[n]);
close(input);
close(output);
end.
T4
在跳棋游戏大获全胜后,小明就开始一个人在校园里溜达了。突然他在校园角落里发现了一面神奇的墙壁,墙壁上有一排钉子,每个钉子上都挂着一根两端系有小球的绳子,如下图所示
小明可以调整每一根绳子在钉子左右两端的长度,当来自不同绳子的相邻小球高度一样时(具体可见样例说明),便可获得积分1分。当小明的方案获得最高积分时,迷宫大门就会开启,小明就可以进去寻找宝藏啦!
思路:
贪心,锦里将绳子与上一个匹配。如果long<l 则无法匹配。
代码:
uses math;
var l,r,n,i,ans,x,y,t:longint;
a:array[-10..500011] of longint;
begin
assign(input,'door.in');
assign(output,'door.out');
reset(input);
rewrite(output);
read(n);
for i:=1 to n do begin read(x,y); a[i]:=x+y; end;
l:=0; r:=a[1];
for i:=2 to n do
begin
{if (a[i]>=l)and(a[i]<=r) then
begin
inc(ans);
t:=r; r:=a[i]-l;
l:=a[i]-t;
end else}
if a[i]<l then
begin
l:=0; r:=a[i];
end else
begin
t:=l; l:=a[i]-r;
if l<0 then l:=0; r:=a[i]-t;
inc(ans);
end;
end;
write(ans);
close(input);
close(output);
end.