AtCoder Beginner Contest 056 Review of past questions

Time required

スクリーンショット 2020-01-30 16.32.01.png

Impressions

The first yellow question completed within the time! !! It's easy and it's about blue, but I'm happy! I want to work harder and be able to solve the yellow color stably.

Problem A

I didn't split the input because it was a hassle.

answerA.py


s=input()
print("H" if s=="H H" or s=="D D" else "D")

B problem

Consider which section is on the right side (on the 2D plane).

answerB.py


w,a,b=map(int,input().split())
if a<=b<=a+w or a<=b+w<=a+w:
    print(0)
elif b>a+w:
    print(b-a-w)
else:
    print(a-b-w)

C problem

** I was able to pass it without proof. ** I think it's proof that you're getting stronger. First, we want to reach as soon as possible, so consider the maximum reachable coordinates at time $ t $. Then you can easily see that it becomes $ 1 + 2 +… + t = t \ times (t + 1) \ div 2 $. Also, when the time is $ t-1 $, it can reach $ (t-1) \ times t \ div 2 $, so when the time is $ t $, $ (t-1) \ times t \ div 2 + 1 $ If we can show that we can reach all the coordinates of ~ $ t \ times (t + 1) \ div 2 $ (✳︎), we will calculate $ t \ times (t + 1) \ div 2 $ in order from time 1. You can see that the time it takes to be greater than or equal to the given x is the minimum value to be calculated. Here, if you select the action of doing nothing at any time k, you will reach the coordinates of $ t \ times (t + 1) \ div 2-k $, and by moving k = t-1 → 1. Since $ (t-1) \ times t \ div 2 +1 $ → $ t \ times (t + 1) \ div 2-1 $ can all be represented, (✳︎) is shown.

answerC.py


x=int(input())
for i in range(1,10**5):
    if x<=(i*(i+1))//2:
        print(i)
        break

D problem

As expected, the yellow problem was difficult, but I managed to get a complete answer.

First of all, when I experimented with the sample, I noticed that it seems that cards smaller than ** are likely to be unnecessary **, so after sorting for the time being, the i-th element is unnecessary. I wondered if there was one. Also, paying attention to the case where card i is not unnecessary, ** if card i becomes a good set when added to a bad set ** (if there is even one way to select such a bad set), I also realized that it wasn't unnecessary. (← This paraphrase seems to be the most difficult ... However, I feel that I often use the idea that the opposite of ** is added **.) Here, I tried to find a pattern that would be a good set when I added card i to a bad set, but it didn't work. Why not switch the policy here and think about all the number patterns that can be represented by cards except card i? I thought. Then you can use DP. In other words, if you can record the sum total pattern of the numbers written on the card by DP and add the numbers written on the card i to create a number that exceeds k, that card i is unnecessary. It can be judged that there is no **. Conversely, if the sum of the numbers written on the card is less than k but the maximum value (note that it contains ** 0 **) and the sum of the numbers written on the card i do not exceed k, the card i is no longer needed. From the above, when searching for a pattern that becomes a good set when card i is added to a bad set, the amount of calculation is O ($ nk ), but if you check in order whether card i is unnecessary, O ( n ^) It will be 2k ) and will not end within the time limit. However, if you use the monotonicity that "when card i is unnecessary, card j (j <= i) with a smaller number is also unnecessary", binary search can be used, and O ( nk ) Since $ nk \ log {n} $ is about $ 10 ^ 8 $ in the amount of calculation of log {n} $), you can write a program that is just in time. Also, the estimation of the amount of calculation was loose (I thought it was about $ 10 ^ 7 ), and I issued it in Python and issued TLE. Also, it seems that the total amount of calculation can be reduced to O ( nk $), and I think that it will be in time if it is reduced to this level even in Python. Other than that, I forgot that ** the array is not sorted even though it is a binary search **, ** the element is always required if the number written on the card is k or more **, and so on. Be careful as such ** can't be helped if the basic algorithm is wrong **.

answerD_TLE.py


n,k=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()

def check(d):#Do you need it
    global a,n,k
    dp=[0]*(k)#k-Up to 1
    if a[d]>=k:
        return True
    for i in range(n):
        if i!=d:
            dp_sub=[0]*(k)
            for j in range(k-1):
                if j==0 or dp[j]!=0:
                    if j+a[i]<k:
                        dp_sub[j+a[i]]=1
                    else:
                        break
            for j in range(k):
                if dp_sub[j]==1:
                    dp[j]=1
                    if j+a[d]>=k:
                        return True
    return False
l,r=0,n-1
while l+1<r:
    d=(l+r)//2
    if check(d):#Is it necessary
        r=d
    else:
        l=d
if check(l):
    print(l)
else:
    if check(r):
        print(r)
    else:
        print(r+1)

answerD.cc


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;
typedef long long ll;
ll n,k;
vector<ll> a;

bool check(ll d){
    vector<ll> dp(k,0);
    if(a[d]>=k) return  true;
    for(ll i=0;i<n;i++){
        if(i!=d){
            vector<ll> dp_sub(k,0);
            for(ll j=0;j<k-1;j++){
                if(j==0 or dp[j]!=0){
                    if(j+a[i]<k){
                        dp_sub[j+a[i]]=1;
                    }else{
                        break;
                    }
                }
            }
            for(ll j=0;j<k;j++){
                if(dp_sub[j]==1){
                    dp[j]=1;
                    if(j+a[d]>=k) return true;
                }
            }
        }
    }
    return false;
}

signed main(){
    cin >> n >> k;
    a.resize(n);
    for(ll i=0;i<n;i++){cin >> a[i];}
    sort(a.begin(),a.end());
    ll l=0;ll r=n-1;
    while(l+1<r){
        ll d=floor(l+r)/2;
        if(check(d)){
            r=d;
        }else{
            l=d;
        }
    }
    if(check(l)){
        cout << l << endl;
    }else if(check(r)){
        cout << r << endl;
    }else{
        cout << r+1 << endl;
    }
}

Recommended Posts

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 Review of past questions