A - Penalty Kick
Problem Statement
Takahashi will have N penalty kicks in a soccer match.
For the i-th penalty kick, he will fail if i is a multiple of 3, and succeed otherwise.
Print the results of his penalty kicks.
Constraints
- 1≤N≤100
- All inputs are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Print a string of length N representing the results of Takahashi's penalty kicks. The i-th character (1≤i≤N) should be
o
if Takahashi succeeds in the i-th penalty kick, andx
if he fails.Sample Input 1
7
Sample Output 1
ooxooxo
C++代码 :
#include<iostream>
using namespace std;
int main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
if(i%3) cout<<'o';
else cout<<'x';
}
B - Farthest Point
Problem Statement
On the xy-plane, there are N points with ID numbers from 11 to N. Point i is located at coordinates(Xi,Yi), and no two points have the same coordinates.
From each point, find the farthest point and print its ID number. If multiple points are the farthest, print the smallest of the ID numbers of those points.
Here, we use the Euclidean distance: for two points (x1,y1) and(x2,y2), the distance between them is sqrt((x1−x2)^2+(y1−y2)^2).
Constraints
2≤N≤100
−1000≤Xi,Yi≤1000
(Xi,Yi)≠(Xj,Yj) if i≠j.
All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print N lines. The i-th line should contain the ID number of the farthest point from point i.
Sample Input 1
4 0 0 2 4 5 0 3 4
Sample Output 1
3 3 1 1
C++代码:
#include<iostream>
#include<cmath>
using namespace std;
const int N=1005;
typedef pair<double,double> PII;
PII q[N];
double get_dist(int i,int j)
{
int x1=q[i].first,y1=q[i].second;
int x2=q[j].first,y2=q[j].second;
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
{
double a,b;cin>>a>>b;
q[i]={a,b};
}
for(int i=1;i<=n;i++)
{
double max_=0;
int u;
for(int j=1;j<=n;j++)
{
if(i==j) continue;
double dist=get_dist(i,j);
if(max_<dist)
{
max_=dist;
u=j;
}
}
cout<<u<<endl;
}
}
C - Colorful Beans
Problem Statement
There are N types of beans, one bean of each type. The i-th type of bean has a deliciousness of Ai and a color of Ci. The beans are mixed and can only be distinguished by color.
You will choose one color of beans and eat one bean of that color. By selecting the optimal color, maximize the minimum possible deliciousness of the bean you eat.
Constraints
- 1≤N≤2×10^5
- 1≤Ai≤10^9
- 1≤Ci≤10^9
- All input values are integers.
C++代码:
#include<iostream>
#include<map>
using namespace std;
map<int,int> h;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;cin>>n;
int max_=0;
for(int i=1;i<=n;i++)
{
int a,b;cin>>a>>b;
if(!h[b]) h[b]=a;
else
{
if(h[b]>a) h[b]=a;
}
}
for(auto it:h)
{
max_=max(max_,it.second);
}
cout<<max_;
return 0;
}
D - Medicines on Grid
Problem Statement
There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left. The state of each cell is represented by the character Ai,j, which means the following:
.
: An empty cell.#
: An obstacle.S
: An empty cell and the start point.T
: An empty cell and the goal point.Takahashi can move from his current cell to a vertically or horizontally adjacent empty cell by consuming 1 energy. He cannot move if his energy is 00, nor can he exit the grid.
There are N medicines in the grid. The i-th medicine is at the empty cell (Ri,Ci) and can be used to set the energy to Ei. Note that the energy does not necessarily increase. He can use the medicine in his current cell. The used medicine will disappear.
Takahashi starts at the start point with 0 energy and wants to reach the goal point. Determine if this is possible.
Constraints
- 1≤H,W≤200
- Ai,j is one of
.
,#
,S
, andT
.- Each of
S
andT
exists exactly once in Ai,j.- 1≤N≤300
- 1≤Ri≤H
- 1≤Ci≤W
- (Ri,Ci)≠(Rj,Cj) if i≠j.
- ARi,Ci is not
#
.- 1≤Ei≤HW
C++代码:
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
typedef pair<int,PII> PIII;
const int N=305;
char a[N][N];
int g[N][N],h,w,n,sx,sy,ex,ey;
int dist[N][N];
int ne[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool st[N][N];
void Dijkstra()
{
memset(dist,-1,sizeof dist);
dist[sx][sy]= g[sx][sy];
priority_queue<PIII> heap;
heap.push({dist[sx][sy],{sx,sy}});
while(heap.size())
{
PIII it=heap.top();heap.pop();
int energe=it.x,xx=it.y.x,yy=it.y.y;
if(xx==ex&&yy==ey)
{
cout<<"Yes";
return ;
}
for(int i=0;i<=3;i++)
{
int tx=xx+ne[i][0],ty=yy+ne[i][1];
if(tx>=1&&tx<=h&&ty>=1&&ty<=w&&a[tx][ty]!='#')
{
if(dist[tx][ty]<energe-1)
{
dist[tx][ty]= ((energe-1)<g[tx][ty]?g[tx][ty]:(energe-1));
heap.push({dist[tx][ty],{tx,ty}});
}
}
}
}
cout<<"No";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>h>>w;
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
{
cin>>a[i][j];
if(a[i][j]=='S') sx=i,sy=j;
if(a[i][j]=='T') ex=i,ey=j;
}
cin>>n;
for(int i=1;i<=n;i++)
{
int x,y,c;cin>>x>>y>>c;
g[x][y]=c;
}
Dijkstra();
return 0;
}
E - Minimize Sum of Distances
Problem Statement
You are given a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects vertices Ai and Bi.
You are also given a sequence of positive integers C=(C1,C2,…,CN) of length N. Let d(a,b) be the number of edges between vertices a and b, and for x=1,2,…,N, let f(x)=i=1∑N(Ci×d(x,i)). Find min 1≤v≤N f(v).
Constraints
- 1≤N≤10^5
- 1≤Ai,Bi≤N
- The given graph is a tree.
- 1≤Ci≤10^9
C++代码:
#include<iostream>
#include<cstring>
#include<vector>
#define int long long
using namespace std;
const int N=1e5+5;
vector<int> edge[N];
int n,depth[N],f[N],sum,c[N],sz[N],ans=1e19;
void dfs1(int u,int fa)
{
sz[u]=c[u];
depth[u]=depth[fa]+1;
for(auto it:edge[u])
{
if(it==fa) continue;
dfs1(it,u);
sz[u]+=sz[it];
}
}
void dfs2(int u,int fa)
{
ans=min(ans,f[u]);
for(auto it:edge[u])
{
if(it==fa) continue;
f[it]=f[u]+sum-sz[it]-sz[it];
dfs2(it,u);
}
}
signed main()
{
cin>>n;
for(int i=1;i<n;i++)
{
int a,b;cin>>a>>b;
edge[a].push_back(b);
edge[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
cin>>c[i];
sum+=c[i];
}
depth[0]=-1;
dfs1(1,0);
for(int i=1;i<=n;i++) f[1]+=c[i]*depth[i];
dfs2(1,0);
cout<<ans;
}