E. Change-free贪心

本文探讨了在固定价格下,如何通过合理安排现金支付,包括纸币和硬币,以最小化因找零引起的不满情绪。通过一种贪心算法,我们能够找到在给定条件下,使得收银员总不满情绪最小的支付方案。

E. Change-free

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

贪心,把一个整付改成找钱,m变化量为+100恒定,维护minimun的dissatisfaction即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<vector>

using namespace std;

int n;
long long m;
long long c[100005];
long long w[100005];
int ans[100005];

struct node
{
    int id;
    long long val;
    bool friend operator<(node x,node y)
    {
        return x.val>y.val;
    }
};

int main()
{
    while(~scanf("%d%lld",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&c[i]);
            ans[i]=0;
        }
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&w[i]);
        }

        priority_queue<node> q;
        long long anss=0;

        for(int i=0;i<n;i++)
        {
            long long cost=c[i]%100;
            long long val=w[i]*(100-cost);
            node cur;
            cur.id=i;
            cur.val=val;
            if(cost!=0)
                q.push(cur);

            m-=cost;
            //cout<<m<<endl;
            if(m<0)
            {
                node fst=q.top();
                anss+=fst.val;
                m+=100;
                q.pop();
                ans[fst.id]=1;
            }
        }
        printf("%lld\n",anss);
        for(int i=0;i<n;i++)
        {
            if(ans[i]==0)
            {
                printf("%lld %lld\n",c[i]/100,c[i]%100);
            }
            else
            {
                printf("%lld %lld\n",c[i]/100+1,0);
            }
        }
    }
}

 

#ifndef BRRT_H #define BRRT_H #include "occ_grid/occ_map.h" #include "visualization/visualization.hpp" #include "sampler.h" #include "node.h" #include "kdtree.h" #include <ros/ros.h> #include <utility> #include <queue> #include <algorithm> namespace path_plan { class BRRT { public: BRRT(){}; BRRT(const ros::NodeHandle &nh, const env::OccMap::Ptr &mapPtr) : nh_(nh), map_ptr_(mapPtr) { nh_.param("BRRT/steer_length", steer_length_, 0.0); nh_.param("BRRT/search_time", search_time_, 0.0); nh_.param("BRRT/max_tree_node_nums", max_tree_node_nums_, 0); ROS_WARN_STREAM("[BRRT] param: steer_length: " << steer_length_); ROS_WARN_STREAM("[BRRT] param: search_time: " << search_time_); ROS_WARN_STREAM("[BRRT] param: max_tree_node_nums: " << max_tree_node_nums_); sampler_.setSamplingRange(mapPtr->getOrigin(), mapPtr->getMapSize()); valid_tree_node_nums_ = 0; nodes_pool_.resize(max_tree_node_nums_); for (int i = 0; i < max_tree_node_nums_; ++i) { nodes_pool_[i] = new TreeNode; } } ~BRRT(){}; bool plan(const Eigen::Vector3d &s, const Eigen::Vector3d &g) { reset(); if (!map_ptr_->isStateValid(s)) { ROS_ERROR("[BRRT]: Start pos collide or out of bound"); return false; } if (!map_ptr_->isStateValid(g)) { ROS_ERROR("[BRRT]: Goal pos collide or out of bound"); return false; } /* construct start and goal nodes */ start_node_ = nodes_pool_[1]; start_node_->x = s; start_node_->cost_from_start = 0.0; goal_node_ = nodes_pool_[0]; goal_node_->x = g; goal_node_->cost_from_start = 0.0; // important valid_tree_node_nums_ = 2; // put start and goal in tree vis_ptr_->visualize_a_ball(s, 0.3, "start", visualization::Color::pink); vis_ptr_->visualize_a_ball(g, 0.3, "goal", visualization::Color::steelblue); ROS_INFO("[BRRT]: BRRT starts planning a path"); return brrt(s, g); } vector<Eigen::Vector3d> getPath() { return final_path_; } vector<vector<Eigen::Vector3d>> getAllPaths() { return path_list_; } vector<std::pair<double, double>> getSolutions() { return solution_cost_time_pair_list_; } void setVisualizer(const std::shared_ptr<visualization::Visualization> &visPtr) { vis_ptr_ = visPtr; }; private: // nodehandle params ros::NodeHandle nh_; BiasSampler sampler_; double steer_length_; double search_time_; int max_tree_node_nums_; int valid_tree_node_nums_; double first_path_use_time_; double final_path_use_time_; double cost_best_; std::vector<TreeNode *> nodes_pool_; TreeNode *start_node_; TreeNode *goal_node_; vector<Eigen::Vector3d> final_path_; vector<vector<Eigen::Vector3d>> path_list_; vector<std::pair<double, double>> solution_cost_time_pair_list_; // environment env::OccMap::Ptr map_ptr_; std::shared_ptr<visualization::Visualization> vis_ptr_; void reset() { final_path_.clear(); path_list_.clear(); cost_best_ = DBL_MAX; solution_cost_time_pair_list_.clear(); for (int i = 0; i < valid_tree_node_nums_; i++) { nodes_pool_[i]->parent = nullptr; nodes_pool_[i]->children.clear(); } valid_tree_node_nums_ = 0; } double calDist(const Eigen::Vector3d &p1, const Eigen::Vector3d &p2) { return (p1 - p2).norm(); } RRTNode3DPtr addTreeNode(RRTNode3DPtr &parent, const Eigen::Vector3d &state, const double &cost_from_start, const double &cost_from_parent) { RRTNode3DPtr new_node_ptr = nodes_pool_[valid_tree_node_nums_]; valid_tree_node_nums_++; new_node_ptr->parent = parent; parent->children.push_back(new_node_ptr); new_node_ptr->x = state; new_node_ptr->cost_from_start = cost_from_start; new_node_ptr->cost_from_parent = cost_from_parent; return new_node_ptr; } void changeNodeParent(RRTNode3DPtr &node, RRTNode3DPtr &parent, const double &cost_from_parent) { if (node->parent) node->parent->children.remove(node); //DON'T FORGET THIS, remove it form its parent's children list node->parent = parent; node->cost_from_parent = cost_from_parent; node->cost_from_start = parent->cost_from_start + cost_from_parent; parent->children.push_back(node); // for all its descedants, change the cost_from_start and tau_from_start; RRTNode3DPtr descendant(node); std::queue<RRTNode3DPtr> Q; Q.push(descendant); while (!Q.empty()) { descendant = Q.front(); Q.pop(); for (const auto &leafptr : descendant->children) { leafptr->cost_from_start = leafptr->cost_from_parent + descendant->cost_from_start; Q.push(leafptr); } } } void fillPath(const RRTNode3DPtr &node_A, const RRTNode3DPtr &node_B, vector<Eigen::Vector3d> &path) { path.clear(); RRTNode3DPtr node_ptr = node_A; while (node_ptr->parent) { path.push_back(node_ptr->x); node_ptr = node_ptr->parent; } path.push_back(start_node_->x); std::reverse(std::begin(path), std::end(path)); node_ptr = node_B; while (node_ptr->parent) { path.push_back(node_ptr->x); node_ptr = node_ptr->parent; } path.push_back(goal_node_->x); } Eigen::Vector3d steer(const Eigen::Vector3d &nearest_node_p, const Eigen::Vector3d &rand_node_p, double len) { Eigen::Vector3d diff_vec = rand_node_p - nearest_node_p; double dist = diff_vec.norm(); if (diff_vec.norm() <= len) return rand_node_p; else return nearest_node_p + diff_vec * len / dist; } bool greedySteer(const Eigen::Vector3d &x_near, const Eigen::Vector3d &x_target, vector<Eigen::Vector3d> &x_connects, const double len) { double vec_length = (x_target - x_near).norm(); Eigen::Vector3d vec_unit = (x_target - x_near) / vec_length; x_connects.clear(); if(vec_length < len) return map_ptr_->isSegmentValid(x_near,x_target); Eigen::Vector3d x_new, x_pre = x_near; double steered_dist = 0; while(steered_dist + len < vec_length) { x_new = x_pre + len * vec_unit; if( (!map_ptr_->isStateValid(x_new)) || (!map_ptr_->isSegmentValid(x_new,x_pre)) ) return false; x_pre = x_new; x_connects.push_back(x_new); steered_dist += len; } return map_ptr_->isSegmentValid(x_target,x_pre); } bool brrt(const Eigen::Vector3d &s, const Eigen::Vector3d &g) { ros::Time rrt_start_time = ros::Time::now(); bool tree_connected = false; bool path_reverse = false; /* kd tree init */ kdtree *kdtree_1 = kd_create(3); kdtree *kdtree_2 = kd_create(3); //Add start and goal nodes to kd trees kd_insert3(kdtree_1, start_node_->x[0], start_node_->x[1], start_node_->x[2], start_node_); kd_insert3(kdtree_2, goal_node_->x[0], goal_node_->x[1], goal_node_->x[2], goal_node_); kdtree *treeA = kdtree_1; kdtree *treeB = kdtree_2; /* main loop */ int idx = 0; for (idx = 0; (ros::Time::now() - rrt_start_time).toSec() < search_time_ && valid_tree_node_nums_ < max_tree_node_nums_; ++idx) { /* random sampling */ Eigen::Vector3d x_rand; sampler_.samplingOnce(x_rand); // samplingOnce(x_rand); if (!map_ptr_->isStateValid(x_rand)) { continue; } /* request nearest node in treeA */ struct kdres *p_nearestA = kd_nearest3(treeA, x_rand[0], x_rand[1], x_rand[2]); if (p_nearestA == nullptr) { ROS_ERROR("nearest query error"); continue; } RRTNode3DPtr nearest_nodeA = (RRTNode3DPtr)kd_res_item_data(p_nearestA); kd_res_free(p_nearestA); /* Extend treeA */ Eigen::Vector3d x_new = steer(nearest_nodeA->x, x_rand, steer_length_); if ( (!map_ptr_->isStateValid(x_new)) || (!map_ptr_->isSegmentValid(nearest_nodeA->x, x_new)) ) { /* Steer Trapped */ std::swap(treeA, treeB); path_reverse = !path_reverse; continue; } /* Add x_new to treeA */ double dist_from_A = nearest_nodeA->cost_from_start + steer_length_; RRTNode3DPtr new_nodeA(nullptr); new_nodeA = addTreeNode(nearest_nodeA, x_new, dist_from_A, steer_length_); kd_insert3(treeA, x_new[0], x_new[1], x_new[2], new_nodeA); /* request x_new's nearest node in treeB */ struct kdres *p_nearestB = kd_nearest3(treeB, x_new[0], x_new[1], x_new[2]); if (p_nearestB == nullptr) { ROS_ERROR("nearest query error"); continue; } RRTNode3DPtr nearest_nodeB = (RRTNode3DPtr)kd_res_item_data(p_nearestB); kd_res_free(p_nearestB); /* Greedy steer & check connection */ vector<Eigen::Vector3d> x_connects; bool isConnected = greedySteer(nearest_nodeB->x, x_new, x_connects, steer_length_); /* Add the steered nodes to treeB */ RRTNode3DPtr new_nodeB = nearest_nodeB; if(!x_connects.empty()){ if( valid_tree_node_nums_ + (int)x_connects.size() >= max_tree_node_nums_ ){ valid_tree_node_nums_ = max_tree_node_nums_; //max_node_num reached break;} for(auto x_connect: x_connects){ new_nodeB = addTreeNode(new_nodeB, x_connect, new_nodeB->cost_from_start + steer_length_, steer_length_); kd_insert3(treeB, x_connect[0], x_connect[1], x_connect[2], new_nodeB); } } /* If connected, trace the connected path */ if(isConnected){ tree_connected = true; double path_cost = new_nodeA->cost_from_start + new_nodeB->cost_from_start + calDist(new_nodeB->x, new_nodeA->x); if(path_cost < cost_best_) { vector<Eigen::Vector3d> curr_best_path; if(path_reverse) fillPath(new_nodeB, new_nodeA, curr_best_path); else fillPath(new_nodeA, new_nodeB, curr_best_path); path_list_.emplace_back(curr_best_path); solution_cost_time_pair_list_.emplace_back(path_cost, (ros::Time::now() - rrt_start_time).toSec()); cost_best_ = path_cost; } } /* Swap treeA&B */ std::swap(treeA, treeB); path_reverse = !path_reverse; }//End of sampling iteration if (tree_connected) { final_path_use_time_ = (ros::Time::now() - rrt_start_time).toSec(); ROS_INFO_STREAM("[BRRT]: find_path_use_time: " << solution_cost_time_pair_list_.front().second << ", length: " << solution_cost_time_pair_list_.front().first); visualizeWholeTree(); final_path_ = path_list_.back(); } else if (valid_tree_node_nums_ == max_tree_node_nums_) { visualizeWholeTree(); ROS_ERROR_STREAM("[BRRT]: NOT CONNECTED TO GOAL after " << max_tree_node_nums_ << " nodes added to rrt-tree"); } else { ROS_ERROR_STREAM("[BRRT]: NOT CONNECTED TO GOAL after " << (ros::Time::now() - rrt_start_time).toSec() << " seconds"); } return tree_connected; } void visualizeWholeTree(){ // Sample and visualize the resultant tree vector<Eigen::Vector3d> vertice; vector<std::pair<Eigen::Vector3d, Eigen::Vector3d>> edges; vertice.clear(); edges.clear(); sampleWholeTree(start_node_, vertice, edges); sampleWholeTree(goal_node_, vertice, edges); std::vector<visualization::BALL> tree_nodes; tree_nodes.reserve(vertice.size()); visualization::BALL node_p; node_p.radius = 0.12; for (size_t i = 0; i < vertice.size(); ++i) { node_p.center = vertice[i]; tree_nodes.push_back(node_p); } vis_ptr_->visualize_balls(tree_nodes, "tree_vertice", visualization::Color::blue, 1.0); vis_ptr_->visualize_pairline(edges, "tree_edges", visualization::Color::red, 0.06); } void sampleWholeTree(const RRTNode3DPtr &root, vector<Eigen::Vector3d> &vertice, vector<std::pair<Eigen::Vector3d, Eigen::Vector3d>> &edges) { if (root == nullptr) return; // whatever dfs or bfs RRTNode3DPtr node = root; std::queue<RRTNode3DPtr> Q; Q.push(node); while (!Q.empty()) { node = Q.front(); Q.pop(); for (const auto &leafptr : node->children) { vertice.push_back(leafptr->x); edges.emplace_back(std::make_pair(node->x, leafptr->x)); Q.push(leafptr); } } } public: void samplingOnce(Eigen::Vector3d &sample) { static int i = 0; sample = preserved_samples_[i]; i++; i = i % preserved_samples_.size(); } void setPreserveSamples(const vector<Eigen::Vector3d> &samples) { preserved_samples_ = samples; } vector<Eigen::Vector3d> preserved_samples_; }; } // namespace path_plan #endif 注释上述代码
09-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值