USACO Section 1.3 题解

 都是锻炼代码能力,简单的贪心,模拟,暴力枚举....

 

----------------------------------------------------------------------------美丽的分界线-------------------------------------------------------------------------

 

 

USACO 1.3.1 Mixing Milk 混合牛奶

USACO的题解有O(n)的算法   orz下   很好很强大  程序也很短

 

program milk(input,output);
var
  n,m,i,ans:longint;
  a:array[1..2,1..5000] of longint;
procedure init;
 begin
   assign(input,'milk.in');
   assign(output,'milk.out');
   reset(input);
   rewrite(output);
 end;
procedure outit;
 begin
   close(input);
   close(output);
 end;
procedure qsort(l,r:longint);
 var
   x,i,j,t :longint;
 begin
   x:=a[1,(l+r) div 2];
   i:=l;j:=r;
   repeat
      while a[1,i]<x do inc(i);
      while a[1,j]>x do dec(j);
      if i<=j then
        begin
          t:=a[1,i]; a[1,i]:=a[1,j]; a[1,j]:=t;
          t:=a[2,i]; a[2,i]:=a[2,j]; a[2,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
  init;
  readln(n,m);
  for i:=1 to m do readln(a[1,i],a[2,i]);
  qsort(1,m);
  for i:=1 to m do
    begin
      if n=0 then break;
      if a[2,i]<=n then
        begin
          inc(ans,a[1,i]*a[2,i]);
          dec(n,a[2,i]);
        end
                   else
        begin 
          inc(ans,a[1,i]*n);
          n:=0;
        end;
    end;
  writeln(ans);
  outit;
end.


 

 

USACO 1.3.2 Barn Repair 修理牛棚

 

先用一块大木板把所有门都封了,从中割掉(n-1)块(也就是没牛的地方)

 

program barn1(input,output);
var
  m,s,c,i,j,ans:longint;
  a:array[1..2,1..200] of longint;
procedure init;
 begin
   assign(input,'barn1.in');
   assign(output,'barn1.out');
   reset(input);
   rewrite(output);
 end;
procedure outit;
 begin
   close(input);
   close(output);
 end;
procedure qsort(k,l,r:longint);
 var
   x,i,j,t :longint;
 begin
   x:=a[k,random(r-l+1)+l];
   i:=l;j:=r;
   repeat
     while a[k,i]<x do inc(i);
     while a[k,j]>x do dec(j);
     if i<=j then
       begin
         t:=a[k,i]; a[k,i]:=a[k,j]; a[k,j]:=t;
	 inc(i);dec(j);
       end;
   until i>j;
   if l<j then qsort(k,l,j);
   if i<r then qsort(k,i,r);
 end;
begin
  init;
  readln(m,s,c);
  for i:=1 to c do  readln(a[1,i]);
  qsort(1,1,c);
  ans:=a[1,c]-a[1,1]+1;
  for i:=2 to c do   a[2,i]:=a[1,i]-a[1,i-1]-1;
  qsort(2,1,c);
  for i:=1 to m-1 do   dec(ans,a[2,c-i+1]);
  writeln(ans);
  outit;
end.


 

 

USACO 1.3.3 Calf Flac

字符回文有奇数个或偶数个  要分情况枚举

 

program calfflac(input,output);
var
  len,i,max,left,right,
         t ,  x ,  y  ,mx,my:longint;
  a:array[0..25000] of char;
procedure init;
 begin
   assign(input,'calfflac.in');
   assign(output,'calfflac.out');
   reset(input);
   rewrite(output);
 end;
procedure outit;
 begin
   close(input);
   close(output);
 end;
begin
  init;
  while not eof do begin inc(len);read(a[len]); end;
  for i:=1 to len-1 do
    if (a[i] in ['A'..'Z'])or(a[i] in ['a'..'z']) then
      begin
                                               //回文字符数为奇时
        t:=1;x:=i;y:=i;
        while upcase(a[x])=upcase(a[y]) do
          begin
            if x<>y then inc(t,2);
            mx:=x;my:=y;
            dec(x);inc(y);
            while(x>0)and(not((a[x] in ['A'..'Z'])or(a[x] in ['a'..'z'])))do dec(x);
            while(y<=len)and(not((a[y] in ['A'..'Z'])or(a[y] in ['a'..'z'])))do inc(y);
          end;
        if t>max then begin left:=mx;right:=my; max:=t;end;
                                             //回文字符数为偶时
        t:=0;x:=i;y:=i+1;
        while(y<=len)and(not((a[y] in ['A'..'Z'])or(a[y] in ['a'..'z'])))do inc(y);
        while upcase(a[x])=upcase(a[y]) do
          begin
            inc(t,2);
            mx:=x;my:=y;
            dec(x);inc(y);
            while(x>0)and(not((a[x] in ['A'..'Z'])or(a[x] in ['a'..'z']))) do dec(x);
            while(y<=len)and(not((a[y] in ['A'..'Z'])or(a[y] in ['a'..'z'])))do inc(y);
          end;
        if t>max then begin left:=mx;right:=my; max:=t;end;
      end;
  writeln(max);
  for i:=left to right do write(a[i]);
  writeln;
  outit;
end.


 

 

USACO 1.3.4 Prime Cryptarithm 牛式

 

纯暴力...

 

program crypt1(input,output);
var
  s:set of 0..9;
  n,i,j,k,h,t,num,group1,group2,ans:longint;
  f:array[0..10] of longint;
procedure init;
 begin
   assign(input,'crypt1.in');
   assign(output,'crypt1.out');
   reset(input);
   rewrite(output);
 end;
procedure outit;
 begin
   close(input);
   close(output);
 end;
function ok(a:longint):boolean;
 var
   i:longint;
 begin
   while a>0 do
     begin
       i:=a mod 10;
       a:=a div 10;
       if not(i in s) then exit(true);
     end;
   exit(false);
 end;
begin
  init;
  readln(n);
  s:=[];
  for i:=1 to n do begin read(f[i]);s:=s+[f[i]];end;
  for i:=1 to n do
    if f[i]<>0 then
      for j:=1 to n do
        for k:=1 to n do
          begin
            num:=f[i]*100+f[j]*10+f[k];
              for h:=1 to n do
                if f[h]<>0 then
                  begin
                    group1:=f[h]*num;
                    if group1>999 then continue;
                    if ok(group1) then continue;
                    for t:=1 to n do
                      begin
                        group2:=f[t]*num;
                        if group2>999 then continue;
                        if ok(group2) then continue;
                        if group1*10+group2>9999 then continue;
                        if ok(group1*10+group2) then continue;
                        inc(ans);
                      end;
                  end;
          end;             
  writeln(ans);
  outit;
end.


 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值