首页 > 其他分享 >2024.12.3 周二

2024.12.3 周二

时间:2024-12-04 11:44:09浏览次数:6  
标签:2024.12 return int res maxw long 周二 define

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;
}

标签:2024.12,return,int,res,maxw,long,周二,define
From: https://www.cnblogs.com/jkkk/p/18585947

相关文章

  • 2024.12.3
    //计算每个人的平均成绩JavaPairRDD<String,Double>averages=scores.join(counts).mapValues(newFunction<Tuple2<Integer,Integer>,Double>(){@OverridepublicDoublecall(Tuple2<Integer,Integer>tuple){return(double)tu......
  • 2024.12.2 周一
    2024.12.2周一Q1.1100给定一个数字(32位以内),使用1,0,-1构造二进制数位,同时保证不能有相邻的非0数存在。Q2.1200给定2个相同数位的数(<=1e100),任意操作:交换2数中相同位的数字使两数之积最大。Q3.1300前缀后缀板题Q4.1400给定n,m(<=2e6)。a:1n,b:1m,问:满足a+b是b*g......
  • 2024.12.3(周二)
    #导入必要的库fromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_score,StratifiedKFoldfromsklearn.svmimportSVCfromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_score,classification......
  • 2024.12.2(周一)
    importnumpyasnpfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_scorefromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_score,confusion_matrix,make_scorerfromsklearn.treeimpo......
  • 2024.12.6(周五)
    #导入相关库importnumpyasnpfromsklearn.datasetsimportload_irisfromsklearn.model_selectionimporttrain_test_split,cross_val_scorefromsklearn.clusterimportKMeansfromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_scor......
  • 2024.12.5(周四)
    #导入必要的库importnumpyasnpfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_score,StratifiedKFoldfromsklearn.naive_bayesimportGaussianNBfromsklearn.metricsimportaccuracy_score,precision_score,reca......
  • 2024.12.9(周一)
    importnumpyasnpimportpandasaspdfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_score,StratifiedKFoldfromsklearn.ensembleimportRandomForestClassifierfromsklearn.metricsimportaccuracy_score,prec......
  • 2024.11.26(周二)
    旅游的出行方式有乘坐飞机旅行、乘火车旅行和自行车游,不同的旅游方式有不同的实现过程,客户可以根据自己的需要选择一种合适的旅行方式。实验要求:1. 画出对应的类图;2. 提交源代码;3. 注意编程规范。  1、类图  2、源代码#include<iostream>usingnamespaces......
  • 2024.11.26 周二日常
    2024.11.26周二日常Q1.1200给定一数组k(代表n个人的倍率),设在每个人上投资为xi,若其胜利则获k*xi,最终一人胜利。问是否可以保证无论谁胜利,收益大于总投资的方案。(n<=20,k<=50)Q2.1300给定一数组,问最小的k使所有长度为k的区间按位或相等。Q3.1500给定一数组,定......
  • 2024.10.1(周二)
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head><title>生产制令</title><style>/*整体页面布局和样式*/......