首页 > 其他分享 >【每日一题】Problem 1832B - Maximum Sum

【每日一题】Problem 1832B - Maximum Sum

时间:2023-06-07 23:35:29浏览次数:60  
标签:std int Sum 1832B cin long vec Problem

原题

解决思路:

类似滑动窗口的方式,窗口大小为 k 次操作,从中找到更大的结果即可

误区:

一开始的想法是每次都在前一次的基础上减去最少的,然而在样例的第五个测试点出错,因为 10+11 > 15

#include <bits/stdc++.h>

int main() {
    int t;
    std::cin >> t;
    while (t--) {
        int n, k;
        std::cin >> n >> k;
        std::vector<int> vec(n, 0);
        for (int i = 0; i < n; ++i) std::cin >> vec[i];
        std::sort(vec.begin(), vec.end());

        long long sum = 0;
        long long cutSum = 0;
        long long ans = 0;
        for (int i = 0; i < n; ++i) {
            sum += vec[i];
            if (i >= n - k) cutSum += vec[i];
        }

        for (int i = 0; i <= k; ++i) {
            ans = std::max(ans, sum - cutSum);
            cutSum = cutSum - vec[n- k + i] + vec[i * 2] + vec[i * 2 + 1];
        }

        std::cout << ans << std::endl;
    }
    return 0;
}

更好的解

采用"前缀和"的方法省略了很多代码

#include <bits/stdc++.h>

int main() {
    int t;
    std::cin >> t;
    while (t--) {
        int n, k;
        std::cin >> n >> k;
        std::vector<long long> vec(n+1, 0); // 需要 0 位置来充当"哨兵"
        for (int i = 1; i <= n; ++i) std::cin >> vec[i];
        std::sort(vec.begin(), vec.end());

        long long ans = 0;
        for (int i = 2; i <= n; ++i) vec[i] += vec[i-1];
        for (int i = 0; i <= k; ++i) {
            ans = std::max(ans, vec[n - k + i] - vec[i * 2]); // 使用"哨兵"更加方便
        }

        std::cout << ans << std::endl;
    }
    return 0;
}

标签:std,int,Sum,1832B,cin,long,vec,Problem
From: https://www.cnblogs.com/HelloEricy/p/17464880.html

相关文章

  • codeforces.com/contest/1553/problem/B
    简单字符串哈希题意给一个字符串s和t,问从s的某个位置开始,向右到某个点后再向左,顺序遍历到的字符形成的字符串可否为t。思路数据只有500,\(O(n^3)\)可过,枚举转折点,然后枚举开头和结尾。代码intn,m,k;ullHash[1010],rHash[1010],p[1010],rp[1010],sum;voidsolve(){ ......
  • Contrastive Learning for Representation Degeneration Problem in Sequential Recom
    目录概符号说明MotivationDuoRecContrastiveRegularization代码QiuR.,HuangZ.,YingH.andWangZ.Contrastivelearningforrepresentationdegenerationprobleminsequentialrecommendation.WSDM,2022.概对比学习之于序列推荐.符号说明\(\mathcal{V}\),ite......
  • 解决cURL error 60: SSL certificate problem: unable to get local issuer certifica
    转载:报错原因:因为没有配置信任的服务器HTTPS验证。默认情况下,cURL被设为不信任任何CAs,因此浏览器无法通过HTTPs访问你服务器。一、解决方式下载证书1、放到这里来2、修改php.ini文件,去掉前面“;”路径带上""3、openssl这个扩展开启4、记得重启,不然不生效......
  • Git 的SSL certificate problem: unable to get local issuer certificate问题
    D:\temp>gitclonehttps://github.com/xxxxxx/yyyyyy.gitCloninginto'yyyyyy'...fatal:unabletoaccess'https://github.com/xxxxxx/yyyyyy.git/':SSLcertificateproblem:unabletogetlocalissuercertificate处理方法:D:\temp>gitco......
  • Sum of MSLCM 题解
    SumofMSLCM题目大意定义\(\text{MSLCM}(n)\)为所有满足该数集的\(\text{lcm}\)为\(n\)的数集中元素个数最多的数集的所有数字的和,现有多次询问,求\[\sum_{i=2}^n\text{MSLCM}(i)\]思路分析大水题。虽然看着这个东西很可怕,但仔细一想你就会发现,其实\(\text{MSLCM}(n)......
  • w task2 - problem and solution
    Readandunderstandthequestion-highlight/underlinekeypartscauses...solutions youropinionIbelieve...   Introduction: varietyofreasons, stpescanbetakentotackleItisturethat...hasbeengettingworseinrecentyearsTherea......
  • 解决 NVIDIA Windows has stopped this device because it has reported problems. (C
    场景当跑需要使用GPU算力的一些项目时候,需要用到CUDA,确保电脑是具有独立显卡的机子,但是怎么也没法让代码中的torch跑在GPU上;点击任务管理器查看"性能"下的GPU选项,看到运行中的并非是独立显卡而是集成显卡;点击设备管理器,发现NVIDIA显卡左下角有感叹号,双击发现里面显示"Wind......
  • P1001 A+B Problem
    A+BProblem题目背景强烈推荐新用户必读帖。不熟悉算法竞赛的选手请看这里:算法竞赛中要求的输出格式中,不能有多余的内容,这也包括了“请输入整数$\bma$和$\bmb$”这一类的提示用户输入信息的内容。若包含了这些内容,将会被认为是WrongAnswer,即洛谷上的WA。在对比代码输......
  • android: workaround for slow ‘building workspace’ problem in eclipse
    Whiledevelopingforandroidoneclipse3.6ihadtheproblemthateachtimeisavedafile,eclipseblockedmeseveralsecondswith‘buildingworkspace…’.Similartothese:stackoverflow–android-compilation-is-slow-using-eclipsestackoverflow–android-......
  • centos7卡在sda assuming drive cache write through不能进入操作系统的一个解决方案
    1、https://blog.csdn.net/shishui07/article/details/113934961?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-5-113934961-blog-101298947.235^v36^pc_relevant_default_base3&spm=1001.2101.3001.4242.4&utm_rel......