2024.12.3 周二

2024.12.3 周二


Q1. 1100

给定两个长度为n和n+1的数组a,b。每次操作:选择a的任意一个数 +1/-1/复制到末尾。

问将a变成b的最小操作次数。

Q2. 1200

设定一个数组是美丽的:当其可以通过任意次操作将数组里的数变成同一个数字,操作:如果a[i-1]==a[i+1],则可使a[i]=a[i-1]。

问删除数组里最少的一些数使数组变得不美丽。

Q3. 1200

给定n,k。n头牛,每头牛有自己的力量值。设比赛过程:a[1]与a[2]pk,胜者与a[3]pk,胜者依次向后pk。

对于第k头牛,你可以与其他牛交换位置/不动,问其可以取胜场数的最大值。

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

  • 看似简单的3个题却用了2个小时各wa2一发。本以为应该10分钟一道的,看来确实思维基础不够好,本质抓的不够快,假思路太多。

A1.

两点:1.遍历每个数找出其作为b[i]和b[n+1]贡献的最小值,其他值的次数是一定的。

2.对每个a[i]讨论2种情况,找最优解。

A2.

两点:1.发现美丽的数组总是1121211311这样,即不同于a[1]的数的连续个数只能为1.

2.考虑在数组中值为a[1]的一些连续的块(双指针处理),发现只需要删除最小的块即可。

A3.

两点:1.如果不换位置,只是为a[k]安排位置的话,答案和前缀最大值有关。

2.考虑两个方向,大于a[k]和小于a[k]的。发现都是与最左端的交换更优。

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

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;
    cin >> n;
    vector<int> a(n + 1), b(n + 2);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n + 1; i++)
        cin >> b[i];

    auto to = [](int a, int b)
    {
        return abs(a - b);
    };
    int res = 1e18, s = 0;
    for (int i = 1; i <= n; i++)
        s += to(a[i], b[i]);
    for (int i = 1; i <= n; i++)
    {
        int tres = s - to(a[i], b[i]);
        int minw = b[i], maxw = b[n + 1];
        if (minw > maxw)
            swap(maxw, minw);
        if (minw >= a[i])
            tres += to(a[i], maxw);
        else if (maxw <= a[i])
            tres += to(a[i], minw);
        else
            tres += to(a[i], b[i]) + to(a[i], b[n + 1]);
        res = min(res, tres);
    }
    cout << res + 1 << 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<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    vector<int> cnt;
    for (int i = 1; i <= n; i++)
        if (a[i] == a[1])
        {
            int x = 1;
            int j = i + 1;
            for (; j <= n && a[j] == a[1]; j++)
                x++;
            i = j - 1;
            // bug(x);
            cnt.push_back(x);
        }
    int res = n;
    for (auto v : cnt)
        res = min(res, v);

    for (int i = 1; i < n; i++)
        if (a[i] - a[1] && a[i + 1] - a[1])
            res = -1;
    if (a[1] - a[n] || res == n)
        res = -1;
    cout << res << 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, k;
    cin >> n >> k;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int l_s = k, l_b = k;
    for (int i = n; i; i--)
        if (a[i] < a[k])
            l_s = i;
    for (int i = 1; i <= n; i++)
        if (a[i] > a[k])
        {
            l_b = i;
            break;
        }
    auto grt = [&](int x)
    {
        int res = 0;
        auto t = a;
        int tk = t[k];
        swap(t[x], t[k]);
        int maxw = t[1];
        // for (int i = 1; i <= n; i++)
        //     cout << t[i] << " ";
        // cout << endl;
        for (int i = 1; i <= n; i++)
        {
            maxw = max(maxw, t[i]);
            if (maxw > tk)
                break;
            else if (i - x && i >= x - 1)
                res++;
        }
        // bug(res);
        return res;
    };
    int res = max({grt(k), grt(l_s), grt(l_b)});
    cout << res << endl;
}
posted @   Jkke  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示