Atcoder Beginner Contest 420个人题解(部分)
A - What month is it?
签到题,输出\((x+y-1) mod 12+1\)即可
B - Most Minority
签到题,暴力枚举两次,第一次枚举每次投票的较少人数,再遍历一次所有人,给符合条件者加\(1\)即可
AC代码:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu long long unsigned int
#define db double
#define endl '\n'
#define PII pair<ll,ll>
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;
const ll nn=2e5+5;
const ll mm=3e5+5;
const ll kk=20;
const ll INF=1e18;
/*
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<48||ch>57)
{
if(ch=='-')
f=-1;
ch=nc();
}
while(ch>=48&&ch<=57)
x=x*10+ch-48,ch=nc();
return x*f;
}
*/
/*
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
*/
void solve()
{
ll n,m;cin>>n>>m;
vector<string>v(n+1);
vector<ll>ans(n+1);
for(ll i=1;i<=n;i++)
{
cin>>v[i];
}
for(ll i=0;i<m;i++)
{
ll cnt0=0,cnt1=0;
for(ll j=1;j<=n;j++)
{
if(v[j][i]=='0')cnt0++;
else cnt1++;
}
if(cnt0<cnt1)
{
for(ll j=1;j<=n;j++)
{
if(v[j][i]=='0')ans[j]++;
}
}
else{
for(ll j=1;j<=n;j++)
{
if(v[j][i]=='1')ans[j]++;
}
}
}
ll mx=0;
for(ll i=1;i<=n;i++)
{
mx=max(ans[i],mx);
}
for(ll i=1;i<=n;i++)
{
if(ans[i]==mx)cout<<i<<" ";
}
cout<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
ll tt=1;
//tt=read();
//cin>>tt;
while(tt--)
solve();
return 0;
}
C - Sum of Min Query
已知一次修改只改变一个元素的值,故容易想到在线处理的思路:
先预处理出初始的\(ans\)值,对于每次修改,先令\(ans\)减去\(x\)下标上的贡献,修改后再将新贡献计入\(ans\)即可
AC代码:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu long long unsigned int
#define db double
#define endl '\n'
#define PII pair<ll,ll>
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;
const ll nn=2e5+5;
const ll mm=3e5+5;
const ll kk=20;
const ll INF=1e18;
/*
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<48||ch>57)
{
if(ch=='-')
f=-1;
ch=nc();
}
while(ch>=48&&ch<=57)
x=x*10+ch-48,ch=nc();
return x*f;
}
*/
/*
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
*/
void solve()
{
ll n,q;cin>>n>>q;
vector<ll>a(n+1),b(n+1);
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
for(ll i=1;i<=n;i++)
{
cin>>b[i];
}
ll ans=0;
for(ll i=1;i<=n;i++)
{
ans+=min(a[i],b[i]);
}
for(ll i=1;i<=q;i++)
{
char c;
ll x,y;cin>>c>>x>>y;
ans-=min(a[x],b[x]);
if(c=='A')
{
a[x]=y;
}
else{
b[x]=y;
}
ans+=min(a[x],b[x]);
cout<<ans<<endl;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
ll tt=1;
//tt=read();
//cin>>tt;
while(tt--)
solve();
return 0;
}
D - Toggle Maze
思考本题与普通\(BFS\)的区别:需要考虑门的开关状态
故除\(x, y\)坐标外,还需维护一个状态\(t,t\in[0,1]\),规定\(t=0\)时\(o\)可走,\(x\)不可走,\(t=1\)时相反,就转化成了平凡的\(BFS\)
AC代码:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu long long unsigned int
#define db double
#define endl '\n'
#define PII pair<ll,ll>
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;
const ll nn=2e5+5;
const ll mm=3e5+5;
const ll kk=20;
const ll INF=1e18;
/*
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<48||ch>57)
{
if(ch=='-')
f=-1;
ch=nc();
}
while(ch>=48&&ch<=57)
x=x*10+ch-48,ch=nc();
return x*f;
}
*/
/*
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
*/
struct node{
ll x,y,t;
};
void solve()
{
ll n,m;cin>>n>>m;
vector<string>a(n);
for(ll i=0;i<n;i++)cin>>a[i];
ll sx=0,sy=0,gx=0,gy=0;
for(ll i=0;i<n;i++)
{
for(ll j=0;j<m;j++)
{
if(a[i][j]=='S')
{
sx=i,sy=j;
}
if(a[i][j]=='G')
{
gx=i,gy=j;
}
}
}
vector<vector<array<ll,2>>>dis(n,vector<array<ll,2>>(m,{INF,INF}));
queue<node>q;
dis[sx][sy][0]=0;
q.push({sx,sy,0});
ll w[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
auto fuck=[&](char now,ll t)->bool
{
if(now=='#')return false;
else if(now=='.'||now=='S'||now=='G'||now=='?')return true;
else if(now=='o')
{
if(t==0)return true;
return false;
}
else{
if(t==1)return true;
return false;
}
};
while(!q.empty())
{
auto [x,y,t]=q.front();
q.pop();
ll tmp=dis[x][y][t];
if(x==gx&&y==gy)
{
cout<<tmp<<endl;
return;
}
for(ll k=0;k<4;k++)
{
ll nx=x+w[k][0],ny=y+w[k][1];
if(nx<0||nx>=n||ny<0||ny>=m)continue;
char now=a[nx][ny];
if(!fuck(now,t))continue;
ll nt=t;
if(now=='?')nt^=1;
if(dis[nx][ny][nt]>tmp+1)
{
dis[nx][ny][nt]=tmp+1;
q.push({nx,ny,nt});
}
}
}
cout<< -1<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
ll tt=1;
//tt=read();
//cin>>tt;
while(tt--)
solve();
return 0;
}
E - Reachability Query
无向图加边染色判连通性,一眼并查集
对每一个连通块,额外维护一个块内黑色节点数量\(bb[root](root)为根\),对于任意节点\(v\)的颜色反转,只需根据其反转前的颜色对连通块黑色节点总数做修改即可,对于类型3的查询,只需判断\(v\)节点所在连通块有无黑色节点即可
AC代码:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu long long unsigned int
#define db double
#define endl '\n'
#define PII pair<ll,ll>
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;
const ll nn=2e5+5;
const ll mm=3e5+5;
const ll kk=20;
const ll INF=1e18;
/*
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<48||ch>57)
{
if(ch=='-')
f=-1;
ch=nc();
}
while(ch>=48&&ch<=57)
x=x*10+ch-48,ch=nc();
return x*f;
}
*/
/*
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
*/
struct DSU{
vector<ll>p,sz,bb;
DSU(ll n):p(n+1),sz(n+1,1),bb(n+1,0)
{
iota(p.begin(),p.end(),0);
}
ll find(ll x)
{
if(p[x]==x)return x;
return p[x]=find(p[x]);
}
void unite(ll a,ll b)
{
a=find(a),b=find(b);
if(a==b)return;
if(sz[a]<sz[b])swap(a,b);
p[b]=a;
sz[a]+=sz[b];
bb[a]+=bb[b];
}
void add1(ll x)
{
x=find(x);
bb[x]++;
}
void re1(ll x)
{
x=find(x);
bb[x]--;
}
bool fuck(ll x)
{
x=find(x);
if(bb[x]>0)return true;
return false;
}
};
void solve()
{
ll n,q;cin>>n>>q;
DSU dsu(n);
vector<ll>col(n+1);
for(ll i=1;i<=q;i++)
{
ll t;cin>>t;
if(t==1)
{
ll x,y;cin>>x>>y;
dsu.unite(x,y);
}
else if(t==2)
{
ll x;cin>>x;
if(!col[x])
{
col[x]=1;
dsu.add1(x);
}
else{
col[x]=0;
dsu.re1(x);
}
}
else{
ll x;cin>>x;
if(dsu.fuck(x))cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
ll tt=1;
//tt=read();
//cin>>tt;
while(tt--)
solve();
return 0;
}
愿称之为史上最速题解
打cf去了

浙公网安备 33010602011771号