zoukankan      html  css  js  c++  java
  • Codeforces Round #398 E题Change-free(贪心)解题报告

    E. Change-free
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Student Arseny likes to plan his life for n days ahead. He visits a canteen every day and he has already decided what he will order in each of the following n days. Prices in the canteen do not change and that means Arseny will spend ci rubles during the i-th day.

    There are 1-ruble coins and 100-ruble notes in circulation. At this moment, Arseny has m coins and a sufficiently large amount of notes (you can assume that he has an infinite amount of them). Arseny loves modern technologies, so he uses his credit card everywhere except the canteen, but he has to pay in cash in the canteen because it does not accept cards.

    Cashier always asks the student to pay change-free. However, it's not always possible, but Arseny tries to minimize the dissatisfaction of the cashier. Cashier's dissatisfaction for each of the days is determined by the total amount of notes and coins in the change. To be precise, if the cashier gives Arseny x notes and coins on the i-th day, his dissatisfaction for this day equals x·wi. Cashier always gives change using as little coins and notes as possible, he always has enough of them to be able to do this.

    "Caution! Angry cashier"

    Arseny wants to pay in such a way that the total dissatisfaction of the cashier for n days would be as small as possible. Help him to find out how he needs to pay in each of the n days!

    Note that Arseny always has enough money to pay, because he has an infinite amount of notes. Arseny can use notes and coins he received in change during any of the following days.

    Input

    The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 109) — the amount of days Arseny planned his actions for and the amount of coins he currently has.

    The second line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105) — the amounts of money in rubles which Arseny is going to spend for each of the following days.

    The third line contains a sequence of integers w1, w2, ..., wn (1 ≤ wi ≤ 105) — the cashier's dissatisfaction coefficients for each of the following days.

    Output

    In the first line print one integer — minimum possible total dissatisfaction of the cashier.

    Then print n lines, the i-th of then should contain two numbers — the amount of notes and the amount of coins which Arseny should use to pay in the canteen on the i-th day.

    Of course, the total amount of money Arseny gives to the casher in any of the days should be no less than the amount of money he has planned to spend. It also shouldn't exceed 106 rubles: Arseny never carries large sums of money with him.

    If there are multiple answers, print any of them.

    Examples
    input
    5 42
    117 71 150 243 200
    1 1 1 1 1
    output
    79
    1 17
    1 0
    2 0
    2 43
    2 0
    input
    3 0
    100 50 50
    1 3 2
    output
    150
    1 0
    1 0
    0 50
    input
    5 42
    117 71 150 243 200
    5 4 3 2 1
    output
    230
    1 17
    1 0
    1 50
    3 0
    2 0

    对于任意一天,设x为模100后的部分。如果全交硬币,那么硬币数-x,如果多交1张纸币,硬币数-x+100,总值加上 w[i]*(100-x)。于是先每一天都按交硬币减去需要硬币数,不够的情况
    就是减完为负。由于是按时间顺序一天天来的,那么就说明在这一天即其之前的天里,必须有一天是破百元的钱的。选择方法是找其中对总值加的最少的。用set写就是.begin(),
    也可以用优先队列重载运算符做就是top的元素。并且又得到100枚硬币后一定可以够交钱,采用这样的贪心策略进行即可。
     1 #include <iostream>
     2 #include<bits/stdc++.h>
     3 #include <stack>
     4 #include <queue>
     5 #include <cstdio>
     6 #include <cstring>
     7 #include <algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef unsigned long long ull;
    11 const int MAX=1e5+5;
    12 int n,m;
    13 int c[MAX],w[MAX];
    14 set < pair<int,int> > s;
    15 ll an=0;
    16 bool st[MAX];
    17 int main()
    18 {
    19     scanf("%d %d",&n,&m);
    20     memset(st,false,sizeof(st));
    21     int i;
    22     for(i=1;i<=n;i++)
    23         scanf("%d",&c[i]);
    24     for(i=1;i<=n;i++)
    25         scanf("%d",&w[i]);
    26     for(i=1;i<=n;i++)
    27     {
    28         if(c[i]%100==0)
    29             continue;
    30         m-=c[i]%100;
    31         s.insert(make_pair(w[i]*(100-c[i]%100),i));
    32         if(m<0)
    33         {
    34             m+=100;
    35             an+=s.begin()->first;
    36             st[s.begin()->second]=true;
    37             s.erase(s.begin());
    38         }
    39     }
    40     cout<<an<<"
    ";
    41     for(i=1;i<=n;i++)
    42     {
    43         if(st[i])
    44             printf("%d 0
    ",c[i]/100+1);
    45         else
    46             printf("%d %d
    ",c[i]/100,c[i]%100);
    47     }
    48 }
     
  • 相关阅读:
    saltstack学习
    linux命令学习(2):wc 命令
    linux命令学习(1):grep 命令
    Docker Compose—简化复杂容器应用的利器
    Docker-compose命令详解
    Python json数据写入csv json excel文件
    Centos 不重启 修改ulimit参数
    pip包管理工具 基本使用
    python 使用xpath解析含有命名空间(xmlns)的xml
    pyqt5 + pyinstaller 制作爬虫小程序
  • 原文地址:https://www.cnblogs.com/quintessence/p/6415995.html
Copyright © 2011-2022 走看看