The second past question that I have already solved
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")
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]))
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)
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