Codeforces 767E 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

题目大意:

一共有两种货币:100块的和1块的.一开始有m个1块的,和无限个100块的.

一共有N天,其中每天的饭钱是ci.每天收银员的怒气值是wi.

对应一天收银员不高兴的值会增长:找零钱的数量(100块的和1块的找零总数)*wi.

收银员很聪明,他肯定给你找零是最小的数量。

就是说假如今天饭钱是165.假如你给他300,他一定找你一张100的和35个一块的。

这N天是按照时间顺序给出的,不能打乱,问最终收银员这N天积累的不高兴值最高是多少。

对应每天给收银员的货币情况是什么。


思路:


1、

①首先,如果m>=a[i].ci%100.那么对应这天直接用1块a[i].ci%100个.以及100块a[i].ci/100个。

②否则,如果遇到了m<a[i].ci%100的情况,那么只有两种弥补方式,要么这一天给a[i].ci/100+1个100块的货币,要么在之前某些天来凑够剩余的硬币数量tmp(就是说在之前某些天用整数钱换一堆零钱到这一天用).使得tmp+m>=a[i].ci%100.


2、那么这个“有些天”如何控制呢?想了很久,后来才发现一个关键点:对于之前的某天,如果他选择的方式①支付,那么他那天就支付了a[那天].ci%100个1块的硬币,那么如果我们那天改变成②方式支付,那么相当于到这一天,多了100硬币出来,我们肯定一点,100+m是一定>=a[i].ci%100的,所以只要找到之前某一天,选择的方式①支付,我们改成方式②去支付,就能够使得今天的1块钱够用。

对于出现了m<a[i].ci%100的情况,其一定选择【0~i】中的某天,从方式①支付变成了方式②支付.使得1块钱的硬币足够用。

这里贪心的去想,肯定想要选择的那天收银员增长的不高兴值尽可能的小,那么这里我们维护一个优先队列即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define ll __int64
struct node
{
    ll ci,wi,cmp,pos;
    friend bool operator <(node a,node b)
    {
        return a.cmp>b.cmp;
    }
}a[108000],now;
ll use[108000];
ll ans[108000][2];
int main()
{
    ll n,m;
    while(~scanf("%I64d%I64d",&n,&m))
    {
        ll output=0;
        memset(use,0,sizeof(use));
        priority_queue<node >s;
        for(ll i=0;i<n;i++)scanf("%I64d",&a[i].ci);
        for(ll i=0;i<n;i++)scanf("%I64d",&a[i].wi);
        for(ll i=0;i<n;i++)
        {
            a[i].cmp=(100-(a[i].ci%100))*a[i].wi;
            a[i].pos=i;
            ans[i][0]=a[i].ci/100;
            ans[i][1]=a[i].ci%100;
            if(ans[i][1]!=0)s.push(a[i]);
            else use[i]=1;
            if(m>=a[i].ci%100)
            {
                m-=a[i].ci%100;
            }
            else
            {
                now=s.top();
                output+=now.cmp;
                m-=ans[i][1];
                m+=100;
                s.pop();
            }
        }
        while(!s.empty())
        {
            use[s.top().pos]=1;
            s.pop();
        }
        printf("%I64d\n",output);
        for(ll i=0;i<n;i++)
        {
            if(use[i]==0)ans[i][0]++,ans[i][1]=0;
            printf("%I64d %I64d\n",ans[i][0],ans[i][1]);
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值