SRM 153 1A 2013.12.5

SRM 153 1A 2013.12.5

DIV 1

250

 

Problem Statement

    

Inventory control is an important part ofany business that maintains an inventory. On the one hand, businesses want tohave enough of a product in stock that they can ship orders immediately.However, they do not want to have so much of a product in stock that all oftheir capital is tied up in that product. To deal with this, businesses oftenmaintain a small stock, and replenish that stock as it is sold on a monthlybasis. In other words, every month the business orders or produces more of aproduct so that it has some of the product in stock for the next month.  In this problem, a business wants to place astanding monthly order so that a certain number of items are delivered to iteach month. Your task is to help the business determine how large a standingorder to place. You will be given a vector <int>, sales, representing thenumber of items that the business sold for each of a number of months, and areto determine how many they can expect to sell in an average month.Unfortunately, the business may have run out of items some months, so this isnot as simple as just taking the average of the number of items sold each month.  You will be given a vector <int>,daysAvailable, whose elements represent the number of days that the item wasavailable in each of the months (elements of daysAvailable correspond toelements of sales with the same index). You should assume that, if the item wasnot available for a whole month, the business would have continued to sell theitem at the same rate during the days of the month that it was not available asit did during the days the item was available, had sufficient stock beenpresent. So, for example, if the business sold 5 items in the first half of amonth, and then ran out, you can assume that they would have sold 10 items thatmonth, if they had been available.  Onmonths when the item was available for zero days, you can tell nothing aboutthe number of items that might have sold, so you should not include thesemonths in your calculation. Also, for simplicity, you may assume that allmonths have 30 days. Thus, if the item were in stock for exactly half of themonth this would be represented by a 15 in daysAvailable. Furthermore, if theexpected number of sales per month is not a whole number, you should round upsince it is probably better to have one too many items than it is to have onetoo few.

Definition

    

Class:

Inventory

Method:

monthlyOrder

Parameters:

vector <int>, vector <int>

Returns:

int

Method signature:

int monthlyOrder(vector <int> sales,vector <int> daysAvailable)

(be sure your method is public)

    

 

Notes

-

While it is possible to solve this problemwithout using any double arithmetic, the last constraint ensures that thesimpler solution using doubles will work if an epsilon is used properly (payspecial attention to the last example). In other words, once you have computedthe expected number of sales, you should subtract a small number (like 1e-9)from this number before rounding up.

Constraints

-

daysAvailable and sales will have the samenumber of elements.

-

daysAvailable and sales will both havebetween 1 and 50 elements, inclusive.

-

Each element of daysAvailable will bebetween 0 and 30, inclusive.

-

Each element of sales will be between 0 and10,000, inclusive.

-

If an element of daysAvailable is 0, thecorresponding element of sales will also be 0.

-

At least one element of daysAvailable willbe greater than 0.

-

The expected number of sales, prior torounding, will not be within 1e-9 of an integer, unless the expected number isexactly an integer.

Examples

0)

 

    

{5}

{15}

Returns: 10

If 5 items are sold in 15 days (half amonth), then 10 items could have been sold in a full month.

1)

 

    

{75,120,0,93}

{24,30,0,30}

Returns: 103

The expected number of sales per month is102.25. Rounding up, we get 103.

2)

 

    

{8773}

{16}

Returns: 16450

 

3)

 

    

{1115,7264,3206,6868,7301}

{1,3,9,4,18}

Returns: 36091

Watch out for double imprecision. Theexpected number of sales per month, without rounding, is exactly 36091.

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

 

159.72

#include<vector>

#include<iostream>

#include<string>

 

#include<cmath>

 

using namespace std;

 

class Inventory

{

 

public:int monthlyOrder(vector <int>sales, vector <int> daysAvailable)

         {

                      int ans=0;

                      double sum=0;

                      int n=sales.size();

                      for(int i=0;i<sales.size();i++)

                      {

                               if (daysAvailable[i]!=0)

                                       sum+=(double)sales[i]*30.0/(double)daysAvailable[i];

                               else n--;

                      }

                      sum/=(double)n;

                      ans=ceil(sum-0.000000001);

                      return ans;

         }

};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值