codeforces 937 div4

文章包含了多个编程题目,涉及模拟算法、条件判断、字符串处理、二进制转换、树形结构分析、状态压缩和哈密顿路径问题的解决方案,展示了不同IT技术在解决实际问题中的应用。
摘要由CSDN通过智能技术生成

A. Stair, Peak, or Neither?

模拟题。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
typedef pair<int, int>PII;
#define x first
#define y second


void solve()
{
    int a, b, c;
    cin >> a >> b >> c;
    if (a < b && b < c)
    {
        cout << "STAIR" << endl;
    }
    else if (a<b && b>c)
    {
        cout << "PEAK" << endl;
    }
    else cout << "NONE" << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int _;
    cin >> _;
    //_ = 1;
    while (_--)
    {
        solve();
    }
    return 0;
}

B. Upscaling

模拟题,根据行和列 模4的余数 进行模拟。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
typedef pair<int, int>PII;
#define x first
#define y second


void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= 2 * n; i++)
    {
        for (int j = 1; j <= 2 * n; j++)
        {
            if (i % 4 == 2 || i % 4 == 1)
            {
                if (j % 4 == 2 || j % 4 == 1)
                {
                    cout << "#";
                }
                else cout << ".";
            }
            else
            {
                if (j % 4 == 2 || j % 4 == 1)
                {
                    cout << ".";
                }
                else cout << "#";
            }
        }
        cout << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int _;
    cin >> _;
    //_ = 1;
    while (_--)
    {
        solve();
    }
    return 0;
}

C. Clock Conversion

直接根据样例模拟,有错误就改正

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
typedef pair<int, int>PII;
#define x first
#define y second


void solve()
{
    string s;
    cin >> s;
    bool f = false,f1=false;
    int hh = (s[0] - '0') * 10 + s[1]-'0';
    int ss = (s[3] - '0') * 10 + s[4]-'0';
    
    if (hh == 12)f = true;
    if (hh > 12)  hh -= 12, f = true,f1=true;

    if (!f1 && hh == 0) hh = 12;

    if (hh >= 10) cout << hh << ":";
    else cout << 0 << hh << ":";

    if (ss >= 10)cout << ss << " ";
    else cout << 0 << ss << " ";

    

    if (f)cout << "PM" << endl;
    else cout << "AM" << endl;

}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int _;
    cin >> _;
    //_ = 1;
    while (_--)
    {
        solve();
    }
    return 0;
}

D. Product of Binary Decimals

预处理出所有的1e5内所有的二进制十进制数。o(6*n)

1.该数本身是否是二进制十进制数

2.然后枚举所有的二进制十进制数,判断是否是n的约数,如果是,就除尽求出。判断最后的n是否是二进制十进制数。O(64*logn)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
typedef pair<int, int>PII;
#define x first
#define y second
vector<int> v;
bool is_B(int x)
{
    while (x)
    {
        int t = x % 10;
        if (t != 1 && t != 0) return false;
        x /= 10;
    }
    return true;
}

void solve()
{
    int n;
    cin >> n;
    if(is_B(n))
    {
        cout<<"YES"<<endl;
        return;
    }
    for (auto t : v)
    {
        if(t<n)
        {
            while(n%t==0)
            {
                n/=t;
            }
        }
    }
    if (is_B(n))cout << "YES"<<endl;
    else cout << "NO" << endl;

}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    for (int i = 1e5;i >= 2;i--)
    {
        if (is_B(i))
        {
            v.push_back(i);
        }
    }


    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

E. Nearly Shortest Repeating Substring

等价与将字符串分割成等长的n/len段,判断每一段是否满足要求

判断的方法是将每一段的对应位置的字符统计出来,如果全部相同的话mx==parts

否则err+=parts-mx;

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
typedef pair<int, int>PII;
#define x first
#define y second

int n;
string s;

bool check(int len)
{
    int err = 0;
    int parts = n / len;

    for (int i = 0;i < len;i++)
    {
        int mx = 0;//存储字符出现最多的一次
        int cnt[30] = {};
        for (int j = i;j < n;j += len)
        {
            int num = s[j] - 'a' + 1;
            cnt[num]++;
            mx = max(cnt[num], mx);
        }
        err += parts - mx;
    }
    return err < 2;
}

void solve()
{

    cin >> n;
    
    cin >> s;

    for (int len = 1;len<=n;len++)
    {
        if (n % len != 0)continue;
        if (check(len))
        {
            cout << len << endl;
            return;
        }
    }

}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);




    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

F. 0, 1, 2, Tree!

二叉树的性质:有两个儿子的节点个数+1=没有儿子的节点个数

贪心的思考,先放有两个儿子节点的顶点,再放1,再放0,这样使得高度最小

然后进行模拟。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
typedef pair<int, int>PII;
#define x first
#define y second

int pows[20];

void solve()
{
    int a, b, c;
    cin >> a >> b >> c;
    if (a + 1 != c)
    {
        cout << -1 << endl;
        return;
    }

    if (!a)
    {
        cout << b << endl;
        return;
    }

    int d = ceil(log2(a + 1));//上取整
    int rem = pows[d] - 1 - a;
    if (b <= rem)
    {
        cout << d << endl;
        return;
    }

    b -= rem;

    int sz = a - (pows[d - 1] - 1);
    sz = sz * 2 + rem;
    d += (b + sz - 1) / sz;
    cout << d << endl;
    


    

}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    pows[0] = 1;
    for (int i = 1;i < 20;i++)
    {
        pows[i] = pows[i - 1] * 2;
    }

    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

G. Shuffling Songs

n<=16考虑状态压缩。哈密顿路径问题,f[i][j]表示状态是i,最后节点是j对应的方案是否符合要求。

按照最后一步是哪一首歌曲k变到j进行划分集合。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
typedef pair<string, string>PII;
#define x first
#define y second

const int N = 20;

bool f[1 << N][N];

vector<int>state[N];//存储可以由哪个点转移而来

PII q[N];

int get(int x)
{
    int res = 0;
    while (x)
    {
        res++;
        x = x - (x & -x);
    }
    return res;
}
void solve()
{
    int n;
    cin >> n;

    memset(f,0,sizeof f);
    
    
    for (int i = 0;i < n;i++)
    {
        cin >> q[i].x >> q[i].y;
    }

    for (int i = 0;i < n;i++)
    {
        state[i].clear();
        for (int j = 0;j < n;j++)
        {
            if (i == j)continue;
            if (q[i].x == q[j].x || q[i].y == q[j].y)
            {
                state[i].push_back(j);//存储可以转移的编号
            }
        }
    }
    
    

    //初始化:每一首歌曲都能符合要求
    for(int i=0;i<n;i++)
    {
        f[1<<i][i]=true;
    }

    for (int i = 0;i < 1 << n;i++)
    {
        for (int j = 0;j < n;j++)
        {
            if (i >> j & 1)//当前状态合法
            {
                for (auto k : state[j]) //编号为k的歌曲转移到j
                {
                    if(i-(1<<j)>>k&1)
                    {
                        f[i][j]=f[i][j]||f[i-(1<<j)][k];
                    }
                }
            }
        }
    }
    int ans=1e9;
    
    for (int i = 0;i < 1<<n;i++)
    {
        for (int j = 0;j < n;j++)
        {
            if (i >> j & 1)
            {
                if (f[i][j])
                {
                    ans=min(ans,n-get(i));
                }
            }
        }
    }
    cout<<ans<<endl;

}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);


    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值