zoukankan      html  css  js  c++  java
  • CF779C(round 402 div.2 C) Dishonest Sellers

    题意:

    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·1050 ≤ 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.

    思路:

    贪心,排序。

    实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 struct node
     7 {
     8     int a, b;
     9 };
    10 node t[200005];
    11 int n, k;
    12 bool cmp(const node & x, const node & y)
    13 {
    14     if (x.a >= x.b && y.a >= y.b)
    15     {
    16         return x.a - x.b < y.a - y.b;
    17     }
    18     else if (x.a < x.b && y.a >= y.b)
    19     {
    20         return true;
    21     }
    22     else if (x.a >= x.b && y.a < y.b)
    23     {
    24         return false;
    25     }
    26     else
    27     {
    28         return x.b - x.a > y.b - y.a;
    29     }
    30     
    31 }
    32 int main()
    33 {
    34     cin >> n >> k;
    35     for (int i = 0; i < n; i++)
    36     {
    37         scanf("%d", &t[i].a);
    38     }
    39     for (int i = 0; i < n; i++)
    40     {
    41         scanf("%d", &t[i].b);
    42     }
    43     sort(t, t + n, cmp);
    44     int sum = 0;
    45     for (int i = 0; i < n; i++)
    46     {
    47         if (i < k)
    48         {
    49             sum += t[i].a;
    50         }
    51         else
    52         {
    53             sum += min(t[i].a, t[i].b);
    54         }
    55     }
    56     cout << sum << endl;
    57     return 0;
    58 }
  • 相关阅读:
    剑指offer63:数据流中的中位数
    剑指offer62:二叉搜索树的第k个结点,二叉搜索树【左边的元素小于根,右边的元素大于根】
    剑指offer61:序列化二叉树
    关于手机拍摄的图片在处理过程中出现问题的解决方法
    一道逻辑思考题
    鼠标右键无反应解决方法
    六大设计原则
    开源镜像网站
    获取当前文件夹下的文件名称
    wget使用方法
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6445518.html
Copyright © 2011-2022 走看看