说明:本文为co_fun算法集训活动参考答案,有兴趣参与co_fun算法集训的同学可以关注b站 “co_fun算法内推社区” 获得群号。co_fun算法集训活动,针对算法基础一般的同学,从常见考点开始训练,每日三道题,会有讲解。三个月攻坚大厂笔试面试算法题。
2022.4.5-1
#include<bits/stdc++.h>
#define int long long
#define MAXN 200005
using namespace std;
vector<int> edge[MAXN];
int val[MAXN],ans[MAXN];
void dfs(int now,int d)
{
ans[d]=max(ans[d],val[now]);
for(int i=0;i<edge[now].size();i++)
dfs(edge[now][i],d+1);
}
signed main()
{
int n;cin>>n;
int rt;
for(int i=1;i<=n;i++)
{
int f;cin>>f>>val[i];
if(f)edge[f].push_back(i);
else rt=i;
}
dfs(rt,1);
for(int i=1;i<=n;i++)
if(ans[i])
cout<<ans[i]<<' ';
}
2022.4.5-2
#include<iostream>
#define int long long
#define MAXN 2000005
using namespace std;
int pre[MAXN],suf[MAXN];
int a[MAXN];
signed main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
{
char c;cin>>c;
a[i]=c-'0';
}
for(int i=1;i<=n;i++)
pre[i]+=a[i]+pre[i-1];
for(int i=n;i>=1;i--)
suf[i]+=1-a[i]+suf[i+1];
int ans=0x3f3f3f3f;
for(int i=0;i<=n;i++)
ans=min(ans,pre[i]+suf[i+1]);
cout<<ans<<endl;
}
2022.4.5-3
#include<iostream>
#define int long long
#define MAXN 200005
using namespace std;
int vis[505][505];
int v[505][505];
int x[4]={0,1,0,-1};
int y[4]={1,0,-1,0};
signed main()
{
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>v[i][j];
for(int i=1;i<=n;i++)
vis[i][0]=vis[i][m+1]=1;
for(int i=1;i<=m;i++)
vis[0][i]=vis[n+1][i]=1;
int nx=1,ny=0,pos=0;
for(int i=0;i<250000;i++)
{
if(vis[nx+x[pos]][ny+y[pos]])
pos=(pos+1)%4;
if(vis[nx+x[pos]][ny+y[pos]])
continue;
nx+=x[pos];
ny+=y[pos];
cout<<v[nx][ny]<<' ';
vis[nx][ny]=1;
}
}
2022.4.6-1
#include<iostream>
#include<vector>
#define int long long
#define MAXN 2000005
using namespace std;
vector<int> edge[MAXN];
int ans=0;
void dfs(int now)
{
if(edge[now].size()==0)ans++;
for(int i=0;i<edge[now].size();i++)
dfs(edge[now][i]);
}
signed main()
{
int n;cin>>n;
for(int i=1;i<n;i++)
{
int a,b;cin>>a>>b;
edge[a].push_back(b);
}
dfs(1);
cout<<ans<<endl;
}
2022.4.6-2
#include<bits/stdc++.h>
#define int long long
#define MAXN 2000005
using namespace std;
struct P
{
int l,r;
bool operator<(const P& a)const&{
return r<a.r;
}
};
vector<P> v;
signed main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
{
int a,b;cin>>a>>b;
if(a>b)swap(a,b);
v.push_back(P{a,b});
}
sort(v.begin(),v.end());
int now=-100000,cnt=0;
for(int i=0;i<v.size();i++)
if(v[i].l>=now)
cnt++,now=v[i].r;
cout<<cnt<<endl;
}
2022.4.6-3
#include<bits/stdc++.h>
#define int long long
#define MAXN 2000005
using namespace std;
unordered_map<string,int> um1,um2;
signed main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
{
string s;cin>>s;
um1[s]++;
sort(s.begin(),s.end());
um2[s]++;
}
int m;cin>>m;
for(int i=1;i<=m;i++)
{
string s;cin>>s;
int t=um1[s];
sort(s.begin(),s.end());
cout<<um2[s]-t<<endl;
}
}
2022.4.7-1
#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;
unordered_map<int,int> um;
int cnt[MAXN];
vector<int> lan;
struct M
{
int a,b;
bool operator<(const M& x)const&{
if(a==x.a)return b<x.b;
return a<x.a;
}
};
M ans;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int l,m,n;cin>>l>>m>>n;
for(int i=1;i<=l;i++)
{
int t;cin>>t;
lan.push_back(t);
}
sort(lan.begin(),lan.end());
int ptr=0;
for(int i=0;i<lan.size();i++)
{
if(!um[lan[i]])
um[lan[i]]=++ptr;
}
for(int i=1;i<=m;i++)
{
int t;cin>>t;
cnt[um[t]]++;
}
vector<M> v;
for(int i=1;i<=n;i++)
{
int a,b;cin>>a>>b;
a=um[a],b=um[b];
int t1=cnt[a-1]+cnt[a]+cnt[a+1];
int t2=cnt[b-1]+cnt[b]+cnt[b+1];
v.push_back(M{t1,t2});
}
sort(v.begin(),v.end());
int len=v.size()-1;
cout<<v[len].a<<' '<<v[len].b<<endl;
}
2022.4.7-2
#include<bits/stdc++.h>
#define int long long
#define MAXN 200005
using namespace std;
int a[MAXN];
signed main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
int ans=0,MOD=1e9+7;
for(int i=1;i<=n;i++)
ans=(ans+a[i]*(i-0)*(n+1-i))%MOD;
cout<<ans<<endl;
}
2022.4.7-3
#include<bits/stdc++.h>
#define int long long
#define MAXN 200005
using namespace std;
int a[MAXN],k;
bool check(int x)
{
int cnt2=0,cnt5=0;
for(int i=2;i<=x;i*=2)
cnt2+=x/i;
for(int i=5;i<=x;i*=5)
cnt5+=x/i;
return min(cnt2,cnt5)>=k;
}
signed main()
{
cin>>k;
int L=0,R=1e18+7;
while(L<R)
{
int mid=(L+R)/2;
if(check(mid)) R=mid;
else L=mid+1;
}
cout<<L<<endl;
}