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 cow‘s 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 FJ‘s question.

PROBLEM NAME: stacking

INPUT FORMAT:

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

* Lines 2..1+K: Each line contains one of FJ‘s 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代码

USACO JAN 2012 Bronze,布布扣,bubuko.com

时间: 2025-01-13 18:22:37

USACO JAN 2012 Bronze的相关文章

Balanced Teams (USACO Jan Bronze 2014)

既然是bronze,毫无压力的AC了. 就是个深搜,当然加个剪枝--最后一个组不用搜. 恩可以一个一个组分层次dfs,这样会跑得飞起~~也不容易错 #include <cstdio> int f[13],i,su,tt1,tt2,lev[4],min; bool has[13]; inline int md(int a,int b,int c,int d){ tt1=a,tt2=a; if(tt1<b)tt1=b; if(tt1<c)tt1=c; if(tt1<d)tt1=d

【组队赛#9】USACO 2006 January Bronze

[A题]A. Stump Removal 链接click here~~ [题目大意]一排高低不平的树桩,需要用炸弹全部炸掉,如果一个树桩的前面和后面的树桩高度都比它小,炸弹爆炸的时候会同时炸掉,求尽可能少的放置炸弹的数目,输出树桩的编号. [解题思路] 理解题意,从左往右扫,如果当前位置右边或左边的比它低了或相等,那么就把这个位置炸掉,然后把能炸的都炸掉,判断当前树桩前面的和后面的高度比较, 核心代码 for(int i=1;i<=n+1;i++) { if(a[i]>=a[i-1]&

[usaco jan 09] 气象牛 baric [dp]

题面: 传送门 思路: 题意有点绕,实际上就是给你一个计算规则,让你取最少的元素,通过这个计算方式,得到一个小于指定误差上限的结果 这个规则分为三个部分,这里分别用pre,sum,suf表示 因为给定的元素个数(天数)很少,可以使用O(n^3)算法,因此考虑使用经过了预处理的dp解决问题 具体地,设dp[i][j]表示前i个元素使用了j个且一定取用了第i个时可能达到的最小误差值 预处理:pre[i]表示通过第一种计算方式得到的,第一个元素取第i个时得到的误差 suf[i]同理,为第三种计算方式

[usaco jan 09] 安全路径 travel [最短路径树]

题面: 传送门 思路: 既然最后一条边不能走,那么就一定是换了一条路,一条不经过这最后一条边的路 如果想要这条路最短,那么其在路上一定尽可能多地走了最短路径 因此,我们对这张图跑一遍从1开始的单源最短路,并建立出最短路径树 那么新的路径(1->u)一定是这样构成的:(1->v)+edge(v,w)+(w->u),其中w是u在最短路径树上的后代 那么,我们对于每一条非树边(u,v),其树上路径上所有点(除了lca)的答案,都可以被dis[u]+dis[v]+w(u,v)-dis[路径上的点

POJ3048 Max Factor

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权!   Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct s

F-Dining Cows(POJ 3671)

Dining Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7584   Accepted: 3201 Description The cows are so very silly about their dinner partners. They have organized themselves into two groups (conveniently numbered 1 and 2) that ins

garbage collection - 垃圾收集

Professional.JavaScript.for.Web.Developers.3rd.Edition.Jan.2012 JavaScript is a garbage-collected language, meaning that the execution environment is responsible for managing the memory required during code execution. In languages like C and C++, kee

HTML5离线应用无法更新的定位与解决

些许前提 最近在制作一个Web应用, 其中用到了HTML5的离线应用功能(offline application), 离线应用的概念就不再阐述, 可以查看这两篇文章: http://www.ibm.com/developerworks/cn/web/1011_guozb_html5off/ http://www.mhtml5.com/2011/02/583.html 这里主要讨论它的更新问题. 首先浏览器是有两部分cache的, browser cache 和app cache, browser

HDU2710_Max Factor【水题】【筛法求素数】

Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3966    Accepted Submission(s): 1289 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1