贪心算法HURUST题目
题目描述:
Yogurt factory
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.
* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and Y_i.
4 5
88 200
89 400
97 300
91 500
126900 -----------------------
这道题是我在HURBST做的题目,链接是https://vjudge.net/contest/262212#problem/D 中文理解:有一个酸奶酪生产商,每一个星期都有其需求量和价格量,奶酪可以保存到下个星期使用,不过有保存费用,此题目要求你找出最小费用的解; 个人看法:这道题目是贪心的基础题目,它满足了贪心算法的基于当前是最优解,找出下一个最优解,保证这一步是最优解。
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<iterator>
#include<string>
#include<string.h>
#include<ctype.h>
#include<map>
#include<stack>
#include<queue>
#include<iostream>
#include<time.h> using namespace std; #define rep(i ,a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i <= b; i--)
typedef long long ll;//数据量大,用longlong来进行运算较合适。不然会数据溢出。 struct milk
{
ll c_i, y_i, id;
}a[]; ll findmin(ll n, ll s)
{
ll ans=a[].c_i*a[].y_i, k = ;
for(ll i = ; i < n; i++)
{
ll temp = a[k].c_i+s*(i- a[k].id), cur = a[i].c_i;//记录前一周牛奶的价格和这个一周的价格,进行比较然后,选取较小的作为当前的最小解;
if(temp < cur)
{
ans += temp*a[i].y_i;
}
else
{
ans += cur*a[i].y_i;
k = i;
}
}
return ans;
} int main()//这是贪心问题。
{
ll n, s;
while(scanf("%lld%lld", &n, &s) != EOF)
{
for(ll i =; i < n; i++)
{
scanf("%lld%lld", &a[i].c_i, &a[i].y_i);
a[i].id = i;
}
printf("%lld\n", findmin(n, s));
}
return ;
}
贪心算法HURUST题目的更多相关文章
- 1033. To Fill or Not to Fill (25) -贪心算法
题目如下: With highways available, driving a car from Hangzhou to any other city is easy. But since the ...
- 【九度OJ】题目1434贪心算法
题目 本题的贪心算法策略需要深入思考一下 看到题目,最初没有理解题目的要求:看尽量多的完整的节目.尽量多是指数量多,自己理解成观看的时间最长.这样想其实简化了这道题. 正确理解题意后,首先想到的想法是 ...
- Leetcode题目55.跳跃游戏(贪心算法-中等)
题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...
- 题目1437:To Fill or Not to Fill:贪心算法解决加油站选择问题(未解决)
//贪心算法解决加油站选择问题 //# include<iostream> # include<stdio.h> using namespace std; # include& ...
- 贪心算法(Greedy Algorithm)
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
- poj 1088 滑雪(贪心算法)
思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...
- 剑指Offer——贪心算法
剑指Offer--贪心算法 一.基本概念 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解.虽然贪心算法不能对 ...
- 贪心算法----区间选点问题(POJ1201)
题目: 题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量. 输入: 第一行输入n,代表n个 ...
- python数据结构与算法第十六天【贪心算法与动态规划】
对于一个字符串,对字符串进行分割,分割后的每个子字符串都为回文串,求解所有可行的方案 这个问题可以使用贪心算法与动态规划来求解 步骤如下: (1)先得出所有的单个字符的回文串,单个字符必定是回文串, ...
随机推荐
- 小程序码B接口生成出错:场景内容包含非法字符
由于包含了非法字符,微信返回的字节不超过100字符,但是没有包含提示内容,因此很难识别发现问题所在
- kettle数据同步
通过kettle实现两张表的数据同步,具体设计如下:
- 大数据开发实战:HDFS和MapReduce优缺点分析
一. HDFS和MapReduce优缺点 1.HDFS的优势 HDFS的英文全称是 Hadoop Distributed File System,即Hadoop分布式文件系统,它是Hadoop的核心子 ...
- selenium-百度搜索框输入后,定位联想下拉框元素
1.输入关键字后,显示联想下拉框,鼠标右键对应的联想字段,点击检查,就可在F12模式下元素查看器中定位到,之后使用Xpath定位.
- linux 文本操作
sed -i 直接修改 sed 是负责插入替换删除字符串操作. sed -n '/11/p' 11.txt |sed 's/11/33333/g' 查找11并替换11位3333 sed 's/1 ...
- PTA-B 1039 到底买不买 解题思路记录
#include <cstdio> #include <string> #include <iostream> using namespace std; int m ...
- Visual Studio内存泄露检測工具
使用简单介绍 在敲代码的过程中.难免会遇到内存泄露的时候.这个时候假设手工查找内存泄露,不说方法没有通用的,就是真的要自己手工查找也是非常耗时间和精力的.诚然.我们能够借助一些工具,并且我们还 ...
- Winfrom 使用WCF 实现双工通讯
实现双工通讯主要分三步. 通信接口的定义: 被调用接口的实现 双工通道的建立 请先引用DLL(CSDN的代码编辑器真尼玛蛋疼) 整个解决方案的结构 1.通信接口的定义: 服务端调用客户端接口IServ ...
- oracle查询父节点及其下所有子节点
1.我们的组织机构就是这种树形菜单的格式. . 2.执行sql: select ( select organization_name from SYS_ORGANIZATION where organ ...
- 使用Tortoise结合Git比较两个版本的差异
1.右键项目,TortoiseGit -------> Diff with previous version 2.单击出分支选择弹窗,进行选择要比较的两个分支 3.比较同个分支的两个不同的版本 ...