1. 题目描述

Problem Statement

  You are playing a game called Slime Tycoon.You will be selling Slimonades in this game, and your goal is to sell as many as you can.

The game will consist of N game days, numbered 0 through N-1 in order.You are given two vector <int>s morning and customers with N elements each, and an int
stale_limit.These represent constraints on how many Slimonades you can produce and sell, as explained below.



In each game day, three things happen, in the following order:

  • Early in the morning of day i: All Slimonades that were produced stale_limit days ago (i.e., on day i-stale_limit) go stale. You cannot sell stale Slimonades, you must throw them away immediately.
  • During day i: You can produce at most morning[i] new Slimonades. (Formally, you choose an integer X between 0 and
    morning[i], inclusive, and produce X Slimonades.)
  • In the evening of day i: You can sell at most customers[i] Slimonades. (That is, if you have at mostcustomers[i] Slimonades, you sell all of them. Otherwise, you sell exactly customers[i] Slimonades. In
    that case, you get to choose which Slimonades you sell and which ones you keep for later days.)

What is the maximum total number of Slimonades that you can sell during these N days?

Definition

 
Class: SlimeXSlimonadeTycoon
Method: sell
Parameters: vector <int>, vector <int>, int
Returns: int
Method signature: int sell(vector <int> morning, vector <int> customers, int stale_limit)
(be sure your method is public)

Limits

 
Time limit (s): 2.000
Memory limit (MB): 256

Constraints

- morning will contain between 2 and 50 elements, inclusive.
- Each element of morning will be between 0 and 10000, inclusive.
- customers will contain the same number of elements as
morning
.
- Each element of customers will be between 0 and 10000, inclusive.
- stale_limit will be between 1 and N, inclusive.

Examples

 
{5, 1, 1}
{1, 2, 3}
2
Returns: 5
Here's one optimal solution.

  • Day 0: We produce 4 Slimonades, then sell 1 of them.
  • Day 1: We produce 1 Slimonade (so now we have 4). In the evening, we sell two of the Slimonades that were made yesterday.
  • Day 2: We still have one Slimonade that was made on day 0. It goes stale and we throw it away. We produce one more Slimonade. In the evening, we sell 2 Slimonades (the one made yesterday and the one made today).

2. 分析

采用如下的策略,每天尽可能多地生产,并且优先卖生产日期久的。。采用一个队列。。

3. 代码

class SlimeXSlimonadeTycoon
{
public:
int sell(vector <int> morning, vector <int> customers, int stale_limit)
{
if(morning.empty()) return 0; int que[100000];
int front = 0, rear = 0;
int count(0);
for(int i=0; i<morning.size(); ++i)
{
if(front - rear == stale_limit) ++rear;
que[front++] = morning[i]; while(rear < front && customers[i] > 0)
{
if(que[rear] > customers[i]){
count += customers[i];
que[rear] -= customers[i];
break;
}
else{
count += que[rear];
customers[i] -= que[rear];
++rear;
}
}
}
return int(count) ;
}
};

TopCoder----卖柠檬的更多相关文章

  1. JS—实现拖拽

    JS中的拖拽示例:    1)实现拖拽思路:当鼠标按下和拖拽过程中,鼠标与拖拽物体之间的相对距离保持不变    2)实现拖拽遇到的问题:        问题1:当鼠标按下移动过快时,离开了拖拽的物体时 ...

  2. 怎么让猫吃辣椒 转载自 xiaotie

    典故: 某日,毛.周.刘三人聊天. 毛:怎么能让猫自愿吃辣椒? 刘:掐着脖子灌. 毛:强迫不是自愿. 周: 先饿几天,再混到猫爱吃的东西里. 毛:欺骗不是自愿.把辣椒涂到猫肛门上,它就会自己去舔了. ...

  3. 【LeetCode】860. Lemonade Change 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [LeetCode] Lemonade Change 买柠檬找零

    At a lemonade stand, each lemonade costs $5.  Customers are standing in a queue to buy from you, and ...

  5. 如何一步一步用DDD设计一个电商网站(四)—— 把商品卖给用户

    阅读目录 前言 怎么卖 领域服务的使用 回到现实 结语 一.前言 上篇中我们讲述了“把商品卖给用户”中的商品和用户的初步设计.现在把剩余的“卖”这个动作给做了.这里提醒一下,正常情况下,我们的每一步业 ...

  6. Java多线程卖票例子

    package com.test; public class SaleTickets implements Runnable { private int ticketCount = 10;// 总的票 ...

  7. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  8. 1奶茶店创业成本: 2发饰品加盟店创业成本 3眼镜行业店创业成本 从“程序员转行卖烧饼”想到IT人创业

    总结: -------奶茶店创业成本: 而这个奶茶店初期投资是:3万元加盟费+1万元保证金+8000装修+两万设备(冰柜.展示柜.收银机等等).别说赚钱,什么时候把初期投资赚回来呀! 一个店的利润就是 ...

  9. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  10. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

随机推荐

  1. YTU 3025: 创建二叉树

    原文链接:https://www.dreamwings.cn/ytu3025/2628.html 3025: 创建二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 3 ...

  2. ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多

    ListView实现Item上下拖动交换位置  并且实现下拉刷新  上拉加载更多 package com.example.ListViewDragItem; import android.app.Ac ...

  3. 用webdriver+phantomjs实现无浏览器的自动化过程

    环境准备 1. 安装python: 2. 安装pip: 3. 通过pip安装selenium: 4. 下载phantomJS的包并解压缩: 1. 若在Windows系统中,将下载的phantomjs文 ...

  4. linux定时执行任务

    (1)Linux下如何定时执行php脚本?(2)Linux下如何设置定时任务?(3)Crontab定时执行程序 核心提示:键入 crontab -e 编辑crontab服务文件 分为两种情况:(还有一 ...

  5. jquery.validate使用 - 2

    jQuery.validate.js API说明 参考http://ideabean.javaeye.comPlugin methods Name Type validate( options ) R ...

  6. 使用Github管理项目代码的版本

    作为开源代码库以及版本控制系统,Github拥有140多万开发者用户.随着越来越多的应用程序转移到了云上,Github已经成为了管理软件开发以及发现已有代码的首选方法. 在Git中并不存在主库这样的概 ...

  7. poj3301Texas Trip(三分)

    链接 这题还真没看出来长得像三分.. 三分角度,旋转点. 最初找到所有点中最左边.右边.上边.下边的点,正方形边长为上下距离和左右距离的最大值,如图样例中的四个点(蓝色的),初始正方形为红色的正方形. ...

  8. 关于 RxJava 技术介绍

    Awesome-RxJava RxJava resources Blog 给 Android 开发者的 RxJava 详解 -强烈推荐 扔物线的文章 讲解非常详细 NotRxJava懒人专用指南 -这 ...

  9. [讨论] win7封装时如何直接开通局域网共享

    ekincheng 发表于 2016-10-31 20:17:54 https://www.itsk.com/thread-371838-1-5.html Win7封装时不能像XP那样直接开启局域网共 ...

  10. 用cxf开发restful风格的WebService

    我们都知道cxf还可以开发restful风格的webService,下面是利用maven+spring4+cxf搭建webService服务端和客户端Demo 1.pom.xml <projec ...