AtCoder Beginner Contest 015
はい。
https://atcoder.jp/contests/abc015
A - 高橋くんの研修
PyPy3
a=input() b=input() print(a if len(a)>len(b) else b)
文字列の長さを比較。異なる長さであることが保証されているので細かいことは気にせずに。
B - 高橋くんの集計
PyPy3
n=int(input()) a=[int(i) for i in input().split()] ans=chk=0 for i in a: if i>0: chk+=1 ans+=i print((ans+chk-1)//chk)
対象になるソフト数とバグ数を数えて繰り上げになるように割り算で解になるはず。
C - Attack Survival
PyPy3
# from https://atcoder.jp/contests/abc015/submissions/281622 def sol(t,pos,x): if pos==len(t): return x==0 for u in t[pos]: if sol(t,pos+1,x^u): return True return False n,k=map(int,input().split()) t=[] for i in range(n): t.append([int(i) for i in input().split()]) sol(t,0,0) print("Found" if sol(t,0,0) else "Nothing")
未だにdfs書くのダメなので検索したコードから写しました。
D - 高橋くんの苦悩
C++14
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;++i) #define per(i,n) for(int i=n-1;i>=0;--i) #define sc1(a) scanf("%d",&a) #define sc2(a,b) scanf("%d %d",&a,&b) #define sc3(a,b,c) scanf("%d %d %d",&a,&b,&c) #define sl1(a) scanf("%lld",&a) #define sl2(a,b) scanf("%lld %lld",&a,&b) #define sl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define PI 3.1415926535897932 int main(){ int w,n,k,ans=0; sc1(w); sc2(n,k); int dp[k+1][w+1]; rep(i,k+1) rep(j,w+1) dp[i][j]=0; rep(i,n) { int a,b; sc2(a,b); for(int j=k;j>0;j--) for(int p=w;p>0;p--) { if (p-a>=0) { dp[j][p]=max(dp[j][p],dp[j-1][p-a]+b); ans=max(ans,dp[j][p]); } } } printf("%d\n",ans); return 0; }
PyPy3
w=int(input()) n,k=map(int,input().split()) dp=[[0]*(w+1) for i in range(k+1)] ans=0 for x in range(n): a,b=map(int,input().split()) i=(a,b) for j in range(k,0,-1): for p in range(w,0,-1): if (p-i[0]>=0): dp[j][p]=max(dp[j][p],dp[j-1][p-i[0]]+i[1]) ans=max(ans,dp[j][p]) print(ans)
処理手順はC++もPythonも同様のことをしています。
dpの初期テーブルは dp[k+1][w+1]
です。これは0個からk個までと、幅0から幅wまでを想定してます。
入力を受け取りながらdpテーブルを逆から更新しています。同じスクリーンショットを複数回使用することを避けるためです。
あと解になる値も別の変数に入れて常に更新しておきます。