首页 > 代码库 > USACO JAN 2012 Bronze

USACO JAN 2012 Bronze

Problem 1: Gifts [Kalki Seksaria and Brian Dean, 2012]

Farmer John wants to give gifts to his N (1 <= N <= 1000) cows, using his
total budget of B (1 <= B <= 1,000,000,000) units of money.

Cow i requests a gift with a price of P(i) units, and a shipping cost of
S(i) units (so the total cost would be P(i)+S(i) for FJ to order this
gift).  FJ has a special coupon that he can use to order one gift of his
choosing at only half its normal price.  If FJ uses the coupon for cow i,
he therefore would only need to pay P(i)/2+S(i) for that cows gift. 
Conveniently, the P(i)s are all even numbers.

Please help FJ determine the maximum number of cows to whom he can afford
to give gifts.  

PROBLEM NAME: gifts

INPUT FORMAT:

* Line 1: Two space-separated integers, N and B.

* Lines 2..1+N: Line i+1 contains two space-separated integers, P(i)
        and S(i).  (0 <= P(i),S(i) <= 1,000,000,000, with P(i) even)

SAMPLE INPUT (file gifts.in):

5 24
4 2
2 0
8 1
6 3
12 5

INPUT DETAILS:

There are 5 cows, and FJ has a budget of 24.  Cow 1 desires a gift with
price 4 and shipping cost 2, etc.

OUTPUT FORMAT:

* Line 1: The maximum number of cows for whom FJ can purchase gifts.

SAMPLE OUTPUT (file gifts.out):

4

OUTPUT DETAILS:

FJ can purchase gifts for cows 1 through 4, if he uses the coupon for cow
3.  His total cost is (4+2)+(2+0)+(4+1)+(6+3) = 22.  Note that FJ could
have used the coupon instead on cow 1 or 4 and still met his budget.
A题

裸枚举

Problem 2: Haybale Stacking [Brian Dean, 2012]

Feeling sorry for all the mischief she has caused around the farm recently,
Bessie has agreed to help Farmer John stack up an incoming shipment of hay
bales.  

She starts with N (1 <= N <= 1,000,000, N odd) empty stacks, numbered 1..N.
FJ then gives her a sequence of K instructions (1 <= K <= 25,000), each of
the form "A B", meaning that Bessie should add one new haybale to the top
of each stack in the range A..B.  For example, if Bessie is told "10 13",
then she should add a haybale to each of the stacks 10, 11, 12, and 13.

After Bessie finishes stacking haybales according to his instructions, FJ
would like to know the median height of his N stacks -- that is, the height
of the middle stack if the stacks were to be arranged in sorted order
(conveniently, N is odd, so this stack is unique).  Please help Bessie
determine the answer to FJs question.

PROBLEM NAME: stacking

INPUT FORMAT:

* Line 1: Two space-separated integers, N K.

* Lines 2..1+K: Each line contains one of FJs instructions in the
        form of two space-separated integers A B (1 <= A <= B <= N).

SAMPLE INPUT (file stacking.in):

7 4
5 5
2 4
4 6
3 5

INPUT DETAILS:

There are N=7 stacks, and FJ issues K=4 instructions.  The first
instruction is to add a haybale to stack 5, the second is to add haybales
to stacks 2..4, etc.

OUTPUT FORMAT:

* Line 1: The median height of a stack after Bessie completes the
        instructions.

SAMPLE OUTPUT (file stacking.out):

1

OUTPUT DETAILS:

After Bessie is finished, the stacks have heights 0,1,2,3,3,1,0.  The median
stack height is 1, since 1 is the middle element in the sorted ordering
0,0,1,1,2,3,3.
B题

前缀和直接搞

Problem 3: Grazing Patterns [Brian Dean, 2012]

Due to recent budget cuts, FJ has downsized his farm so that the grazing
area for his cows is only a 5 meter by 5 meter square field!  The field is
laid out like a 5x5 grid of 1 meter by 1 meter squares, with (1,1) being
the location of the upper-left square, and (5,5) being the location of the
lower-right square:

(1,1) (1,2) (1,3) (1,4) (1,5)
(2,1) (2,2) (2,3) (2,4) (2,5)
(3,1) (3,2) (3,3) (3,4) (3,5)
(4,1) (4,2) (4,3) (4,4) (4,5)
(5,1) (5,2) (5,3) (5,4) (5,5)

Every square in this grid is filled with delicious grass, except for K
barren squares (0 <= K <= 22, K even), which have no grass.  Bessie the cow
starts grazing in square (1,1), which is always filled with grass, and
Mildred the cow starts grazing in square (5,5), which also is always filled
with grass.  

Each half-hour, Bessie and Mildred finish eating all the grass in their
respective squares and each both move to adjacent grassy squares (north,
south, east, or west).  They want to consume all the grassy squares and end
up in exactly the same final location.  Please compute the number of
different ways this can happen.  Bessie and Mildred always move onto
grassy squares, and they never both move onto the same square unless that
is the very last grassy square remaining.

PROBLEM NAME: grazing

INPUT FORMAT:

* Line 1: The integer K.

* Lines 2..1+K: Each line contains the location (i,j) of a non-grassy
        square by listing the two space-separated integers i and j.

SAMPLE INPUT (file grazing.in):

4
3 2
3 3
3 4
3 1

INPUT DETAILS:

The initial grid looks like this (where . denotes a grassy square, x
denotes a non-grassy square, b indicates the starting location of Bessie,
and m indicates the starting location of Mildred):

b  .  .  .  .

.  .  .  .  .

x  x  x  x  .

.  .  .  .  .

.  .  .  .  m

OUTPUT FORMAT:

* Line 1: The number of different possible ways Bessie and Mildred can
        walk across the field to eat all the grass and end up in the
        same final location.

SAMPLE OUTPUT (file grazing.out):

1

OUTPUT DETAILS:

There is only one possible solution, with Bessie and Mildred meeting at
square (3,5):

b  b--b  b--b
|  |  |  |  |
b--b  b--b  b
            |
x  x  x  x b/m
            |
m--m--m--m--m
|
m--m--m--m--m
C题

N比较小,可以直接dfs.

 

Codes:

  1 #include<cmath>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<iostream>
  6 #include<algorithm>
  7 using namespace std;
  8 #define For(i,n) for(int i=1;i<=n;i++)
  9 #define Rep(i,l,r) for(int i=l;i<=r;i++)
 10 
 11 struct GOODS{
 12     int P,B,tot;
 13 }p[1100];
 14 int n,ans,b;
 15 void init(){
 16     scanf("%d%d",&n,&b);
 17     For(i,n){
 18         scanf("%d%d",&p[i].P,&p[i].B);
 19         p[i].tot = p[i].P + p[i].B;
 20     }
 21 }
 22 
 23 bool cmp(GOODS A,GOODS B){
 24     return A.tot<B.tot;
 25 }
 26 
 27 int main(){
 28     init();
 29     sort(p+1,p+n+1,cmp);
 30     For(i,n){
 31         int temp = b , tans = 0;
 32         if(temp>=p[i].P/2+p[i].B) {
 33             temp-=p[i].P/2+p[i].B;
 34             tans++;
 35         }
 36         For(j,n)
 37           if(j!=i)
 38             if(temp>=p[j].tot){
 39                 temp-=p[j].tot;
 40                 tans++;
 41             }
 42         ans = max(ans,tans);
 43     }
 44     printf("%d\n",ans);
 45     return 0;
 46 }
 47 ------------------------------以上是A题------------------------------
 48 #include<cmath>
 49 #include<cstdio>
 50 #include<cstring>
 51 #include<cstdlib>
 52 #include<iostream>
 53 #include<algorithm>
 54 using namespace std;
 55 #define For(i,n) for(int i=1;i<=n;i++)
 56 #define Rep(i,l,r) for(int i=l;i<=r;i++)
 57  
 58 int n,m,l,r,sum[1000010],ans;
 59  
 60 void init(){
 61     scanf("%d%d",&n,&m);
 62     For(i,m){
 63         scanf("%d%d",&l,&r);
 64         sum[l]++;sum[r+1]--;
 65     }       
 66 }
 67  
 68 int main(){
 69     init();
 70     For(i,n){
 71         ans+=sum[i];
 72         sum[i] = ans;
 73     }
 74     sort(sum+1,sum+n+1);
 75     printf("%d\n",sum[n/2+1]);
 76     return 0;
 77 }
 78 -----------------------------以上是B题-------------------------------
 79 #include<cmath>
 80 #include<queue>
 81 #include<cstdio>
 82 #include<cstring>
 83 #include<cstdlib>
 84 #include<iostream>
 85 #include<algorithm>
 86 using namespace std;
 87 const int dx[5] = {0,-1,0,1,0},
 88           dy[5] = {0,0,-1,0,1};
 89 #define For(i,n) for(int i=1;i<=n;i++)
 90 #define Rep(i,l,r) for(int i=l;i<=r;i++)
 91 bool hash[10][10],vis[10][10];
 92 int n,can,x,y,ans;
 93 
 94 void init(){
 95     scanf("%d",&n);
 96     can = 23-n;
 97     memset(vis,true,sizeof(vis));
 98     For(i,5)
 99       For(j,5) vis[i][j] = false;
100     For(i,n){
101         scanf("%d%d",&x,&y);
102         hash[x][y] = true;
103     }
104     hash[1][1] = true;hash[5][5] = true;
105 }
106 
107 void DFS(int x1,int y1,int x2,int y2,int k){
108     if(k==can){
109         For(i,4){
110             int newx1 = x1 + dx[i] , newy1 = y1 + dy[i];
111             if(!vis[newx1][newy1])
112                 For(j,4){
113                     int newx2 = x2 + dx[j] , newy2 = y2 + dy[j];
114                     if(!vis[newx2][newy2]&&newx2==newx1&&newy2==newy1&&!hash[newx1][newy1]) {
115                         ans++;
116                         return;
117                     }
118                 }
119         }
120     }
121     For(i,4){
122         int newx1 = x1 + dx[i] ,newy1 = y1 + dy[i];
123         if(hash[newx1][newy1]||vis[newx1][newy1]) continue;
124         vis[newx1][newy1] = true;
125         For(j,4){
126             int newx2 = x2 + dx[j] ,newy2 = y2 + dy[j];
127             if((hash[newx2][newy2]||vis[newx2][newy2])) continue;
128                 vis[newx2][newy2] = true;
129                 DFS(newx1,newy1,newx2,newy2,k+2);
130                 vis[newx2][newy2] = false;
131         }
132         vis[newx1][newy1] =false;
133     }
134 }
135 
136 int main(){
137     init();
138     DFS(1,1,5,5,1);
139     printf("%d\n",ans);
140     return 0;
141 }
142 ---------------------------------以上是C题---------------------------
ABC代码