谷歌kickstart_竞争编程评论笔记Google kickstart回合问题2

谷歌kickstart

I’ve been participating in Google’s Kickstart which is a competitive programming contest and I found out that writing a review note on those problem I struggled would be great to improve my skills and help others who struggled as well!

我一直参加Google的Kickstart竞赛,这是一个竞争激烈的编程竞赛,我发现就我所遇到的那些问题写评论笔记将非常有助于提高我的技能并帮助其他同样挣扎的人!

I use C++ for competitive programing (because it is everyone’s favorite language — haha) and I would love to discuss about the problem 2, 3, 4 of Round E which happened in August 23. (This post will talk about problem 2)

我使用C ++进行竞争性编程(因为它是每个人都喜欢的语言-哈哈),我很想讨论发生在8月23日的E轮问题2、3、4。(本文将讨论问题2)

[问题2:高层建筑] ([Problem2: High Buildings])

问题 (Problem)

In an unspecified country, Google has an office campus consisting of N office buildings in a line, numbered from 1 to N from left to right. When represented in meters, the height of each building is an integer between 1 to N, inclusive.

在一个未指定的国家/地区,Google拥有一个办公园区,该园区由N座办公大楼排成一行,从左到右从1到N。 当以米为单位表示时,每座建筑物的高度是1到N之间(包括1和N )的整数。

Andre and Sule are two Google employees working in this campus. On their lunch break, they wanted to see the skyline of the campus they are working in. Therefore, Andre went to the leftmost point of the campus (to the left of building 1), looking towards the rightmost point of the campus (to the right of building N). Similarly, Sule went to the rightmost point of the campus, looking towards the leftmost point of the campus.

Andre和Sule是在该园区工作的两名Google员工。 在午休时,他们想看到他们正在工作的校园的天际线。因此,安德烈(Andre)来到了校园的最左端(1号楼的左侧),朝着校园的最右端(即N号楼的右侧)。 同样,Sule到达了校园的最右边,朝着校园的最左边看。

To Andre, a building x is visible if and only if there is no building to the left of building x that is strictly higher than building x. Similarly, to Sule, a building x is visible if and only if there is no building to the right of building x that is strictly higher than building x.

对于安德烈(Andre),只有且仅当建筑物x左侧没有建筑物严格高于建筑物x时,建筑物x才可见。 与Sule类似,当且仅当在建筑物x的右侧没有严格高于建筑物x的建筑物时,建筑物x才可见。

Andre learned that there are A buildings that are visible to him, while Sule learned that there are B buildings that are visible to him. After they regrouped and exchanged information, they also learned that there are C buildings that are visible to both of them.

安德烈得知有A建筑物对他可见,而苏雷得知有B建筑物对他可见。 在他们重新组织并交换了信息之后,他们还了解到,有两座C建筑物都对他们可见。

They are wondering about the height of each building. They are giving you the value of N, A, B, and C for your information. As their friend, you would like to construct a possible height for each building such that the information learned on the previous paragraph is correct, or indicate that there is no possible height construction that matches the information learned (thus at least one of them must have been mistaken).

他们想知道每座建筑物的高度。 他们为您提供NABC的值供您参考。 作为他们的朋友,您想为每座建筑物构造一个可能的高度,以使在上一段中了解到的信息正确,或者指示没有可能与所获信息匹配的高度构造(因此,至少其中一个必须具有被弄错了)。

输入项 (Input)

The first line of the input gives the number of test cases, T. T test cases follow. Each consists of a single line with four integers N, A, B, and C: the information given by Andre and Sule.

输入的第一行给出测试用例的数量T。 随后是T测试用例。 每个都由一行包含四个整数NABC组成 :Andre和Sule给出的信息。

输出量 (Output)

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is IMPOSSIBLE if there is no possible height for each building according to the above information, or N space-separated integers otherwise. The i-th integer in y must be the height of the i-th building (in meters) between 1 to N.

对于每个测试用例,输出包含Case #x: y一行,其中x是测试用例编号(从1开始),如果根据上述信息每个建筑物都没有高度,则yIMPOSSIBLE ,或者N个空格-否则,将整数分开。 y的第i个整数必须是1到N之间的第i个建筑物的高度(以米为单位)。

限度 (Limits)

Time limit: 20 seconds per test set.Memory limit: 1GB.1 ≤ T ≤ 100.1 ≤ CN.CAN.CBN.

时间限制:每测试set.Memory极限20秒:1GB.1≤:T≤100.1≤C≤ñ。 C≤A≤ñ。 C≤ ≤ñ。

测试集1 (Test Set 1)

1 ≤ N ≤ 5.

1≤N≤5。

测试集2 (Test Set 2)

1 ≤ N ≤ 100.

1≤N≤100。

样品 (Sample)

Input Output

输入输出

3
4 1 3 1
4 4 4 3
5 3 3 2Case #1: 4 1 3 2
Case #2: IMPOSSIBLE
Case #3: 2 1 5 5 3

注释 (Comments)

Yeah, we basically use the information of # buildings the left person can see, # buildings the right person can see, and # buildings both can see to give a possible building combination.

是的,我们基本上使用左人可以看到的#建筑物,右人可以看到的#建筑物和#都可以看到的#建筑物的信息来给出可能的建筑物组合。

So I came up with an approach which generates the combination of buildings based on the constraints and used “greater stack”(which will only accept the new element if the new element is bigger than equal to the top of the stack) concept to find the #buildings either the left or right can see or both can see. This was so straightforward, and I got TLE for their second test case which is larger test set than the first test case.

因此,我想出了一种方法,该方法根据约束条件生成建筑物的组合并使用“更大的堆栈”(仅当新元素大于等于堆栈顶部时才接受新元素)概念来找到#buildings左侧或右侧可以看到,或者两者都可以看到。 这非常简单,我为他们的第二个测试用例获得了TLE,比第一个测试用例更大的测试集。

Since I was struggling with their problem 3 and 4, I wasn’t able to spend my time finding a better way to solve this during the competition. But after the competition, I revisited the problem to figure out what can be optimized.

由于我一直在努力解决他们的问题3和问题4,因此我无法花时间在比赛中找到更好的解决方案。 但是在比赛之后,我重新考虑了这个问题,以找出可以优化的地方。

  1. My approach is literally investigating all of the possible cases!!!!

    我的方法实际上是调查所有可能的情况!!!
  2. Plus, at each investigation, I’m using O(N) further investigation to collect the A, B, and C information.

    另外,在每次调查中,我都使用O(N)进一步调查来收集A,B和C信息。

This so brute force way should be improved and I found some of the parts I can reduce a time.

应该改进这种蛮力的方式,我发现我可以减少一些时间。

这个想法是,“改变思维方式!!!!” (The idea is, “Change the way of THINKING!!!!”)

We just need to return one of the valid answers, then let’s just directly make a valid answer following the constraint instead of finding a valid one out of all!

我们只需要返回一个有效答案,然后让我们直接按照约束条件做出一个有效答案,而不是从所有答案中找出一个有效答案即可!

In order to do so, some mathematical proof was needed for the base case!

为此,基本情况需要一些数学证明!

Base case will check “IMPOSSIBLE” case and simple cases such as # of building == 1 or two.

基本案例将检查“不可行”案例和简单案例,例如建筑物编号== 1或2。

Under the variable condition where,A == # of buildings the left person sees B == # of buildings the right person seesC == # of buildings both seea == # of buildings ONLY the left person seesb == # of buildings ONLY the right person sees

在以下可变条件下,A ==左眼看到的建筑物数量B ==右眼看到的建筑物数量C ==两眼都看到建筑物的数量==左眼看到建筑物的数量b左眼看到的建筑物==合适的人看到

A + B - C should be ≤ N because A + B == a + b + 2C and so A + B - C == a + b + 2C - C == a + b + C. Why? let say there are ‘d’ many buildings that are not included in any of A, B, C, a and b. Then the total # of building (N) is a + b + d, and so A + B - C which is == to a + b + C cannot be greater than N because the only case this will work is C ==0 and d == 0 but C cannot be 0 in any cases (there should be at least one building that is view-shared by both the left and the right person)

A + B-C应该≤N,因为A + B == a + b + 2C,所以A + B-C == a + b + 2C-C == a + b +C。为什么? 可以说,有“ d”个建筑物不包含在A,B,C,a和b中。 那么建筑物的总数(N)为a + b + d,因此= +等于a + b + C的A + B-C不能大于N,因为唯一可行的情况是C == 0且d == 0,但在任何情况下C都不能为0(应该至少有一个建筑物由左,右两个人共享视图)

Also, except for the case where total # of buildings (N) is 1, A + B - C (equivalent to a + b + C) cannot be 1 because to get the minimum value of (a + b) there should be only one building and so both a and b == 0 and C == 1. If N ≥ 2, then the minimal value of a + b + C is 2 which is the case of two buildings with the same height.

此外,除了建筑物的总数(N)为1的情况外,A + B-C(等于a + b + C)不能为1,因为要获得(a + b)的最小值,一座建筑物,因此a和b == 0且C ==1。如果N≥2,则a + b + C的最小值为2,这是两座具有相同高度的建筑物的情况。

Based on this mathematical proof, we can say

基于这个数学证明,我们可以说

Image for post

Then, let’s tackle down simple cases which is N == 1 and N ==2. Since now we don’t have to worry about “IMPOSSIBLE CASE” if N ==1, then the height of this building can be anything.

然后,让我们解决N == 1和N == 2的简单情况。 从现在开始,如果N == 1,我们就不必担心“不可能的情况”,那么这座建筑物的高度可以是任何东西。

Image for post

For the N ==2, the possible cases are (1) two buildings with the same height (which will have C == 2), or (2) either of them is higher than the other (ex) “1 2” or “2 1”

对于N == 2,可能的情况是(1)两座具有相同高度的建筑物(其C == 2),或(2)其中两座建筑物都比另一座建筑物(例如)“ 1 2”高,或者“ 2 1”

Image for post

Lastly, we have to handle a generic case where it is NOT IMPOSSIBLE an N ≥ 3. Since we just need to return one of the valid combination, let’s only use “1”, “2”, “3” to represent the shortest building, the medium-size building, and the tallest building respectively.

最后,我们必须处理一个通用情况,即N≥3是不可能的。由于我们只需要返回有效组合之一,所以我们仅使用“ 1”,“ 2”,“ 3”来表示最短的建筑物,中型建筑物和最高建筑物。

In the generic case, we have to take care of ‘d’ which is the “invisible buildings” to any of them. According to my mathematical proof above, d == N - (A + B - C). And we will first build the valid building combination based on A(technically ‘a’), B(technically ‘b’), C and then insert those invisibles in some point between the leftmost building and the right most building and the invisible’s height will be “1” cuz “1” is not visible by sitting between “2” or “3”

在一般情况下,我们必须注意“ d”,这是它们中任何一个的“隐形建筑物”。 根据我上面的数学证明,d == N-(A + B-C)。 然后我们将首先基于A(技术上为'a'),B(技术上为'b'),C构建有效的建筑物组合,然后将这些不可见物插入最左侧建筑物和最右侧建筑物之间的某个点,不可见物的高度将为“ 1”,因为坐在“ 2”或“ 3”之间看不到“ 1”

To summarize, we put buildings that are only visible to the left person (a) by using “2” and then put the buildings that are visible by both the left person and right person(C) by using “3” and then put the buildings that are only visible to the right person (b) by using “2.” Then insert those invisibles at some point between the two buildings. In that case, we are not only following the constraint of A, B, and C and meet the requirement of N by padding those invisibles at some appropriate place

总而言之,我们使用“ 2”放置仅对左人可见的建筑物(a),然后使用“ 3”放置对左人和右人(C)都可见的建筑物,然后将通过使用“ 2”仅对正确的人(b)可见的建筑物。 然后在两座建筑物之间的某个位置插入那些不可见的物体。 在这种情况下,我们不仅会遵循A,B和C的约束,而且还会通过在适当的位置填充那些不可见项来满足N的要求

Image for post

That’s it!!! Thanks to this optimized version, now we are not inspecting all of possible cases, but directly building a valid combination which will take O(1) at its best case and O(N) on average!

而已!!! 由于有了这个优化的版本,现在我们不再检查所有可能的情况,而是直接构建有效的组合,该组合将在最佳情况下采用O(1),在平均情况下采用O(N)!

The entire code for this is:

整个代码如下:

Image for post
Image for post
Image for post

Key point of this problem was: figure out all of the possible cases by N and start building from the base case and so you can create a simple and less complicated generic case handler!

这个问题的关键是:用N找出所有可能的情况,然后从基本情况开始构建,这样您就可以创建一个简单而又不太复杂的通用情况处理程序!

If you interested in Google Kickstart Round E’s problem 3 — Toys → https://medium.com/@eunpyolee0420/competitive-programming-review-note-google-kickstart-round-e-problem-3-e9e645fd90c7

如果您对Google Kickstart回合E的问题3感兴趣-玩具→ https://medium.com/@eunpyolee0420/competitive-programming-review-note-google-kickstart-round-e-problem-3-e9e645fd90c7

If you interested in Google Kickstart Round E’s problem 4— Golden Stone → https://medium.com/@eunpyolee0420/competitive-programming-review-note-google-kickstart-round-e-problem-3-e9e645fd90c7

如果您对Google Kickstart回合E的问题4感兴趣-金石→ https://medium.com/@eunpyolee0420/competitive-programming-review-note-google-kickstart-round-e-problem-3-e9e645fd90c7

翻译自: https://medium.com/@eunpyolee0420/competitive-programming-review-note-google-kickstart-round-e-problem-2-94d45b80eecb

谷歌kickstart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值