[PYTHON] AtCoder Beginner Contest 067 Review of past questions

The second past question that I have already solved

Time required

スクリーンショット 2019-12-25 19.42.34.png

Problem A

Cases that are multiples of 3

answerA.py


a,b=map(int,input().split())
if a%3==0 or b%3==0 or (a+b)%3==0:
    print("Possible")
else:
    print("Impossible")

B problem

Get k from the larger one

answerB.py


n,k=map(int,input().split())
l=[int(i) for i in input().split()]
l.sort(reverse=True)
print(sum(l[:k]))

C problem

Just look at it from the front and update mi

answerC.py


n=int(input())
a=[int(i) for i in input().split()]
s=sum(a)
c=0
b=[]
for i in range(n-1):
    c+=a[i]
    b.append(c)
mi=abs(s-2*b[0])
for i in range(n-1):
    mi=min(mi,abs(s-2*b[i]))
print(mi)

answerC2.py


n=int(input())
a=[int(i) for i in input().split()]
s=sum(a)
c=a[0]
mi=abs(s-2*c)
for i in range(1,n):
    c+=a[i]
    mi=min(mi,abs(s-2*c))
print(mi)

D problem

From the condition that we paint in order from the closest place, ** I thought that the one closer to the node of 1 and N would be black and white ** (It is a little growth that I became able to do this idea. ), But I couldn't think of the case where the distances from the nodes 1 and N are the same. Actually, I didn't realize it when I was solving it, but I couldn't grasp the meaning of "acting optimally". When there is a route connecting ** 1 and N, the colors are painted in order from the node on that route, and when there are multiple routes, the colors on the other side are painted in order. **,is what it means. Imagine when you paint this path (see the figure in Explanation), the lead is "black in the first node". Considering this together, the distance between the i-th to j-th Nodes is d (i, j), and ** d (1, i) <= d (N, i) is black when it holds on that path * You can see that it becomes *. Also, for Nodes that are not on that path, the color is uniquely determined from the color painted on this path, and it is clear that d (1, i) <= d (N, i) holds (if it is black). is. If you can consider it as above, the code can be written as BFS or DFS, so it is as follows. (It is written in DFS.) (In the end, the point is ** I want to know the distance from the Node because it is painted from a close place **, ** I want to erode the opponent's position more because I act optimally ** ,.)

answerD.cc


#include<iostream>
#include<vector>

using namespace std;
typedef vector< vector<int> > vv;
#define INF 10000000

void dfs(int now,vv& tree,vector<int>& dep,int d,vector<bool>& check){
  dep[now]=d;
  check[now]=true;
  int l=tree[now].size();
  for(int i=0;i<l;i++){
    if(check[tree[now][i]]==false){dfs(tree[now][i],tree,dep,d+1,check);}
  }
}

int main(){
  int n;cin >> n;
  vv tree(n);
  for(int i=0;i<n-1;i++){
    int a,b;cin >> a >> b;
    tree[a-1].push_back(b-1);
    tree[b-1].push_back(a-1);
  }
  vector<int> dep1(n,INF);
  vector<bool> check1(n,false);
  vector<int> dep2(n,INF);
  vector<bool> check2(n,false);
  dfs(0,tree,dep1,0,check1);
  dfs(n-1,tree,dep2,0,check2);
  int c1=0;int c2=0;
  for(int i=0;i<n;i++){
    if(dep1[i]<=dep2[i]){
      c1+=1;
    }else{
      c2+=1;
    }
  }
  if(c1>c2){
    cout << "Fennec" << endl;
  }else{
    cout << "Snuke" << endl;
  }
}

Recommended Posts

AtCoder Beginner Contest 102 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 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 105 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
AtCoder Beginner Contest 114 Review of past questions