2024.12.8 周日

2024.12.8 周日


Q1. 1100

给定n,k。构造长度为n的数组,元素之和为k,且按位或和的值在二进制下1最多。

Q2. 1300

给定两行01串,从(1,1)走到(2,n),每次只能向右或向下走。问路径经过的最小字符串以及方案数。

Q3. 1200

将一个数组分为连续的k>1段,使每段的MEX(未出现的最小自然数)相等。

Q4. 1400

给定n,m,k(1e9)与长度为n数组vec:第i天价格。如果第i天买x张票,那么以后每天的价格+x。每天最多买m张票,问买k张票的最小代价。

------------------------独自思考分割线------------------------

  • 这次的4道题都特别好。Q1wa了一发,Q2:45分钟,Q3:50分钟,Q4没出。

A1. 2点

1.假思路:一个数构造一个1:1 2 4 8...

2.本质是考虑1数量的上限,构造小于等于k的数中1最多的数,最多2项即可。

A2. 2点

1.找到目标字符串可以bfs,也可用点思维:发现仅有一个拐点(向下走),枚举寻找拐点,判断方式:贪心一下发现a[2][i]<a[1][i+1]时作为拐点最优。

2.方案数用一下dp:f[i][j]=f[i-1][j]+f[i][j-1];

vector<array<int,3>>数组开小了调了半天。

A3. 2点

1.发现小性质:若可以分为k段则一定可分为2段。

2.枚举分界点,数据结构维护左右MEX,更新加判断。

A4.

1.思路不放了,放点思想。有点难度的贪心:一种贪心策略满足两层条件。

2.微扰法代替数学证明:简单易懂。

------------------------代码分割线------------------------

A1.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, k;
    cin >> n >> k;
    vector<int> res;
    for (int i = 0;; i++)
        if (k >> i == 0)
        {
            int t = (1ll << i - 1) - 1;
            res.push_back(t);
            break;
        }
    res.push_back(k - res[0]);
    for (int i = res.size() + 1; i <= n; i++)
        res.push_back(0);
    if (n == 1)
        res.assign(1, k);
    for (auto v : res)
        cout << v << ' ';
    cout << endl;
    // for (int i = 1; i < n; i++)
    // {
    //     if (k < st)
    //         break;
    //     k -= st;
    //     res.push_back(st);
    //     st <<= 1;
    // }
    // res.push_back(k);
    // for (int i = res.size() + 1; i <= n; i++)
    //     res.push_back(0);
    // for (auto v : res)
    //     cout << v << ' ';
    // cout << endl;
}

A2.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    vector<string> a(3);
    for (int i = 1; i <= 2; i++)
    {
        cin >> a[i];
        a[i] = " " + a[i];
    }
    int o = n;
    for (int i = 1; i < n; i++)
        if (a[2][i] < a[1][i + 1])
        {
            o = i;
            break;
        }
    string res = a[1].substr(1, o) + a[2].substr(o);
    cout << res << endl;
    res = " " + res;
    vector<vector<int>> f(3, vector<int>(n + 1));
    f[1][0] = 1;
    for (int i = 1; i <= 2; i++)
        for (int j = 1; j <= n; j++)
        {
            int t = i == 1 ? 0 : 1;
            if (a[i][j] - res[t + j])
                continue;
            if (i == 1)
                f[i][j] = f[i][j - 1];
            else
                f[i][j] = f[i - 1][j] + f[i][j - 1];
        }
    cout << f[2][n] << endl;
}

A3.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}
void _()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    map<int, int> MEX_l, MEX_r, l, r;
    l[a[1]]++;
    for (int i = 2; i <= n; i++)
        r[a[i]]++;
    for (int i = 0; i <= n; i++)
        if (!l.count(i))
            MEX_l[i] = 1;
    for (int i = 0; i <= n; i++)
        if (!r.count(i))
            MEX_r[i] = 1;
    auto seg = [](int l, int r)
    {
        cout << l << " " << r << endl;
    };
    auto same = [&]()
    {
        return (*MEX_l.begin()).first == (*MEX_r.begin()).first;
    };
    auto get = [&]()
    {
        bug2((*MEX_l.begin()).first, (*MEX_r.begin()).first);
    };
    // get();
    if (same())
    {
        // get();
        cout << 2 << endl;
        seg(1, 1);
        seg(2, n);
        return;
    }
    for (int i = 2; i < n; i++)
    {
        if (MEX_l.count(a[i]))
            MEX_l.erase(a[i]);
        l[a[i]]++;
        r[a[i]]--;
        if (r[a[i]] == 0)
            MEX_r[a[i]] = 1;
        if (same())
        {
            // get();
            cout << 2 << endl;
            seg(1, i);
            seg(i + 1, n);
            return;
        }
    }
    cout << -1 << endl;
}
// void _()
// {
//     int n;
//     cin >> n;
//     vector<int> a(n + 1);
//     for (int i = 1; i <= n; i++)
//         cin >> a[i];
//     vector<int> di;
//     for (int i = 1; i <= n / i; i++)
//         if (n % i == 0)
//             di.push_back(i);
//     // bug(di.size()); // 17
//     for (auto len : di)
//     {
//         // bug(len);
//         vector<pair<int, int>> res;
//         map<int, int> ans;
//         for (int r = len; r <= n; r += len)
//         {
//             int l = r - len + 1;
//             res.push_back({l, r});
//             map<int, int> has;
//             for (int i = l; i <= r; i++)
//                 has[a[i]] = 1;
//             for (int i = 0;; i++)
//                 if (!has[i])
//                 {
//                     ans[i] = 1;
//                     break;
//                 }
//         }
//         if (ans.size() == 1)
//         {
//             cout << n / len << endl;
//             for (auto [l, r] : res)
//                 cout << l << ' ' << r << endl;
//             return;
//         }
//     }
//     cout << -1 << endl;
// }

A4.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> vec(n);
    for (int &x : vec)
        cin >> x;
    sort(vec.begin(), vec.end());
    int res = 0, pre = 0;
    for (auto v : vec)
    {
        int buy = min(k, m);
        k -= buy;
        res += buy * (pre + v);
        pre += buy;
    }
    cout << res << endl;
}
posted @   Jkke  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示