Codeforces Round 911 (Div. 2) [A-C]
A. Cover in Water(Rating : 800)
思路
看题目的示例就很难容易发现,水方格就像MC里面的水方块一样,当空格左右两边都有水的时候该空格会被自动填满,由于操作2,我们便可以做出“无限水”,因此,只要有三个及以上的空白格相连,我们只需要两次操作一构建出“无限水”就能填满所有格;相反,如果没有三个及以上的空白格相连,我们只能一个一个去填
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define pb push_back
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
const int N = 2e5+10, INF = -1e9, MAX = 1e9;
ll mod=998244353;
int n;
string s;
void solve()
{
cin>>n>>s;
s="#"+s+"#";
int key=0;
int num=0;
for(int i=0;i<n+2;i++){
if(s[i]=='.'){
key++;
if(key>=3){
cout<<2<<endl;
return ;
}
}
else{
num+=key;key=0;
}
}
cout<<num<<endl;
return ;
}
int main()
{
fast();
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
B. Laura and Operations(Rating : 900)
思路
贪心:以保留a为例:我们发现当b=c的时候一定能成功,沿着这个思路走,先把b和c进行abs(b-c)次减少操作,假设c<=b,此时c=0,b=abs(b-c),本着让b=c的思路,如果可以,就对a和b进行abs(b-c)/2次减少操作,此时就可以做到b=c=abs(b-c)/2,即可以成功,但是如果abs(b-c)不能被2整除,就无法做到b=c,即不能成功,综上:只判断abs(b-c)是否能被整除即可
代码
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define pb push_back
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
const int N = 2e5+10, INF = -1e9, MAX = 1e9;
ll mod=998244353;
int a,b,c;
void solve()
{
cin>>a>>b>>c;
if(abs(b-c)%2==0)cout<<1<<" ";
else cout<<0<<" ";
if(abs(a-c)%2==0)cout<<1<<" ";
else cout<<0<<" ";
if(abs(b-a)%2==0)cout<<1<<endl;
else cout<<0<<endl;
return ;
}
int main()
{
fast();
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
C. Anji's Binary Tree(Rating : 1300)
思路
DFS:一个节点需要储存左子、右子和方向三个数据,还有,虽然是二叉树,但是节点编号混乱,用heap储存很复杂,所以还是用邻接表储存,但是由于需要保存三个信息,我开了个结构体,然后就是板子,BFS即可
代码
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define pb push_back
#define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
const int N = 3e5+10, INF = -1e9, MAX = 1e9;
ll mod=998244353;
typedef struct dian{
int left;
int right;
char move;
}D;
int n;
string s;
vector<D> v(N);
int out;
void dfs(int x,int ans){
if(x==0){
return ;
}
else if(v[x].left==0&&v[x].right==0){
out=min(out,ans);
return ;
}
else{
if(v[x].move=='L'){
dfs(v[x].left,ans);
v[x].move='R';
ans++;
dfs(v[x].right,ans);
v[x].move='L';
ans--;
}
else if(v[x].move=='R'){
dfs(v[x].right,ans);
v[x].move='L';
ans++;
dfs(v[x].left,ans);
v[x].move='R';
ans--;
}
else if(v[x].move=='U'){
v[x].move='R';
ans++;
dfs(v[x].right,ans);
v[x].move='U';
ans--;
v[x].move='L';
ans++;
dfs(v[x].left,ans);
v[x].move='U';
ans--;
}
}
}
void solve()
{
cin>>n;
cin>>s;
out=MAX;
s=" "+s;
int l,r;
for(int i=1;i<=n;i++){
cin>>l>>r;
v[i].left=l;
v[i].right=r;
v[i].move=s[i];
}
dfs(1,0);
cout<<out<<endl;
}
int main()
{
fast();
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}

浙公网安备 33010602011771号