zoukankan      html  css  js  c++  java
  • Codeforces Round #402 (Div. 2) --- C. Dishonest Sellers

    C. Dishonest Sellers
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.

    Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.

    Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.

    Input

    In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — total number of items to buy and minimal number of items Igor wants to by right now.

    The second line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).

    The third line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).

    Output

    Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.

    Examples
    input
    3 1
    5 4 6
    3 1 5
    output
    10
    input
    5 3
    3 4 7 10 3
    4 5 5 12 5
    output
    25
    Note

    In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.

    In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25

    题意:你需要买n件商品,给出降价前和降价后(降价后的价格可能比降价前还要高...)的商品价格,给你一个数k,你在降价前至少买k件,问怎么买花费最少

    感觉就是个贪心的思想,分几种情况,降价后价格比降价前还要高的话,那肯定买降价前的,买完这种之后判断你当前买的数量是否超过k,超过了就直接买降价后的

    ,没超过的话在降价前的一批商品中挑便宜的优先;如果不存在降价后价格比降价前还要高的情况,就排序降价前和降价后的差值,按差值从小到大买降价前,买满k个

    之后加上降价后的...

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ss;
    const int maxn = 2e5 + 5;
    int bef[maxn], af[maxn];
    struct node{
        int bef, af, chazhi;
    };
    bool cmp(node a, node b){
        if(a.chazhi == b.chazhi)    return a.bef < b.bef;    
        else return a.chazhi < b.chazhi;
    }
    node chazhi[maxn];
    int n, k;
    
    int main(){
        ss sum_pri;
        int cnt, l, cz;
        while(scanf("%d%d", &n, &k)!=EOF && n){
            sum_pri = cnt = 0;
            for(int i=0; i<n; i++){
                scanf("%d", &bef[i]);
            }
            for(int i=0; i<n; i++){
                scanf("%d", &af[i]);
            }
            l=0, cz;
            for(int i=0; i<n; i++){
                cz = bef[i] - af[i];
                if(cz < 0){
                    sum_pri += bef[i];
                    cnt++;
                }else{
                    chazhi[l].bef = bef[i];
                    chazhi[l].af = af[i];
                    chazhi[l].chazhi = cz;
                    l++;
                }
            }
            if(cnt>=k){
                for(int i=0; i<l; i++){
                    sum_pri += chazhi[i].af;
                }
                cout << sum_pri << endl;
            }
            else{
                int i;
                sort(chazhi, chazhi+l, cmp);
                for(i=0; i<l; i++){
                    sum_pri += chazhi[i].bef;
    //                cout << chazhi[i].bef << " ";
                    cnt++;
                    if(cnt>=k) break;
                }
                i++;
                for(i; i<l; i++){
                    sum_pri += chazhi[i].af;
        //            cout << chazhi[i].af << " ";
                }
                cout << sum_pri << endl;
            }
        }
        
        
        return 0;
    }
  • 相关阅读:
    jQuery Easing 动画效果扩展--使用Easing插件,让你的动画更具美感。
    JavaScript表达式--掌握最全的表达式,一切尽在掌握中,让表达不再是难事
    JavaScript的格式--从格式做起,做最严谨的工程师
    JavaScript 简介--对javascript的初识,最基础的了解
    手机web页面制作时的注意事项
    实现像淘宝一样牛的语音搜索框
    Cufon在渲染网页字体你不知道的事
    .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?
    使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
    eval解析JSON中的注意点
  • 原文地址:https://www.cnblogs.com/ledoc/p/6445572.html
Copyright © 2011-2022 走看看