AtCoder Beginner Contest 345 A-D

A - Leftrightarrow

思路:

  • 模拟判断即可

以下是代码部分

#include<bits/stdc++.h>
using namespace std;

using ll = long long;
const int N = 5e1 + 5;

void solve()
{
    string s;
    cin >> s;
    int i;
    if(s[0] == '<')
    {
        int ans = 0;
        for(i = 1; i < s.length(); i ++)
        {
            if(s[i] != '=') break;
            ans ++;
        }
        if(!ans)
        {
            cout << "No\n";
            return ;
        }
        if(s[i] == '>' && i == s.length() - 1)
        {
            cout << "Yes\n";
            return ;
        }
        else
        {
            cout << "No\n";
            return ;
        }
    }
    else
    {
        cout << "No\n";
        return ;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin >> t;
    while(t --)
        solve();

    return 0;
}

以下是jiangly的代码

#include<bits/stdc++.h>
using namespace std;

string s;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> s;
    //            生成s.size() - 2个‘=’
    if(s == '<' + string(s.size() - 2, '=') + '>')
        cout << "Yes\n";
    else
        cout << "No\n";

    return 0;
}

B - Integer Division Returns

思路:

  • 分类讨论正数和0和负数的情况即可

以下是代码部分

#include<bits/stdc++.h>
using namespace std;

using ll = long long;
const int N = 5e1 + 5;

void solve()
{
    ll x;
    cin >> x;
    if(x >= 0)
    {
        if(x % 10)
        {
            cout << x / 10 + 1;
        }
        else
            cout << x / 10;
    }
    else
    {
        cout << x / 10;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin >> t;
    while(t --)
        solve();

    return 0;
}

以下是jiangly的代码

#include<bits/stdc++.h>
using namespace std;

using i64 = long long;

int main()
{
    i64 x;
    cin >> x;
    if(x>0)
        cout << (x - 1) / 10 + 1 << '\n';
    else
        cout << x / 10 << '\n';

    return 0;
}

C - One Time Swap

思路:

  • 正难则反
  • 我们很难正着算出有多少种
    • 所以我们可以反着计算
    • 用总的交换次数减去无效的交换次数即可

以下是代码部分,代码参考来源——jiangly

#include<bits/stdc++.h>
using namespace std;

using i64 = long long;

string s;

int main()
{
    cin >> s;
    //初始化
    int cnt[26]{};
    //统计每一个字母的数量
    for(auto c : s)
        cnt[c - 'a'] ++;
    i64 ans = 0;
    //计算字母同相同字母交换的次数和
    for(int i : cnt)
        ans += 1LL * i * (i - 1) / 2;
    int n = (int)s.size();
    //总的交换次数 - 无效交换次数
    ans = 1LL * n * (n - 1) / 2 - ans;
    //如果有一个字符出现的个数>= 2
    // 那么该字符串可以等于它本身
    // 加上它的本身
    if(ans < 1LL * n * (n - 1) / 2)
        ans ++;
    cout << ans << '\n';

    return 0;
}

D - Tiling

思路:

  • 瓷砖放置的顺序并不会影响结果
  • 所以我们可以用字典序的顺序判断
  • 已知:
    • 瓷砖的放置姿势只能成90°角旋转
    • 长方形的放置方向有两种:横着放or竖着放
    • 正方形放置方向只有一种:无论横着竖着都一样

下面是代码部分,代码参考来源——atcoder官方题解

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; ++i)

#define MAX_N 7
#define MAX_H 10
#define MAX_W 10

int n,h,w;
int a[MAX_N];
int b[MAX_N];
int c[MAX_H][MAX_W];
bool ans;

//状态压缩, 总共有(2^n) - 1种情况, (排除了,所有瓷砖不放上去的情况)

void solve(int unused,int curi,int curj)
{
    bool can;
    //遍历,一直到找到未被覆盖的地方
    // 或者全被遍历完成
    //则跳出循环
    while(c[curi][curj] >= 0)
    {
        curj++;
        if(curj>=w)
        {
            curi++;
            curj=0;
        }
        if(curi>=h) break;
    }
    //如果符合此情况,则此铺瓷砖的方法合法
    if(curi>=h)
    {
        ans=true;
        return;
    }
    //遍历选择瓷砖
    rep(i,n)
    {
        //代表如果使用此瓷砖铺地面
        if(unused & (1<<i))
        {
            can = true;
            //一个格子一个格子的判断
            rep(ii,a[i])
                rep(jj,b[i])
                {
                    //如果未越界
                    if(((curi+ii)<h)&&((curj+jj)<w))
                    {
                        //如果没有与其他瓷砖重合
                        if(c[curi+ii][curj+jj]<0)
                            //标记
                            c[curi+ii][curj+jj]=i;
                        else
                            can = false;
                    }
                    else 
                        can=false;
                }
            //若此状态合法,则递归判断下一个状态
            //否则直接回溯试着下一个放置方向是否合法
            if(can)
                solve(unused^(1<<i),curi,curj);
            //回溯
            rep(ii,a[i])
                rep(jj,b[i])
                    if(((curi+ii)<h)&&((curj+jj)<w))
                        if(c[curi+ii][curj+jj]==i)
                            c[curi+ii][curj+jj]=-1;
            //同样的方法判断一边,不过放置方向相反
            if(a[i]!=b[i])
            {
                can=true;
                rep(ii,b[i])
                    rep(jj,a[i])
                    {
                        if(((curi+ii)<h)&&((curj+jj)<w))
                        {
                            if(c[curi+ii][curj+jj]<0)c[curi+ii][curj+jj]=i;
                            else can=false;
                        }
                        else can=false;
                    }
                if(can)
                    solve(unused^(1<<i),curi,curj);
                rep(ii,b[i])
                    rep(jj,a[i])
                        if(((curi+ii)<h)&&((curj+jj)<w))
                            if(c[curi+ii][curj+jj]==i)
                                c[curi+ii][curj+jj]=-1;
            }
        }
    }
}

int main(){
    cin >> n >> h >> w;
    rep(i,n) cin >> a[i] >> b[i];
    //初始化,地板没有瓷砖铺上
    rep(i,h) rep(j,w) c[i][j]=-1;
    ans=false;
    //如果所有的状态没有一种使得ans被赋值为true,则无法成立。
    solve((1 << n) -1,0,0);
    cout << (ans ? "Yes\n" : "No\n");
}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值