【省选水题集Day1】一起来AK水题吧! 题解(更新到B)

时间:2023-03-09 02:35:02
【省选水题集Day1】一起来AK水题吧! 题解(更新到B)

题目:http://www.cnblogs.com/ljc20020730/p/6937936.html

水题A:[AHOI2001]质数和分解

安徽省选OI原题!简单Dp。

一看就是完全背包求方案数!

完全背包都会打吧,原来是最优值,现在是累计值。

状态转移方程:f[j]=f[j]+f[j-w[i]],w[i]是待选质数。

理解:一个数要拆成若干素数和,等同于拆成所有该数减去一个素数差的方案数之和(而不是最优方案数)

但这么做需要初始化为0,同时用滚动数组可以减小时间和空间复杂度。

代码如下:(懒得打筛法求素数了)

const maxn=;
var w,f:array[..]of longint;
u:array[..]of boolean;
i,j,x,t:longint;
begin
w[]:=;
fillchar(u,sizeof(u),true);
inc(t);
for i:= to maxn do begin
for j:= to (i div )do
if i mod j= then begin u[i]:=false; break;end;
if u[i] then begin inc(t); w[t]:=i;end;
end;
f[]:=;
for i:= to t do
for j:=w[i] to maxn do
f[j]:=f[j]+f[j-w[i]];
while not eof do begin
readln(x);
writeln(f[x]);
end;
end.

2017-6-3 更新

水题B:[JSOI2008]完美的对称

首先要明确一点,对于给出的n组数据不是有序的。

这道题目让我们求出这n个点是否关于某一个点成对称像点。

那么需要贪心求解,具体做法如下。

对n组数据的x坐标y坐标分别为第一第二关键字排序。

首尾元素的x的平均值记为待定中心像点x坐标x

首尾元素的y的平均值记为待定中心像点y坐标y

这样待定中心像点坐标出来了。

接下来判断剩余(n/2-1)组是否关于该中心像点对称,操作如上。

如果判断为true,那么中心像点就是所求的点

否则,人物站在危险的地方,按题目输出。

程序:

type rec=record
x,y:longint;
end;
var a:array[..]of rec;
x,y:double;
n,i:longint ;
procedure qsort(l,r:longint);
var t:rec;
midx,midy,i,j:longint;
begin ;
i:=l;j:=r;
midx:=a[(l+r)div ].x;
midy:=a[(l+r)div ].y;
repeat
while (a[i].x<midx)or((a[i].x=midx)and(a[i].y<midy))do inc(i);
while (a[j].x>midx)or((a[j].x=midx)and(a[j].y>midy))do dec(j);
if i<=j then begin
t:=a[i]; a[i]:=a[j]; a[j]:=t;
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
begin
readln(n);
for i:= to n do readln(a[i].x,a[i].y);
qsort(,n);
x:=(a[].x+a[n].x)/;
y:=(a[].y+a[n].y)/;
for i:= to (n+) div do begin
if ((a[i].x+a[n-i+].x)/<>x) or((a[i].y+a[n-i+].y)/<>y) then begin
writeln('This is a dangerous situation!');
halt;
end;
end;
writeln('V.I.P. should stay at (',x::,',',y::,').');
end.

2017-6-10 更新