ACM BUYING FEED
BUYING FEED
- 描述
-
Farmer John needs to travel to town to pick up K (1 <= K <= 100)pounds of feed. Driving D miles with K pounds of feed in his truck costs D*K cents.
The county feed lot has N (1 <= N<= 100) stores (conveniently numbered 1..N) that sell feed. Each store is located on a segment of the X axis whose length is E (1 <= E <= 350). Store i is at location X_i (0 < X_i < E) on the number line and can sell John as much as F_i (1 <= F_i <= 100) pounds of feed at a cost of C_i (1 <= C_i <= 1,000,000) cents per pound.
Amazingly, a given point on the X axis might have more than one store.Farmer John starts at location 0 on this number line and can drive only in the positive direction, ultimately arriving at location E, with at least K pounds of feed. He can stop at any of the feed stores along the way and buy any amount of feed up to the the store's limit. What is the minimum amount Farmer John has to pay to buy and transport the K pounds of feed? Farmer John
knows there is a solution. Consider a sample where Farmer John needs two pounds of feed from three stores (locations: 1, 3, and 4) on a number line whose range is 0..5:
0 1 2 3 4 5
---------------------------------
1 1 1 Available pounds of feed
1 2 2 Cents per poundIt is best for John to buy one pound of feed from both the second and third stores. He must pay two cents to buy each pound of feed for a total cost of 4. When John travels from 3 to 4 he is moving 1 unit of length and he has 1 pound of feed so he must pay1*1 = 1 cents.
When John travels from 4 to 5 heis moving one unit and he has 2 pounds of feed so he must pay 1*2 = 2 cents. The total cost is 4+1+2 = 7 cents.
- 输入
- The first line of input contains a number c giving the number of cases that follow
There are multi test cases ending with EOF.
Each case starts with a line containing three space-separated integers: K, E, and N
Then N lines follow :every line contains three space-separated integers: Xi Fi Ci - 输出
- For each case,Output A single integer that is the minimum cost for FJ to buy and transport the feed
- 样例输入
-
1
2 5 3
3 1 2
4 1 2
1 1 1 - 样例输出
-
7
贪心算法#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; struct Store{
int x,f,c;
Store(int x_ = 0 , int f_ = 0, int c_=0):x(x_),f(f_),c(c_){}
bool operator < (const Store& a) const{
return c <a.c;
}
}; int main(){
int m;
cin >> m;
while(m--){
int k,e,n;
cin >>k >> e>>n;
vector<Store> stores(n);
for(int i = 0 ; i < n; ++ i){
cin >> stores[i].x >> stores[i].f >>stores[i].c;
stores[i].c+=e-stores[i].x;
}
sort(stores.begin(),stores.end());
int sum = 0;
for(int i =0 ; i < n && k >= 0 ; ++ i ){
if(k >= stores[i].f){
k-=stores[i].f;
sum +=stores[i].f*stores[i].c;
}else{
sum +=k*stores[i].c;
k-=k;
}
}
cout<<sum<<endl;
}
}
ACM BUYING FEED的更多相关文章
- 2020: [Usaco2010 Jan]Buying Feed, II
2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 220 Solved: 162[ ...
- BUYING FEED
Problem F: F BUYING FEED Description Farmer John needs to travel to town to pick up K (1 <= K < ...
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- USACO Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II 洛谷传送门 JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II ...
- 【P2616】 【USACO10JAN】购买饲料II Buying Feed, II
P2616 [USACO10JAN]购买饲料II Buying Feed, II 题目描述 Farmer John needs to travel to town to pick up K (1 &l ...
- [河南省ACM省赛-第三届] BUYING FEED (nyoj 248)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...
- 【BZOJ】2020: [Usaco2010 Jan]Buying Feed, II (dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2020 和背包差不多 同样滚动数组 f[j]表示当前位置j份食物的最小价值 f[j]=min(f[j- ...
- Buying Feed, 2010 Nov (单调队列优化DP)
约翰开车回家,又准备顺路买点饲料了(咦?为啥要说"又"字?)回家的路程一共有 E 公里,这一路上会经过 K 家商店,第 i 家店里有 Fi 吨饲料,售价为每吨 Ci 元.约翰打算买 ...
- 【BZOJ2059】Buying Feed 购买饲料
题面 约翰开车来到镇上,他要带V吨饲料回家.如果他的车上有X吨饲料,每公里就要花费X^2元,开车D公里就需要D* X^2元.约翰可以从N家商店购买饲料,所有商店都在一个坐标轴上,第i家店的位置是Xi, ...
随机推荐
- 《CLR via C#》读书笔记(6)类型和成员基础
6.1 类型的各种成员 在一个类型中,可以定义0个或者多个以下种类的成员: 常量 常量是在编译时设置其值并且永远不能更改其值的字段.使用常量可以为特殊值提供有意义的名称以代替数字文本,以使代码变得更容 ...
- 设计模式学习之装饰者模式(Decorator,结构型模式)(16)
参考地址:http://www.cnblogs.com/zhili/p/DecoratorPattern.html 一.定义:装饰者模式以对客户透明的方式动态地给一个对象附加上更多的责任,装饰者模式相 ...
- PHP判断当前访问的是 微信、iphone、android 浏览器
<?phpvar ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i)=="micromess ...
- VS2015上又一必备免费插件:Refactoring Essentials
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:Refactoring Essentials是一款用于代码分析和重构的开源免费VS20 ...
- ARM伪指令,王明学learn
ARM伪指令 在ARM汇编语言程序中里,有一些特殊指令助记符与指令系统的助记符不同,没有相对应的操作码,通常称这些特殊指令助记符为伪指令,他们所完成的操作称为伪操作.伪指令在元程序中的作用是为完成汇编 ...
- How to use Ajax on Visualforce page on Salesforce platform
Just use Ajax pattern to call object data from server on visualforce page. Following is the Asynchro ...
- HDU 3727 Jewel 可持久化线段树
Jewel Problem Description Jimmy wants to make a special necklace for his girlfriend. He bought man ...
- 如何安装Ecshop for linux
下载 http://update.shopex.com.cn/version/program/ECShop/download_ecshop_utf8.php 解压缩之后把upload文件夹中的内容放到 ...
- Windows服务定时执行任务
1.创建多线程类 /// <summary> /// 多线程 /// </summary> public abstract class MuliThread<T> ...
- cvKMeans2函数用法概述
一般情况下,我们通过C++/Matlab/Python等语言进行实现K-means算法,结合近期我刚刚学的C++,先从C++实现谈起,C++里面我们一般采用的是OpenCV库中写好的K-means函数 ...