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.
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")
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)
** 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
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
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