当前位置: 首页 > news >正文

2025.2.10——1400

2025.2.10——1400


A 1400

B 1400

C 1400

D 1400

------------------------------------------------

  • 思维+前缀+数学+贪心/结论


A

  1. 入手点:发现 k > 2 k>2 k>2 时答案为0。
  2. 关键点:考虑 k = = 1 / k = = 2 k==1 / k==2 k==1/k==2 时即可。
  3. 巧妙点: l o w _ b o u n d low\_bound low_bound 去寻找第一个大于与小于指定值的数。

B

  1. 入手点:更换一种问题的计算方式,考虑一层层的后缀和。
  2. 关键点:正数求和。
  3. 对前缀和的理解。

C

  1. 入手:只需考虑最大值与最小值。
  2. 关键:每次操作会使差值/2。贪心奇偶数讨论一下即可。

D

  1. 关键:贪心单调匹配。
  2. 巧妙:使用结构体绑定下标排序,用于匹配。

------------------------代码------------------------

A

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
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
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        el;                     \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    int res = 1e18;
    for (int i = 0; i < n; i++)
        cin >> a[i], res = min(res, a[i]);

    sort(a.begin(), a.end());
    for (int i = 1; i < n; i++)
        res = min(res, a[i] - a[i - 1]);
    if (k == 2)
    {
        vector<int> inv = a;
        reverse(inv.begin(), inv.end());
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
            {
                int v = abs(a[i] - a[j]);
                auto it_r = lower_bound(begin(a), end(a), v);
                auto it_l = lower_bound(begin(inv), end(inv), v, greater<int>());
                if (it_r != end(a))
                    res = min(res, abs(*it_r - v));
                if (it_l != end(inv))
                    res = min(res, abs(*it_l - v));
            }
    }
    if (k > 2)
        res = 0;
    cout << res;
    el;
}

B

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
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
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        el;                     \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    vector<int> a(n + 1), suf(n + 2);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = n; i; i--)
        suf[i] = suf[i + 1] + a[i];
    int res = suf[1];
    for (int i = 2; i <= n; i++)
        res += suf[i] > 0 ? suf[i] : 0;
    cout << res;
    el;
}

C

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
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
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        el;                     \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    int l = 1e12, r = -1;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        l = min(l, x);
        r = max(r, x);
    }
    vector<int> res;
    while (l - r)
    {
        int v = l & 1 ? 1 : 0;
        res.push_back(v);
        l += v, r += v;
        l >>= 1, r >>= 1;
    }
    cout << res.size();
    el;
    if (res.size() <= n)
        for (auto v : res)
            cout << v << ' ';
    el;
}

D

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
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
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        el;                     \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, k;
    cin >> n >> k;
    struct Node
    {
        /* data */
        int v, id;
    };
    vector<Node> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i].v, a[i].id = i;
    for (auto &[v, id] : b)
        cin >> v;
    sort(begin(a), end(a), [](Node &e1, Node &e2)
         { return e1.v < e2.v; });
    sort(begin(b), end(b), [](Node &e1, Node &e2)
         { return e1.v < e2.v; });

    int stb = -1;
    for (int i = n - k; i < n; i++)
        b[++stb].id = a[i].id;
    for (int i = 0; i < n - k; i++)
        b[++stb].id = a[i].id;

    sort(begin(a), end(a), [](Node &e1, Node &e2)
         { return e1.id < e2.id; });
    sort(begin(b), end(b), [](Node &e1, Node &e2)
         { return e1.id < e2.id; });
    int cnt = 0;
    for (int i = 0; i < n; i++)
        cnt += a[i].v > b[i].v;

    if (cnt - k)
    {
        cout << "NO";
        el;
        return;
    }
    cout << "YES";
    el;
    for (auto [v, id] : b)
        cout << v << ' ';
    el;
}

相关文章:

  • stm32电机驱动模块
  • python 基础知识100问
  • 第一章嵌入式系统概论考点02嵌入式系统的组成
  • ASP.NET Core 如何使用 C# 向端点发出 POST 请求
  • HTTP相关面试题
  • 利用MATLAB的linkaxes函数实现子图频率轴同步缩放
  • DateConverter does not support default String to ‘Date‘ conversion.
  • HARCT 2025 分论坛9:专用设备和机器人系统
  • WebGL 导入 OBJ 文件全解析
  • jenkins-获取当前时间戳
  • 买卖股票的最佳时机II(力扣122)
  • Python使用Flask结合DeepSeek开发
  • 2D 游戏艺术、动画和光照
  • Vue 2 — 配置请求转发
  • 【第5章:深度生成模型— 5.4 深度生成模型前沿全景:从Diffusion到多模态,揭秘AI生成技术的未来】
  • $ npx electron-forge import 一直报权限问题 resource busy or locked,
  • EasyRTC嵌入式WebRTC视频通话SDK支持Web浏览器、Linux、ARM、Android、iOS
  • win10右键使用IDEA打开
  • 日志2025.2.14
  • 深入浅出 Python Logging:从基础到进阶日志管理
  • vps利用端口做不同网站/电脑培训班有哪些科目
  • 网站建设公司广告语/品牌设计公司排名前十强
  • 郴州网站建设哪家公司好/2022年国际十大新闻
  • 网站空间 价格/成人短期培训能学什么
  • 网站建设出现乱码是怎么回事/百度通用网址
  • 做好网站建设工作/百度seo优化包含哪几项