其他分享
首页 > 其他分享> > AtCoder Beginner Contest 211 Solution

AtCoder Beginner Contest 211 Solution

作者:互联网

Solution

A - Blood Pressure

纯模拟。。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
double a,b;
double ans;
int main()
{
    cin >> a >> b;
    ans = (a - b) / 3 + b;
    cout << ans << endl;
    return 0;   
}

 

B - Cycle Hit

也是模拟。不过要注意每个目标字符串只能被用一次

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
string a[4];
bool b[4];
int main()
{
    for(int i = 0; i < 4; ++ i)
    {
        cin >> a[i];
        if(a[i] == "3B") b[0] = true;
        else if(a[i] == "HR") b[1] = true;
        else if(a[i] == "2B") b[2] = true;
        else if(a[i] == "H")  b[3] = true;
    }
    if(b[0] && b[1] && b[2] && b[3]) puts("Yes");
    else puts("No");
    return 0;   
}

 

C - chokudai

这题开始稍稍有难度 但难度不大

但是题解的初始化好像写的有点问题qwq

dp [ i ] [ j ]表示前i个字符中匹配目标字符串长度为j的方案数

化整为零,分析集合可以由哪些集合进行转移

容易得到dp状态转移方程

但是要注意 dp[0][0] 也是一种合法方案并且方案数为一。

 

 

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int mod = 1e9 + 7;
int f[N][9];
char s[N];
char x[9] = {'0','c','h','o','k','u','d','a','i'};
int main()
{
    cin >> s + 1;
    int length = strlen(s + 1);
    for(int i = 1; i <= 8; ++ i) f[0][i] = 0;
    for(int i = 1; i <= length; ++ i) f[i][0] = 1;
    f[0][0] = 1;
    for(int i = 1; i <= length; ++ i)
      for(int j = 1; j <= 8; ++ j)
        {
            if(s[i] == x[j]) 
            {
                f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) % mod;
            }
            else f[i][j] = f[i - 1][j];
        }
    cout << f[length][8] << endl;
    return 0;
}

 

D - Number of Shortest paths

统计最短路的个数。

看到边权为一 自然想到bfs。

挺水的。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int n,m;
const int N = 200010;
const int M = 400010;
const int mod = 1e9 + 7;
int h[N],ne[M],e[M],idx = 0;
bool st[N];
inline void add(int a,int b)
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}
int dist[N];
int cnt[N];
int q[N];
inline int bfs()
{
    int hh = 0,tt = 0;
    q[0] = 1;
    st[1] = true;
    memset(dist,0x3f,sizeof dist);
    dist[1] = 0;
    cnt[1] = 1;
    while(hh <= tt)
    {
        int t = q[hh ++];
        for(int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] == 0x3f3f3f3f)
            {
                dist[j] = dist[t] + 1;
                st[j] = true;
                q[++ tt] = j;
                cnt[j] = cnt[t];
            }
            else if(dist[j] == dist[t] + 1)
            {
                cnt[j] = (cnt[j] + cnt[t]) % mod;
            }
        }

    }
    return cnt[n];
}

int main()
{
    memset(h,-1,sizeof h);
    cin >> n  >> m;
    while(m --)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    cout << bfs() << endl;
    return 0;

}

 

E - Red Polyomino

一个搜索题,在一幅图中染色目标点数使得所有被染色的点组成连通块。

dfs即可。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>l_l;
 
vector<string>s;
ll answer=0;
ll n;
vector<ll> dx={1,0,-1,0};
vector<ll> dy={0,1,0,-1};
 
bool valid(ll x,ll y){
    return 0<=x && x<=n-1 && 0<=y && y<=n-1;
}
 
void dfs(ll num){
    if(num==0){
        answer++;
        return;
    }
    
    vector<l_l>next;
    for(int i=0;i<=n-1;i++){
        for(int j=0;j<=n-1;j++){
            if(s[i][j]=='.'){
                bool flag=false;
                for(int z=0;z<=3;z++){
                    ll nxt_i=i+dx[z];
                    ll nxt_j=j+dy[z];
                    if(valid(nxt_i,nxt_j) && s[nxt_i][nxt_j]=='@'){
                        flag=true;
                    }
                }
                if(flag){
                    s[i][j]='@';
                    dfs(num-1);
                    s[i][j]='#';
                    next.push_back({i,j});
                }
            }
        }
    }
    for(l_l pos:next){
        s[pos.first][pos.second]='.';
    }
}
 
signed main(){
    
    ll k;cin>>n>>k;
    s.resize(n);
    for(int i=0;i<=n-1;i++)cin>>s[i];
    
    for(int i=0;i<=n-1;i++){
        for(int j=0;j<=n-1;j++){
            if(s[i][j]=='.'){
                s[i][j]='@';
                dfs(k-1);
                s[i][j]='#';
            }
        }
    }
    cout<<answer<<endl;
    return 0;
}

 

至于F题,九点才来比赛来不及做了QAQ

等我订正。

 

标签:AtCoder,const,211,int,ll,Solution,using,include,true
来源: https://www.cnblogs.com/yjyl0098/p/15057682.html