2022年寒假算法训练-1.6

1.6训练日记1:Acwing题目两道

Acwing2022寒假每日一题 笨拙的手指

2058. 笨拙的手指 - AcWing题库

解题思路:取并集 将二进制和三进制的所有可能遍历 取两个相同的十进制数

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

int get(string s, int b)  // 将b进制的数转化成十进制
{
    int res = 0;
    // 秦九韶算法
    for (auto c: s)
        res = res * b + c - '0';
    return res;
}

int main()
{
    string a, b;
    cin >> a >> b;

    unordered_set<int> S;

    for (auto& c: a)
    {
        c ^= 1;
        S.insert(get(a, 2));
        c ^= 1;
    }

    for (auto& c: b)
    {
        char t = c;
        for (int i = 0; i < 3; i ++ )
            if (i + '0' != t)
            {
                c = i + '0';
                int x = get(b, 3);
                if (S.count(x))
                {
                    cout << x << endl;
                    return 0;
                }
            }
        c = t;
    }

    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/2230690/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

注:unordered_set:无序集一种容器

      头文件:#include<unordered_set>

       count方法为检索方法 如果该容器存在该元素则返回1,否则返回0

Acwing2022寒假每日一题 干草堆

2041. 干草堆 - AcWing题库

解题思路:前缀和与差分

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

using namespace std;

const int N = 1000010;

int n, m;
int b[N];

int main()
{
    scanf("%d%d", &n, &m);

    while (m -- )
    {
        int l, r;
        scanf("%d%d", &l, &r);
        b[l] ++, b[r + 1] -- ;
    }

    for (int i = 1; i <= n; i ++ ) b[i] += b[i - 1];

    nth_element(b + 1, b + n / 2 + 1, b + n + 1);

    printf("%d\n", b[n / 2 + 1]);

    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/2230690/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

注:nth_element方法:

       如nth_element(a+0,a+b,a+n) -->在a数组中找到从0到n从小到大排序的第b+1个元素并正确放         在a[b]这个位置上

       头文件:#include<algorithm>

1.5训练日记2:前缀和与差分算法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值