2024.11.30 周六

2024.11.30 周六

  • Q1. 1200
    给定x(<=107),m(<=1018),循环i:1~m,t=i^x,问t为x或m因子的数量。

  • Q2. 1400
    给定n个数,选择四个坐标点组成一个边平行于坐标轴的矩形,问面积最大时选的点。

  • Q3. 1600
    给定一数组(非负),在非零数中每次可选一个数(代价本身)/相邻2个数(代价和的2倍)。问最小次数下最小代价。

  • A1. 补:
    因子的概念,d为p的因子:d<=p/2,且最高位不同。发现枚举到2倍x或x最高位的下一位即可。其他范围均不会出现x,m的因子。

  • A2. 结论的方式决定代码实现的难度:优化结论
    在有解的情况下,结论就是最大值与次小值,次大值与最小值作为边长时最优(推下式子)。

  • A3. 23mins
    单个直接选,偶数相邻即可,奇数的话需要扫一遍枚举奇数位,树状数组求两边和的2倍/先求和的2倍再减去单个的O(n)。

  • atcoder

  1. C. n个人,m盘菜,每人有一个期望值,当菜的美味值大于等于期望值时便会端走它,每盘菜从1~n人面前经过。问最后每盘菜的归属。
  • 30mins-挺有意思的一道题,前缀数组维护前缀最小值,二分找到第一个小于等于菜的美味值的期望值即归属。
  1. D. 问所有满足条件的差值大于某值的数列
  • 30mins-练爆搜dfs剪枝...

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
const int mod = 998244353;
const int N = 10 + 5e5;
void _();
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        _();
    return 0;
}

void _()
{
    int x, m;
    cin >> x >> m;

    auto to = [](int x)
    {
        string s;
        while (x)
        {
            s += to_string(x % 2);
            x >>= 1;
        }
        reverse(s.begin(), s.end());
        return s.size();
    };
    int high_bit = to(x);
    int res = 0;
    for (int i = 1; i <= min(m, 1ll << high_bit); i++)
    {
        if (x - i)
        {
            int t = x ^ i;
            if (x % t == 0 || i % t == 0)
            {
                res++;
            }
        }
    }
    cout << res << 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);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    map<int, int> cnt1;
    int cnt_2 = 0;
    for (int i = 0, x; i < n; i++)
    {
        cin >> x;
        cnt1[x]++;
    }
    for (auto [c, cnt] : cnt1)
        if (cnt > 1)
            cnt_2 += cnt >> 1;
    if (cnt_2 < 4)
    {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
    vector<int> x(4), y(4);
    map<int, int> cnt;
    vector<int> a;
    for (auto [x, ct] : cnt1)
    {
        int cnt = ct >> 1;
        while (cnt--)
            a.push_back(x);
    }
    sort(a.begin(), a.end());
    int maxw = a.back(), maxw1 = a[a.size() - 2], minw = a[0], minw1 = a[1];

    x[0] = x[1] = minw;
    x[2] = x[3] = maxw1;
    y[0] = y[2] = minw1;
    y[1] = y[3] = maxw;
    for (int i = 0; i < 4; i++)
        cout << x[i] << ' ' << y[i] << ' ';
    cout << endl;
}

// void _()
// {
//     int n;
//     cin >> n;
//     map<int, int> cnt;
//     int cnt_2 = 0, cnt_4 = 1e10;
//     for (int i = 0, x; i < n; i++)
//     {
//         cin >> x;
//         cnt[x]++;
//         if (cnt[x] >= 4)
//             cnt_4 = x;
//     }
//     for (auto [c, cnt] : cnt)
//         if (cnt > 1)
//             cnt_2 += cnt >> 1;
//     if (cnt_2 < 4)
//     {
//         cout << "NO" << endl;
//         return;
//     }
//     cout << "YES" << endl;
//     vector<int> x(4), y(4);
//     struct Node
//     {
//         int x, cnt;
//     };
//     vector<Node> a;
//     for (auto [x, cnt] : cnt)
//         if (cnt > 1)
//             a.push_back({x, cnt});
//     if (a.size() < 4)
//     {
//         int idx = -1;
//         for (int i = 0; i < a.size(); i++)
//             if (a[i].cnt > 1 && idx < 4)
//             {
//                 x[++idx] = a[i].x;
//                 x[++idx] = a[i].x;
//                 a[i].cnt -= 2;
//             }
//         idx = -1;
//         for (int i = 0; i < a.size(); i++)
//             if (a[i].cnt > 1 && idx < 4)
//             {
//                 y[++idx] = a[i].x;
//                 y[++idx] = a[i].x;
//                 a[i].cnt -= 2;
//             }
//     }
//     else
//     {
//         int l1 = 0, l2 = 1, r1 = a.size() - 2, r2 = a.size() - 1;
//         x[0] = x[1] = a[l1].x;
//         x[2] = x[3] = a[r1].x;
//         y[0] = y[2] = a[l2].x;
//         y[1] = y[3] = a[r2].x;
//     }
//     for (int i = 0; i < 4; i++)
//         cout << x[i] << ' ' << y[i] << ' ';
//     cout << endl;
// }

// void _()
// {
//     int n;
//     cin >> n;
//     map<int, int> cnt;
//     int cnt_2 = 0, cnt_4 = 1e10;
//     for (int i = 0, x; i < n; i++)
//     {
//         cin >> x;
//         cnt[x]++;
//         if (cnt[x] >= 4)
//             cnt_4 = x;
//     }
//     for (auto [c, cnt] : cnt)
//         if (cnt > 1)
//             cnt_2 += cnt;
//     if (cnt_2 < 8)
//     {
//         cout << "NO" << endl;
//         return;
//     }
//     cout << "YES" << endl;
//     vector<int> x(4), y(4);
//     if (cnt_4 - (int)1e10)
//     {
//         // bug(11);
//         x = {cnt_4, cnt_4, cnt_4, cnt_4};
//         int id = -1;
//         cnt[cnt_4] -= 4;
//         for (auto &[x, cnt] : cnt)
//             while (id < 4 && cnt)
//                 y[++id] = x, cnt--;
//     }
//     else
//     {
//         struct Node
//         {
//             int x, cnt;
//         };
//         vector<Node> a;
//         for (auto [x, cnt] : cnt)
//             a.push_back({x, cnt});
//         int l = 0, r = 0;
//         int minw = 1e18;
//         for (int i = 0; i < a.size() - 1; i++)
//         {
//             int w = a[i + 1].x - a[i].x;
//             if (w < minw)
//             {
//                 w = minw;
//                 l = i, r = i + 1;
//             }
//         }
//         x[0] = x[1] = a[l].x;
//         x[2] = x[3] = a[r].x;
//         int l2 = 0, r2 = 0;
//         minw = 1e18;
//         for (int i = 1; i < a.size() - 1; i++)
//         {
//             int w = a[i + 1].x - a[i].x;
//             if (w < minw && i + 1 - l && i - l && i - r)
//             {
//                 w = minw;
//                 l2 = i, r2 = i + 1;
//             }
//         }
//         y[0] = y[1] = a[l2].x;
//         y[2] = y[3] = a[r2].x;
//     }
//     for (int i = 0; i < 4; i++)
//         cout << x[i] << ' ' << y[i] << ' ';
//     cout << 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);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

//  23mins
// 树状数组  快速求前缀和
// 维护差分数组(区间加)  位置(统计个数) ...
struct Tree
{
    int n;
    vector<int> tr;
    Tree(int n1)
    {
        n = n1 + 2;
        tr.assign(n + 2, 0);
    }
    void add(int x, int c) // 加点
    {
        for (int i = x; i <= n; i += i & -i)
            tr[i] += c;
    }
    int ask(int x) // 前缀查询
    {
        int res = 0;
        for (int i = x; i; i -= i & -i)
            res += tr[i];
        return res;
    }
    int ask(int l, int r) // 区间查询
    {
        if (l > r)
            return 0ll;
        return ask(r) - ask(l - 1);
    }
}; //    Tree tr(n);  tr.add(x,c)

void _()
{
    int n;
    cin >> n;
    Tree tr(n);
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i], tr.add(i, a[i]);
    int res = 0;
    auto get = [&](int l, int r, int mid)
    {
        return a[mid] + tr.ask(l, mid - 1) * 2 + tr.ask(mid + 1, r) * 2;
    };
    for (int i = 1; i <= n; i++)
        if (a[i])
        {
            int j = i + 1;
            for (; j <= n && a[j]; j++)
                ;
            int l = i, r = j - 1;
            // bug2(l, r);
            int tres = tr.ask(l, r) << 1;
            if ((r - l + 1) & 1)
            {
                if (r - l + 1 == 1)
                    tres = a[l];
                else
                {
                    for (int k = l; k <= r; k += 2)
                        tres = min(tres, get(l, r, k));
                }
            }
            i = j - 1;
            res += tres;
        }
    cout << res << endl;
}

C

#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);
    int T = 1;
    // cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, m;
    cin >> n >> m;
    vector<int> pre(n + 1);
    pre[0] = 1e18;
    for (int i = 1; i <= n; i++)
        cin >> pre[i], pre[i] = min(pre[i], pre[i - 1]); //, bug2(i, pre[i]);
    while (m--)
    {
        int x;
        cin >> x;
        int l = 0, r = n + 1;
        while (r - l - 1)
        {
            int mid = l + r >> 1;
            if (x >= pre[mid])
                r = mid;
            else
                l = mid;
        }
        if (r > n)
            r = -1;
        cout << r << endl;
    }
}

// void _()
// {
//     int n, m;
//     cin >> n >> m;
//     struct Node
//     {
//         int x, f;
//     };
//     vector<Node> a;
//     a.push_back({0, 0});
//     for (int i = 0; i < n; i++)
//     {
//         int x;
//         cin >> x;
//         if (!i)
//             a.push_back({x, i + 1});
//         else if (x < a[i - 1].x)
//             a.push_back({x, i + 1});
//     }
//     n = a.size();
//     while (m--)
//     {
//         int x;
//         cin >> x;
//         int l = 0, r = n + 1;
//         while (r - l - 1)
//         {
//             int mid = l + r >> 1;
//             if (x >= a[mid].x)
//                 r = mid;
//             else
//                 l = mid;
//         }
//         int tans = -1;
//         if (r < n)
//             tans = a[r].f;
//         cout << tans << endl;
//     }
// }

D

#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);
    int T = 1;
    // cin >> T;
    while (T--)
        _();
    return 0;
}

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