码迷,mamicode.com
首页 > 其他好文 > 详细

USACO 2017 Dec Bronze

时间:2017-12-30 23:34:25      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:include   unit   cal   lease   log   stdout   should   lan   possible   

由于自己是第一次参加USACO,于是被光荣分到了铜组,这里就放上三道水题的题解吧(代码是比赛时写的,可能有些丑)。

1、Blocked Billboard

During long milking sessions, Bessie the cow likes to stare out the window of her barn at two huge rectangular billboards across the street advertising "Farmer Alex‘s Amazingly Appetizing Alfalfa" and "Farmer Greg‘s Great Grain". Pictures of these two cow feed products on the billboards look much tastier to Bessie than the grass from her farm.

One day, as Bessie is staring out the window, she is alarmed to see a huge rectangular truck parking across the street. The side of the truck has an advertisement for "Farmer Smith‘s Superb Steaks", which Bessie doesn‘t quite understand, but she is mostly concerned about the truck potentially blocking the view of her two favorite billboards.

Given the locations of the two billboards and the location of the truck, please calculate the total combined area of both billboards that is still visible. It is possible that the truck obscures neither, both, or only one of the billboards.

INPUT FORMAT (file billboard.in):

The first line of input contains four space-separated integers:x1x1 y1y1 x2x2 y2y2, where (x1,y1)(x1,y1) and (x2,y2)(x2,y2) are the coordinates of the lower-left and upper-right corners of the first billboard in Bessie‘s 2D field of view. The next line contains four more integers, similarly specifying the lower-left and upper-right corners of the second billboard. The third and final line of input contains four integers specifying the lower-left and upper-right corners of the truck. All coordinates are in the range -1000 to +1000. The two billboards are guaranteed not to have any positive area of overlap between themselves.

OUTPUT FORMAT (file billboard.out):

Please output the total combined area of both billboards that remains visible.

SAMPLE INPUT:

1 2 3 5
6 0 10 4
2 1 8 3

SAMPLE OUTPUT:

17

Here, 5 units of area from the first billboard and 12 units of area from the second billboard remain visible.

大意:在平面直角坐标系中,有两个矩形(保证不相交),然后给出第三个矩形,求这两个矩形没有被第三个矩形遮住的部分的面积。题目会给出6个坐标,分别描述三个矩形的左下角、右上角坐标(意味着你能求出矩形长、宽、面积)。

题解:直接给出数学依据:S=S1-S2-S1遮-S2遮,具体看程序。

技术分享图片
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 struct point
 5 {
 6  int x,y;
 7 }p[15];
 8 int main()
 9 {
10  //freopen("billboard.in","r",stdin);
11  //freopen("billboard.out","w",stdout);
12  int s1,s2,sj1,sj2;
13  for(int i=1;i<=6;i++)
14   scanf("%d %d",&p[i].x,&p[i].y);
15  s1=(p[2].x-p[1].x)*(p[2].y-p[1].y);
16  s2=(p[4].x-p[3].x)*(p[4].y-p[3].y);
17  if(p[5].x>=p[2].x||p[5].y>=p[2].y||p[6].x<=p[1].x||p[6].y<=p[1].y)sj1=0;//不相交
18  else sj1=(min(p[6].x,p[2].x)-max(p[5].x,p[1].x))*(min(p[6].y,p[2].y)-max(p[5].y,p[1].y));
19  if(p[5].x>=p[4].x||p[5].y>=p[4].y||p[6].x<=p[3].x||p[6].y<=p[3].y)sj2=0;
20  else sj2=(min(p[6].x,p[4].x)-max(p[5].x,p[3].x))*(min(p[6].y,p[4].y)-max(p[5].y,p[3].y));
21  printf("%d",s1+s2-sj1-sj2);
22  //fclose(stdin);
23  //fclose(stdout);
24  return 0;
25 }
View Code

2、The Bovine Shuffle

Convinced that happy cows generate more milk, Farmer John has installed a giant disco ball in his barn and plans to teach his cows to dance!

Looking up popular cow dances, Farmer John decides to teach his cows the "Bovine Shuffle". The Bovine Shuffle consists of his NN cows (1N1001≤N≤100) lining up in a row in some order, then performing three "shuffles" in a row, after which they will be lined up in some possibly different order. To make it easier for his cows to locate themselves, Farmer John marks the locations for his line of cows with positions 1N1…N, so the first cow in the lineup will be in position 1, the next in position 2, and so on, up to position NN.

A shuffle is described with N numbers, a1aNa1…aN, where the cow in position ii moves to position aiai during the shuffle (and so, each aiai is in the range 1N1…N). Every cow moves to its new location during the shuffle. Fortunately, all the aiai‘s are distinct, so no two cows try to move to the same position during a shuffle.

Farmer John‘s cows are each assigned distinct 7-digit integer ID numbers. If you are given the ordering of the cows after three shuffles, please determine their initial order.

INPUT FORMAT (file shuffle.in):

The first line of input contains NN, the number of cows. The next line contains the NN integers a1aNa1…aN. The final line contains the order of the NN cows after three shuffles, with each cow specified by its ID number.

OUTPUT FORMAT (file shuffle.out):

You should write NN lines of output, with a single cow ID per line, specifying the order of the cows before the three shuffles.

SAMPLE INPUT:

5
1 3 4 5 2
1234567 2222222 3333333 4444444 5555555

SAMPLE OUTPUT:

1234567
5555555
2222222
3333333
4444444

大意:给出一种变换顺序及3次变换后按顺序给出的cow的ID,计算出3次变换前牛的实际顺序。

题解:简单的逆推,倒过来求解即可。

技术分享图片
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int order[105],id[105],way[105],c1[105],c2[105],c3[105];
 5 int main()
 6 {
 7  //freopen("shuffle.in","r",stdin);
 8  //freopen("shuffle.out","w",stdout);
 9  int n;
10  scanf("%d",&n);
11  for(int i=1;i<=n;i++)
12   scanf("%d",&order[i]);
13  for(int i=1;i<=n;i++)
14   scanf("%d",&id[i]);
15  for(int i=1;i<=n;i++)
16   way[order[i]]=i;
17  for(int i=1;i<=n;i++)
18   c1[way[i]]=id[i];
19  for(int i=1;i<=n;i++)
20   c2[way[i]]=c1[i];
21  for(int i=1;i<=n;i++)
22   c3[way[i]]=c2[i];
23  for(int i=1;i<=n;i++)
24   printf("%d\n",c3[i]);
25  //fclose(stdin);
26  //fclose(stdout);
27  return 0;
28 }
View Code

3、Milk Measurement

Farmer John purchases three cows: Bessie, Elsie, and Mildred, each of whom initially produces 7 gallons of milk per day. Since the milk output of a cow is known to potentially change over time, Farmer John takes periodic measurements over the next 100 days and scribbles them down in a log book. Entries in his log look like this:

 

35 Bessie -2
14 Mildred +3

The first entry indicates that on day 35, Bessie‘s milk output was 2 gallons lower than it was when last measured. The next entry indicates that on day 14, Mildred‘s milk output increased by 3 gallons from when it was last measured. Farmer John has only enough time to make at most one measurement on any given day. Unfortunately, he is a bit disorganized, and doesn‘t necessarily write down his measurements in chronological order.

To keep his cows motivated, Farmer John proudly displays on the wall of his barn the picture of whichever cow currently has the highest milk output (if several cows tie for the highest milk output, he displays all of their pictures). Please determine the number of days on which Farmer John would have needed to change this display.

 

INPUT FORMAT (file measurement.in):

The first line of input contains NN, the number of measurements Farmer John makes. Each of the next NN lines contains one measurement, in the format above, specifying a day (an integer in the range 1..100), the name of a cow, and the change in her milk output since it was last measured (a nonzero integer). Each cow‘s milk output will always be in the range 0..1000.

 

OUTPUT FORMAT (file measurement.out):

Please output the number of days (an integer in the range 0..100) on which Farmer John needs to adjust his motivational display.

 

SAMPLE INPUT:

4
7 Mildred +3
4 Elsie -1
9 Mildred -1
1 Bessie +2

SAMPLE OUTPUT:

3

Initially, all cows have milk output 7. On day 1, Bessie‘s milk output increases to 9, making her the unique cow with highest milk output and causing Farmer John to change his display. On day 4, Elsie‘s milk output decreases to 6, but this does not change the fact that Bessie is the sole cow in the lead. On day 7, Mildred jumps into the lead, changing the display, and on day 9, Mildred drops in production to be tied with Bessie, again changing the display.

大意:已知刚开始时,每头牛的产量都是7加仑,没有按顺序给出某天某头cow产量的变换值(保证一天最多只会有一头奶牛的产量发生变化),现在FJ想要整奶牛光荣榜,将每天产量第一名的奶牛的照片挂起来(如果有并列,则都挂起来),算出FJ需要改变光荣榜的次数。

题解:把这些记录存下来之后按时间顺序排序一遍,然后无脑模拟一遍,就OK了(这里因为只有3头牛,所以无脑枚举了所以可能的大小关系)。

技术分享图片
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 struct node
 6 {
 7  int d,c;
 8  char name[15];
 9 }a[105];
10 int oa,ob,oc,best;
11 int cmp(const node&a,const node&b)
12 {
13  return a.d<b.d;
14 }
15 int main()
16 {
17  //freopen("measurement.in","r",stdin);
18  //freopen("measurement.out","w",stdout);
19  int n,ans=0;
20  scanf("%d",&n);
21  for(int i=1;i<=n;i++)
22   scanf("%d%s%d",&a[i].d,a[i].name,&a[i].c);
23  sort(a+1,a+n+1,cmp);
24  for(int i=1;i<=n;i++)
25  {
26   if(!strcmp(a[i].name,"Bessie"))oa+=a[i].c;
27   else if(!strcmp(a[i].name,"Elsie"))ob+=a[i].c;
28   else oc+=a[i].c;
29   int newbest;
30   if(oa>ob&&oa>oc)newbest=1;
31   else if(ob>oa&&ob>oc)newbest=2;
32   else if(oc>oa&&oc>ob)newbest=3;
33   else if(oa==ob&&oa>oc)newbest=4;
34   else if(oa==oc&&oa>ob)newbest=5;
35   else if(ob==oc&&ob>oa)newbest=6;
36   else newbest=0;
37   if(newbest!=best)ans++;
38   best=newbest;
39  }
40  printf("%d",ans);
41  //fclose(stdin);
42  //fclose(stdout);
43  return 0;
44 }
View Code

 

USACO 2017 Dec Bronze

标签:include   unit   cal   lease   log   stdout   should   lan   possible   

原文地址:https://www.cnblogs.com/studyingfather/p/8048068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!