总时间限制: 1000ms 内存限制: 65536kB

描述

The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10,000) weeks, the price of milk and labor will fluctuate weekly such that it will cost the company C_i (1 <= C_i <= 5,000) cents to produce one unit of yogurt in week i. Yucky’s factory, being well-designed, can produce arbitrarily many units of yogurt each week.
Yucky Yogurt owns a warehouse that can store unused yogurt at a constant fee of S (1 <= S <= 100) cents per unit of yogurt per week. Fortuitously, yogurt does not spoil. Yucky Yogurt’s warehouse is enormous, so it can hold arbitrarily many units of yogurt.
Yucky wants to find a way to make weekly deliveries of Y_i (0 <= Y_i <= 10,000) units of yogurt to its clientele (Y_i is the delivery quantity in week i). Help Yucky minimize its costs over the entire N-week period. Yogurt produced in week i, as well as any yogurt already in storage, can be used to meet Yucky’s demand for that week.

输入

  • Line 1: Two space-separated integers, N and S.

  • Lines 2…N+1: Line i+1 contains two space-separated integers: C_i and Y_i.

输出

  • Line 1: Line 1 contains a single integer: the minimum total cost to satisfy the yogurt schedule. Note that the total might be too large for a 32-bit integer.

样例输入

4 5
88 200
89 400
97 300
91 500

样例输出

126900

提示

OUTPUT DETAILS:
In week 1, produce 200 units of yogurt and deliver all of it. In week 2, produce 700 units: deliver 400 units while storing 300 units. In week 3, deliver the 300 units that were stored. In week 4, produce and deliver 500 units.

一开始的代码是这样的,复杂度为O(n^2)

#include <cstring>
#include <iostream>
#include <queue>
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>#define MAX_N 1010
#define MAX_M 10010
using namespace std;int main()
{int n, s;int Y[MAX_M] = {0}, C[MAX_M] = {0};long long total = 0;long long tmpmin = 0x3f3f3f;while (scanf("%d%d", &n, &s) != EOF) {memset(Y, 0, sizeof(Y));memset(C, 0, sizeof(C));total = 0;for (int i = 0; i < n; i++) {scanf("%d%d", &C[i], &Y[i]);}for (int i = n - 1; i >= 0; i--) {tmpmin = 1ll * C[i] * Y[i];for (int j = 0; j < i; j++) {long long curval = 1ll * C[j] * Y[i] + 1ll * (i-j) * s * Y[i];if (curval < tmpmin) {tmpmin = curval;}}total += tmpmin;}printf("%lld\n", total);}return 0;
}

后来看到有其他的做法,复杂度只有O(n),

#include <cstring>
#include <iostream>
#include <queue>
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>#define MAX_N 1010
#define MAX_M 10010
using namespace std;int main()
{int n, s, y, c, m;long long total = 0;while (scanf("%d%d", &n, &s) != EOF) {total = 0;m = 0x3f3f3f;while (n--) {scanf("%d%d", &c, &y);m = min(m+s, c);total += 1ll * m * y;}printf("%lld\n", total);}return 0;
}

区别就在于如何遍历所有的日期,我的做法是从后面开始遍历,对于每一天,再从第一天一直找到这一天期间所有的价格,找到一个最小的加上,这样复杂度很高。第二种方法使用m=min(m+s,c)通过将单价c与前一日单价与保存单价之间的比较来得到最后的结果,效率更高。

不过一定要注意的是,对于中间计算结果一定要乘上1ll,就是1后面加一个longlong转换类型,不然会疯狂WA。

POJ 2393 Yogurt factory(贪心)相关推荐

  1. P1376 [USACO05MAR]Yogurt factory 机器工厂

    P1376 [USACO05MAR]Yogurt factory 机器工厂 解题思路:贪心.到达第i周的时候,判断一下是此时的C[i]*Y[i]低,还是往上的某一周到这一周的成本加存储费低. #inc ...

  2. POJ 1017 Packets【贪心】

    POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6.  这些产品通常 ...

  3. POJ 3040 Allowance【贪心】

    POJ 3040 题意: 给奶牛发工资,每周至少 C 元.约翰手头上有面值V_i的硬币B_i个,这些硬币的最小公约数为硬币的最小面值.求最多能发几周? 分析: 贪心策略是使多发的面额最小(最优解).分 ...

  4. POJ 2287 田忌赛马(贪心)

    文章目录 1. 题目 1.1 题目链接 1.2 题目大意 1.3 解题思路 2. Accepted 代码 1. 题目 1.1 题目链接 http://poj.org/problem?id=2287 1 ...

  5. 【POJ 3614 Sunscreen】贪心 优先级队列

    题目链接:http://poj.org/problem?id=3614 题意:C头牛去晒太阳,每头牛有自己所限定的spf安全范围[min, max]:有L瓶防晒液,每瓶有自己的spf值和容量(能供几头 ...

  6. POJ 3111 K Best 贪心 二分

    题目链接: http://poj.org/problem?id=3111 题目描述: 在N个物品中让你选出K个, 使得平均价值最大 解题思路: 这是我错的代码......一会儿回来改啊......一会 ...

  7. POJ 2456 疯牛(二分+贪心)

    疯牛 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 4 描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些 ...

  8. NYOJ 586 疯牛 POJ 2456(二分搜索 + 贪心)

    疯牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间 ...

  9. POJ - 1328 Radar Installation(贪心+思维)

    题目链接:点击查看 题目大意:校长想通过监控设备覆盖学校内的N座建筑物,每座建筑物被视作一个质点,在笛卡尔坐标系中给出他们的坐标(x,y),并且所有建筑物均处在x轴的上方.因为学校的供电和传输线路均沿 ...

最新文章

  1. ROS知识(4)----初级教程之常见问题汇总
  2. 北航云计算公开课 01 Introduction to Cloud Computing
  3. 通过js引入当前所需要的js,css等
  4. java.util类,GitHub - yutaolian/JavaUtils: 总结的一些Java常用的util类
  5. Python 基础语法_Python脚本文件结构
  6. golang编译错误 copying /tmp/go-build069786374/b001/exe/a.out: No such file or directory 解决方法
  7. pwn入门-PLT表与GOT表、libc入门
  8. 面试前需要准备的五个步骤
  9. 5G NR 频率 带宽 栅格
  10. java初学者只要掌握了以下十大原则,可以让你的技术飙升
  11. 16.IDA-列出函数中存在的全部call(函数调用窗口,查看函数内调用了哪些call)
  12. HTTP与Tcp协议下双工通信的差异
  13. [CQOI2011]放棋子
  14. 华为手机的nfc是什么功能_NFC功能是什么意思,华为手机NFC功能怎么用,NFC常用功能场景...
  15. 计算机算log的原理,一位业余爱好者的研究,原本是第一台机械计算器,就这么胎死腹中...
  16. 计算机科学与技术专业读书笔记300字,计算机读书笔记.doc
  17. abstract 和 introduction的写法
  18. STM32——用固件库实现流水灯(源码+仿真图)
  19. word中为代码块儿添加背景色
  20. 【小说】玻璃碎片-第三章

热门文章

  1. 一款练习汇编的神器——DosBox
  2. 计算机网络8 互联网上的音视频服务
  3. 有一个超毒舌的对象是什么体验?
  4. Ubuntu 11.04 更新源(ubuntu yuan)
  5. 用arduino对stm32编程(一)
  6. 从在浏览器地址栏中输入www.baidu.com到看到百度首页,这个过程中间经历了什么?都涉及到哪些网络协议?
  7. 【STM32】窗口看门狗程序
  8. 吉林大学2013级大一下学期程序设计作业:同学通讯录系统
  9. 《GhostXP_SP2电脑公司特别版_8.0》
  10. 【五校联考1day1】我才不是萝莉控呢