1. 程式人生 > >AtCoder Beginner Contest 115 題解

AtCoder Beginner Contest 115 題解

most otto abc ostream def ring decide cin string

題目鏈接:https://abc115.contest.atcoder.jp/

A Christmas Eve Eve Eve

題目:

Time limit : 2sec / Memory limit : 1024MB

Score : 100 points

Problem Statement

In some other world, today is December D-th.

Write a program that prints Christmas if D=25, Christmas Eve if D=24, Christmas Eve Eve if D=23 and Christmas Eve Eve Eve if D

=22.

Constraints

  • 22≤D≤25
  • D is an integer.

Input

Input is given from Standard Input in the following format:

D

Output

Print the specified string (case-sensitive).

Sample Input 1

25

Sample Output 1

Christmas

Sample Input 2

22

Sample Output 2

Christmas Eve Eve Eve

Be sure to print spaces between the words.

題解:

沒什麽好說的。

技術分享圖片
 1 #include <cstdio>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int d;
 8     scanf("%d", &d);
 9     if(d == 25)
10         printf("Christmas\n");
11     else if(d == 24)
12         printf("Christmas Eve\n");
13     else if(d == 23)
14         printf("Christmas Eve Eve\n
"); 15 else if(d == 22) 16 printf("Christmas Eve Eve Eve\n"); 17 return 0; 18 }
View Code

B Christmas Eve Eve

題目:

Time limit : 2sec / Memory limit : 1024MB

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1≤iN) is pi yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N?1 items cost their regular prices. What is the total amount he will pay?

Constraints

  • 2≤N≤10
  • 100≤pi≤10000
  • pi is an even number.

Input

Input is given from Standard Input in the following format:

N

p1

p2

:

pN

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3

4980

7980

6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980+7980?2+6980=15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4

4320

4320

4320

4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320?2+4320+4320+4320=15120 yen.

題解:

沒什麽好說的。

技術分享圖片
 1 #include <cstdio>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n;
 9     scanf("%d", &n);
10     int p[15], maxp = 0, sum = 0;
11     for(int i = 0; i < n; ++i)
12     {
13         scanf("%d", &p[i]);
14         maxp = max(maxp, p[i]);
15         sum += p[i];
16     }
17     sum = sum - maxp + maxp / 2;
18     printf("%d\n", sum);
19     return 0;
20 }
View Code

C Christmas Eve

題目:

Time limit : 2sec / Memory limit : 1024MB

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha‘s garden. The height of the i-th tree (1≤iN) is hi meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be hmax meters, and the height of the shortest decorated tree be hmin meters. The smaller the value hmax?hmin is, the better. What is the minimum possible value of hmax?hmin?

Constraints

  • 2≤K<N≤105
  • 1≤hi≤109
  • hi is an integer.

Input

Input is given from Standard Input in the following format:

N K

h1

h2

:

hN

Output

Print the minimum possible value of hmax?hmin.

Sample Input 1

5 3

10

15

11

14

12

Sample Output 1

2

If we decorate the first, third and fifth trees, hmax=12,hmin=10 so hmax?hmin=2. This is optimal.

Sample Input 2

5 3

5

7

5

7

7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, hmax=7,hmin=7 so hmax?hmin=0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can‘t put a sample with a hundred thousand lines here).

題解:

最終的答案就是k個數中的最大高度-最小高度。要讓答案盡可能的小,就要讓這兩個值盡可能接近。很容易想到排序一下,然後枚舉第i個和第i+k-1個之間的差值(0≤i≤n-k),取最小值即可。

技術分享圖片
 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n, k;
 9     scanf("%d %d", &n, &k);
10     int h[100005];
11     for(int i = 0; i < n; ++i)
12         scanf("%d", &h[i]);
13     sort(h, h + n);
14     int ans = 1e9;
15     for(int i = 0; i <= n - k; ++i)
16         ans = min(ans, h[i+k-1] - h[i]);
17     printf("%d\n", ans);
18     return 0;
19 }
View Code

D Christmas

題目:

Time limit : 2sec / Memory limit : 1024MB

Score : 400 points

Problem Statement

In some other world, today is Christmas.

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

  • A level-0 burger is a patty.
  • A level-L burger (L≥1) is a bun, a level-(L?1) burger, a patty, another level-(L?1) burger and another bun, stacked vertically in this order from the bottom.

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints

  • 1≤N≤50
  • 1≤X≤( the total number of layers in a level-N burger )
  • N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

Sample Input 1

2 7

Sample Output 1

4

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

Sample Input 2

1 1

Sample Output 2

0

The bottom-most layer of a level-1 burger is a bun.

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.

題解:

題目中burger的定義是遞歸的定義,自然也想到用遞歸的方式去做。我們可以發現burger是對稱的。那麽就可以分段遞歸。nlen是每個burger的長度(高度),p記錄的是每個burger含有的p的個數。這兩個公式稍微推一下就可以得到,是一個等比數列的問題,這裏不展開了。(事實上直接叠代也可以得到)

技術分享圖片

另外,我們可以發現X≤N的時候,ans=0;X≥nlen[N]的時候,ans=p[N]。由此可以寫出代碼。

註意輸入輸出的時候為了防止Compilation Error,用cin和cout較為妥當。

技術分享圖片
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 typedef unsigned long long ll;
 8 
 9 ll p[55], nlen[55];
10 
11 ll solve(ll n, ll x)
12 {
13     if(x <= n) return 0;
14     if(x >= nlen[n] - 1) return p[n];
15     if(x == nlen[n] / 2 + 1) return (p[n] / 2 + 1);
16     else if(x < nlen[n] / 2 + 1)
17         return solve(n - 1, x - 1);
18     else
19         return p[n] / 2 + 1 + solve(n - 1, x - nlen[n] / 2 - 1);
20 }
21 
22 int main()
23 {
24     for(int i = 0; i <= 50; ++i)
25     {
26         p[i] = pow(2, i + 1) - 1;
27         nlen[i] = pow(2, i - 1) * 8 - 3;
28     }
29     ll n, x;
30     cin>>n>>x;
31     cout<<solve(n, x)<<endl;
32     return 0;
33 }
View Code

AtCoder Beginner Contest 115 題解