http://acm.nyist.net/JudgeOnline/problem.php?pid=248

BUYING FEED

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
描述

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 pound

It 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
样例输入
2 5 3                  
3 1 2 
4 1 2 
1 1 1 
样例输出
7

解题思路:有C个样例,每个样例第一行有三个整数k, e, n,分别表示农夫要买的种子数量, 这条道路的长度, 何在该条路上的商店数量,接下来n行,每行三个数字xi, fi, ci,分别表示在xi位置的店家有fi的种子且单位重量的种子的运费为ci,让你求到达目的地需要花费最少多少运费, 可以先求出每种种子运到终点的运费之后贪心

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>
 4 #include <string.h>
 5 
 6 struct P{
 7     int xi;
 8     int fi;
 9     int ci;
     int cost;
 }p[];
 
 int cmp(const void *a, const void *b){
     struct P *c = (struct P *)a;
     struct P *d = (struct P *)b;
     return c->cost - d->cost;
 }
 int main(){
     int c, k, e, n;
     int i;
     int ans;
     while(scanf("%d", &c) != EOF){
         while(c--){
             scanf("%d %d %d", &k, &e, &n);
             for(i = ; i < n; i++){
                 scanf("%d %d %d", &p[i].xi, &p[i].fi, &p[i].ci);
                 //计算当前点1单位的种子运到重点需要花费多少?
                 p[i].cost = e - p[i].xi + p[i].ci;
             }
             qsort(p, n, sizeof(p[]), cmp);
 //            for(i = 0; i < n; i++){
 //                printf("%d %d %d %d\n", p[i].xi, p[i].fi, p[i].ci, p[i].cost);
 //            }
             ans = i = ;
             while(k >= p[i].fi){
                 ans += p[i].fi * p[i].cost;
                 k -= p[i].fi;
                 i++;
             }
             if(k > ){
                 ans += k * p[i].cost;
             }
             printf("%d\n", ans);
         }
     }
     return ;

47 }

nyoj-248-buying feed的更多相关文章

  1. ACM BUYING FEED

    BUYING FEED 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 Farmer John needs to travel to town to pick up ...

  2. 2020: [Usaco2010 Jan]Buying Feed, II

    2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 220  Solved: 162[ ...

  3. BUYING FEED

    Problem F: F BUYING FEED Description Farmer John needs to travel to town to pick up K (1 <= K < ...

  4. 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II

    洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...

  5. USACO Buying Feed, II

    洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II 洛谷传送门 JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II ...

  6. 【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 ...

  7. [河南省ACM省赛-第三届] BUYING FEED (nyoj 248)

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...

  8. 【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- ...

  9. Buying Feed, 2010 Nov (单调队列优化DP)

    约翰开车回家,又准备顺路买点饲料了(咦?为啥要说"又"字?)回家的路程一共有 E 公里,这一路上会经过 K 家商店,第 i 家店里有 Fi 吨饲料,售价为每吨 Ci 元.约翰打算买 ...

  10. 【BZOJ2059】Buying Feed 购买饲料

    题面 约翰开车来到镇上,他要带V吨饲料回家.如果他的车上有X吨饲料,每公里就要花费X^2元,开车D公里就需要D* X^2元.约翰可以从N家商店购买饲料,所有商店都在一个坐标轴上,第i家店的位置是Xi, ...

随机推荐

  1. react中虚拟DOM的基本概念

    react中的核心概念 1.DOM的本质是什么: 浏览器中的概念,用js对象来表示页面上的元素,并提供操作DOM对象的API 2.什么事react中的虚拟DOM:是框架中的概念,是程序员用js对象来模 ...

  2. Codevs 1312 连续自然数和

    1312 连续自然数和 题目描述 Description 对于一个自然数M,求出所有的连续的自然数段,使得这些连续自然数段的全部数字和为M.eg:1998+1999+2000+2001+2002=10 ...

  3. 阿里巴巴开源性能监控神器Arthas初体验

    如果问性能测试中最难的是哪部分,相信很多人会说“性能调优”.确实是这样,性能调优是一个非常复杂.技术含量很高的工作.涉及到的知识面很广.以我多年从业经验来看,在企业里,大多数的性能调优都是由开发架构师 ...

  4. Python爬虫|爬取喜马拉雅音频

    "GOOD Python爬虫|爬取喜马拉雅音频 喜马拉雅是知名的专业的音频分享平台,用户规模突破4.8亿,汇集了有声小说,有声读物,儿童睡前故事,相声小品等数亿条音频,成为国内发展最快.规模 ...

  5. C 语言实例 - 从文件中读取一行

    C 语言实例 - 从文件中读取一行 从文件中读取一行. 文件 runoob.txt 内容: $ cat runoob.txt runoob.com google.com 实例 #include < ...

  6. extern用法详解(转)

    1 基本解释 extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义. 另外,extern也可用来进行链接指定. 2 问题:ext ...

  7. iOS开发 - RunLoop理解

    RunLoop概念 运行循环,一个 run loop 就是一个事件处理的循环,用来不停的调度工作以及处理事件 作用 保持程序的持续运行 监听处理App中的各种事件(触摸事件,定时器事件,selecto ...

  8. @PathVariable注解的使用

    带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义. 通过 @PathVariable 可以将 URL 中占位符参数绑 ...

  9. tcp端口扫描(python多线程)

    1 使用单线程扫描单台主机 首先实现的是对单台主机中0-1024端口的扫描,发现差不多每秒扫描一个端口,很慢. import socket def tcp_scanner(host,port): cl ...

  10. bzoj2740 串 && bzoj2176 strange string(最小表示法模板)

    https://konnyakuxzy.github.io/BZPRO/JudgeOnline/2740.html 题解讲的很清楚了 (好像等于的情况应该归入case2而不是case1?并不确定) 具 ...