zoukankan      html  css  js  c++  java
  • H Kuangyeye and hamburgers

    链接:https://ac.nowcoder.com/acm/contest/338/H
    来源:牛客网

    题目描述

    Kuangyeye is a dalao of the ACM school team of Hunan University. His favorite food are hamburgers. One day, Kuangyeye came to the KFC(or maybe McDonald) and saw n hamburgers on the counter.The weight of the i-th hamburger was wi. Since he likes hamburgers very much, he would like to buy some hamburgers. Considering his weight or other factors, Kuangyeye only wanted to eat all the hamburgers from the a-th heaviest to the b-th. Since Kuangyeye is fickle, he had k plans before buying hamburgers. The i-th plan gives ai and bi. Please help Kuangyeye calculate the maximum weight of hamburgers he can eat among the k plans.

    输入描述:

    the first line of input contains two integer n and k--the number of hamburgers on the counter and the number of plans Kuangyeye had;
    the next line contains n integer--the i-th integer represents the weight of i-th hamburger,namely w
    i
    ;
    Each the following k line contains two integer a
    i
     and b
    i
     ,represents Kuangyeye's strategy in his i-th plan.

    输出描述:

    Output contain a single integer,represents maximum weight of hamburgers Kuangyeye can eat.
    示例1

    输入

    复制
    5 2
    4 3 5 2 6
    1 1
    3 4

    输出

    复制
    7

    说明

    Kuangyeye's first plan was to eat the hamburger weighing 6;

    and his second plan was to eat the hamburger weighing 3 and 4;

    So the maximum weight of hamburgers he can eat was 7.

    备注:

    1≤n,k≤100000,1≤a
    i
    ≤b
    i
    ≤n,1≤w
    i
    ≤10000

    题解:排序,计算前缀和,模拟

    #include <iostream>
    #include <algorithm>
    using namespace std;
    int w[100050];
    long long dp[100050];
     
    bool cmp(const int& a,const int& b) {
        return a>b;
    }
     
    int main() {
        int n,k;
        long long ans=0;
        cin>>n>>k;
        for(int i=1;i<=n;i++) cin>>w[i];
        sort(w+1,w+n+1,cmp);
        dp[0]=0;
        for(int i=1;i<=n;i++) {
            dp[i]=dp[i-1]+w[i];
        }
        while(k--) {
            int l,r;
            cin>>l>>r;
            ans=max(dp[r]-dp[l-1],ans);
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    Python中循环引用(import)失败的解决方法
    junit中线程需要注意的问题
    python动态绑定属性和方法
    python输出缓冲区的问题
    使用RateLimiter完成简单的大流量限流,抢购秒杀限流
    guava的限流工具RateLimiter使用
    高性能分布式锁-redisson的使用
    正则表达式
    input 标签鼠标放入输入框补全提示
    Google guava工具类的介绍和使用
  • 原文地址:https://www.cnblogs.com/DWVictor/p/10229893.html
Copyright © 2011-2022 走看看