A . Ichihime and Triangle
Link: A. Ichihime and Triangle
Title :
Give four integers a, b, c, d, and choose three numbers from [a, b], [b, c], [c, d] to form a triangle.
Idea : just
choose one b and two c, so that it can form a triangle.
Code :
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=5e3+7;
int main (){
int k;
cin>>k;
while(k--){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
printf ("%d %d %d\n",a,c,c);
}
}
B. Kana and Dragon Quest game
链接: B. Kana and Dragon Quest game
The meaning of problems :
monster blood initial x, operations may be performed n 1, m 2 operations
Operation 1: Monster blood into n / 2 + 10,
Operation 2: Monster blood into n-10).
Q Finally, the blood can cause less than or equal to 0;
ideas :
- For operation 1, the more the monster's current blood volume, the more deductions, and operation two deduct blood volume is certain.
- So we can greedy for the first operation until the monster's blood volume can no longer be reduced.
- Then just perform the second operation, so it must be optimal.
Code :
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=5e3+7;
int main (){
int k;
cin>>k;
while(k--){
int x,n,m,flag=1;
scanf("%d%d%d",&x,&n,&m);
while(n>0){
if(x-(x)/2<10) break;
x=(x)/2+10;
n--;
}
if(x-m*10>0) printf ("NO\n");
else printf ("YES\n");
}
}
C. Linova and Kingdom
Link to C. Linova and Kingdom
Topic :
Give n cities, which consist of a tree with n-1 roads. Taking 1 as the root node, it is necessary to select k cities from them as industrial cities, so that the contribution value of these k cities is the largest, and the calculation rule of the contribution value is the number of non-industrial cities that this city passes to city 1.
Ideas :
-
If we consider the contribution value as the number of passing cities, then the answer is definitely the top k cities with the largest node depth.
-
Next, consider the number of non-industrial cities with contribution values
-
First, select k points according to the depth of the node from large to small, then when selecting a node, we must have selected all of its child nodes, because the city has become an industrial city, so its contribution The value will be reduced by 1 (because all its children must pass through it to reach 1).
-
So it can be equivalent to the contribution value of each node = node depth-the number of child nodes, and finally sort the contribution value.
Code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=4e5+7;
int n,m,head[maxn],depth[maxn],num=0,cnt[maxn];
int degree[maxn];
struct node{
int next,to;
};
struct node edge[maxn];
void add(int u,int v){
edge[num].next=head[u];
edge[num].to=v;
head[u]=num++;
}
void dfs1(int u, int pre, int d){
depth[u]=d,cnt[u]=1;
for(int i = head[u]; i != -1; i = edge[i].next){
int to = edge[i].to;
if(to == pre) continue;
dfs1(to, u, d + 1);
cnt[u]+=cnt[to];
}
}
int main (){
memset(head,-1,sizeof(head));
int u,v;
ll ans=0;
cin>>n>>m;
for(int i=0;i<n-1;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs1(1,-1,0);
for(int i=1;i<=n;i++) depth[i]-=cnt[i]-1;
sort(depth+1,depth+1+n);
for(int i=0;i<m;i++){
ans+=depth[n-i];
}
printf ("%lld\n",ans);
}
D. Xenia and Colorful Gems
Link: D. Xenia and Colorful Gems
Topic:
For each of the three arrays, choose a number x, y, and z so that (x−y) ^ 2 + (y−z) ^ 2 + (z−x) ^ 2 has the smallest value.
Idea:
First of all, 1e5, too violent is definitely not enough, so we can enumerate only one of the arrays, and then find the number closest to it in the other two arrays. Of course there can be two, and then just enumerate them all. Perform this operation once for all three arrays.
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=4e5+7;
ll a[maxn],b[maxn],c[maxn];
ll f(ll a,ll b, ll c){
return (a-b)*(a-b)+(a-c)*(a-c)+(b-c)*(b-c);
}
int main(){
int k;
cin>>k;
while(k--){
ll ans=2e18+7;
ll px,py,pz;
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
for(int i=0;i<x;i++) scanf("%lld",&a[i]);
for(int i=0;i<y;i++) scanf("%lld",&b[i]);
for(int i=0;i<z;i++) scanf("%lld",&c[i]);
sort(a,a+x);
sort(b,b+y);
sort(c,c+z);
for(int i=0;i<x;i++){
py=lower_bound(b,b+y,a[i])-b;
pz=lower_bound(c,c+z,a[i])-c;
if(py==y) py--;
if(pz==z) pz--;
if(py>0) ans=min(ans,f(a[i],b[py-1],c[pz]));
if(pz>0) ans=min(ans,f(a[i],b[py],c[pz-1]));
ans=min(ans,f(a[i],b[py],c[pz]));
}
for(int i=0;i<y;i++){
px=lower_bound(a,a+x,b[i])-a;
pz=lower_bound(c,c+z,b[i])-c;
if(px==x) px--;
if(pz==z) pz--;
if(px>0) ans=min(ans,f(a[px-1],b[i],c[pz]));
if(pz>0) ans=min(ans,f(a[px],b[i],c[pz-1]));
ans=min(ans,f(a[px],b[i],c[pz]));
}
for(int i=0;i<z;i++){
px=lower_bound(a,a+x,c[i])-a;
py=lower_bound(b,b+y,c[i])-b;
if(py==y) py--;
if(px==x) px--;
if(px>0) ans=min(ans,f(a[px-1],b[py],c[i]));
if(py>0) ans=min(ans,f(a[px],b[py-1],c[i]));
ans=min(ans,f(a[px],b[py],c[i]));
}
printf("%lld\n",ans);
}
}
During the game, the D question was not adjusted, which was a bit uncomfortable