Usaco第一章做完了,总结一下~
这道题就是考你会不会写程序啦,还有呢,就是 ord(c) 这个Function的运用了~
这道题细心一点,名字对应好就不会错了~
这一道题的关键就是:下一个月13号的星期数就等于(这个月13号的星期数+这个月的天数MOD 7)MOD 7
PS:我记星期天为0。
O(N)的算法。Dynamic Programming is good method for solving this problem in O(N).
If we consider two copies of the string we easy transform cyclic configuration of the necklace to linear.
Now we can compute for each breaking point how many beads of the same color can be collected on the left and on the right from the breaking point. I show how we can compute it only for the left side.
For right side it is analogical. Let r[p] and b[p] be the number of red / blue beads that can be collected, when necklace is broken in point p. If we know this and color of next bead (c) we can compute r[p+1] and b[p+1].
r[0] = p[0] = 0 If c = 'r' then r[p+1] = r[p] + 1 and b[p+1] = 0 because the length of the blue beads is 0.
if c = 'b' then b[p+1] = b[p] + 1 and r[p+1] = 0if c = 'w' then both length of the red and length of blue beads canbe longer. so r[p+1] = r[p]+1 and b[p+1] = b[p] + 1.
The number of beads that can be collected in breaking point p is then
max(left[r[p]], left[b[p]]) + max(right[r[p]], right[b[p]]).
And themaximum from this value is answer for the problem.
PS:我是看了标程写出来的呃~做了好久,原来的烦死掉了,就重写了一个。先把珠子复制一遍~
先按起始时间进行快速排序,然后按顺序判断时间段
if t1[i]<=r then
r:=max(t2[i],r)
t1[i],t2[i]为每一位工人的起始结束时间。
l、r为时间段的起始结束时间。
每一个连续的时间段结束后。
a1:=max(a1,r-l);
a2:=max(a2,t1[i]-r);
l:=t1[i];
r:=t2[i];
a1为最长的连续工作时间;
a2为最长的空闲时间;
最后再执行一次求最长的连续工作时间 a1:=max(a1,r-l);
完成~
这道题,好奇怪厄~主程序都一样,原来的就是提交不通过~呃~郁闷的~Procedure N4 为翻转Procedure N1 为顺时针90'最后再用 function bj 来判断两图案是否相同~
procedure N1(l:arr2; var m:arr2);
var i,j:integer;
begin
for i:=1 to n do
for j:=1 to n do
m[j,n-i+1]:=l[i,j];
end;
function bj(a,b:arr2):boolean;
var i,j:integer;
begin
equal:= true;
for i:=1 to n do
for j:=1 to n do
if a[i,j] <> b[i,j] then begin
equal:=false; exit;
end;
end;
掌握好文件的读取写入操作,逐个读入字典中的人名,并转换成数字,与起始的数字比较,相同就输出,全部用字符串,不知道为什么我用int64和longint都不可以。厄~一开始想把数字转换成名字作但复杂度为O(3^N),太复杂,后来看了别人的报告,发觉用字典好~
s:array['A'..'Z'] of char=('2','2','2','3','3','3','4','4','4','5','5','5', '6','6','6','7','7','7','7','8','8','8','9','9','9','9');
最主要的是进制转换。Procedure ToB 然后倒转字符串若相等,即为回文数。输出。
procedure ToB (a:longint;var c:string);
var k,i:longint;
begin
k:=0;
c:='';
repeat inc(k);
i:=a mod b;
a:=a div b;
c:=s[i]+c;
until a=0;
end;
和上一道差不多 TOB +倒转字符串 轻松AC
我先用快排Qsort把所有数据排序,每次选最小的卖牛奶的人把他“榨干”,求出钱数~ 贪心第一题
~~~好汗的~已经是第三道关于牛的了呃~
第一次数据太弱,程序完全错掉还AC3个,汗
先读入全部数据,进行快排。
然后把满牛棚之间的连续的空牛棚的数量存入数组,进行快排~
舍去最大的M-1个数字~
剩下的加上C即为最终解~ 还是贪心
三个数组~a为原文c为a中的字母,转为大写存入~b为c中字母在a中对应的位置,最后输出时使用~
厄~一开始没看清题目~以为最后输出的数为回文数~汗~USACO的题目太奇怪了~做到现在不是回文数就是COW呃~思维定势~别的就没有什么叻~枚举第一行,第二行的每一种可能性,使3,4,5行都满足条件~
郁闷死了,从开学做到现在,不爽 ,Copy lhyxxh1 同学的,
这道题目数据规模很小,大可枚举4个矩形的摆放方式,然后再计算大矩形的边长。
以下用width[1..4]表示矩形的长,height[1..4]表示矩形的宽。用w表示大矩形的长,h表示大矩形的宽:
方式一:
w = width[1] + width[2] + width[3] + width[4]
h = MAX{height[1], height[2], height[3], height[4]}
方式二:
w = MAX{width[1] + width[2] + width[3], width[4]}
h = MAX{height[1], height[2], height[3]} + height[4]
方式三:
w = MAX{width[1] + width[2], width[3]} + width[4]
h = MAX{MAX{height[1], height[2]} + height[3], height[4]}
方式四:
w = width[1] + width[2] + MAX{width[3], width[4]}
h = MAX{height[1], height[2], height[3] + height[4]}
方式五:
同方式四。
方式六:
h = MAX{height[1] + height[3], height[2] + height[4]}
w = MAX: width[1] + width[2], 一般条件
width[3], 一般条件
width[4], 一般条件
width[2] + width[3], 当height[1] < height[2]时
width[1] + width[4], 当height[1] > height[2]时
width[3] + width[4], 当height[1] + height[3] > height[2]时
注意以上是把矩形1放到左下,2放到右下,3放到右上,4放到左上为例的。
枚举所有方法
还有一种Ox的
program clocks;
const
INV : array[3..12] of byte =(1, 0, 0, 2, 0, 0, 3, 0, 0, 0);
var inp, out: text;
a, b, c, d, e, f, g, h, i: integer;
ax, bx, cx, dx, ex, fx, gx, hx, ix,l: integer;
t,an: array[1..9] of integer;
begin
assign (inp, 'clocks.in'); reset (inp);
readln(inp, ax, bx, cx);
readln(inp, dx, ex, fx);
readln(inp, gx, hx, ix);
a:=inv[ax]; b:=inv[bx]; c:=inv[cx]; d:=inv[dx];
e:=inv[ex]; f:=inv[fx]; g:=inv[gx]; h:=inv[hx];
i:=inv[ix];
t[1] := ( 8 + a + 2 * b + c + 2 * d +2 * e - f + g - h ) mod 4;
t[2] := ( a + b + c + d + e + f + 2 * g + 2 * i ) mod 4;
t[3] := ( 8 + a + 2 * b + c - d + 2 * e + 2 * f -h + i ) mod 4;
t[4] := ( a + b + 2 * c + d + e + g + h + 2 * i ) mod 4;
t[5] := ( 4 + a + 2 * b + c + 2 * d - e + 2 * f + g + 2 * h+ i )
mod 4;
t[6] := ( 2 * a + b + c + e + f + 2 * g + h + i ) mod 4;
t[7] := ( 8 + a - b + 2 * d + 2 * e - f + g + 2 * h + i ) mod 4;
t[8] := ( 2 * a + 2 * c + d + e + f + g + h + i) mod 4;
t[9] := ( 8 - b + c - d + 2 * e + 2 * f + g + 2 * h+ i) mod 4;
assign(out, 'clocks.out'); rewrite(out);
for a := 1 to 9 do
for b := 1 to t[a] do Begin
inc(l);
an[l]:=a;
end;
for a:=1 to l-1 do
write(out,an[a],' ');
write(out,an[l]);
writeln(out); close(out)
end.
说实话,没看懂是怎么做的~
说实话,没看懂是怎么做的~
dp[i,j]:=max(dp[i-1,j],dp[i-1,j-1])+tree[i,j];
dp为到该节点的最大权值,tree为该点权值
动规?
判断素数
function prime(z:longint):boolean;
var
i:longint;
begin
begin
i:=2;
while (i*i<=z) and (z mod i <>0) do inc(i);
if z mod i = 0 then prime:=false else prime:=true;
end;
end;
还有就是符合条件的偶数位的数只有11(可以证明) 从三位数搜索吧(<100 的只有 5 7 11)
procedure sp(x,k:longint);
var
i:longint;
begin
if k<=n then
for i:=1 to 4 do begin
x:=x*10+y[i];
if prime(x) then sp(x,k+1);
x:=(x-y[i]) div 10;
end
else
writeln(x);
end;
x为当前数 k为当前数的长度 DFS 搜吧
经典的8皇后问题,小于11的我能通过,大的就TLE了~
就这样吧~先更新到这里了~
BY Wandsea 转载请注明~