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

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

  1.  1 #include <stdio.h>
  2.  2 #include <stdlib.h>
  3.  3 #include <math.h>
  4.  4 #include <string.h>
  5.  5 
  6.  6 struct P{
  7.  7     int xi;
  8.  8     int fi;
  9.  9     int ci;
  10.      int cost;
  11.  }p[];
  12.  
  13.  int cmp(const void *a, const void *b){
  14.      struct P *= (struct P *)a;
  15.      struct P *= (struct P *)b;
  16.      return c->cost - d->cost;
  17.  }
  18.  int main(){
  19.      int c, k, e, n;
  20.      int i;
  21.      int ans;
  22.      while(scanf("%d", &c) != EOF){
  23.          while(c--){
  24.              scanf("%d %d %d", &k, &e, &n);
  25.              for(= ; i < n; i++){
  26.                  scanf("%d %d %d", &p[i].xi, &p[i].fi, &p[i].ci);
  27.                  //计算当前点1单位的种子运到重点需要花费多少?
  28.                  p[i].cost = e - p[i].xi + p[i].ci;
  29.              }
  30.              qsort(p, n, sizeof(p[]), cmp);
  31.  //            for(i = 0; i < n; i++){
  32.  //                printf("%d %d %d %d\n", p[i].xi, p[i].fi, p[i].ci, p[i].cost);
  33.  //            }
  34.              ans = i = ;
  35.              while(>= p[i].fi){
  36.                  ans += p[i].fi * p[i].cost;
  37.                  k -= p[i].fi;
  38.                  i++;
  39.              }
  40.              if(> ){
  41.                  ans += k * p[i].cost;
  42.              }
  43.              printf("%d\n", ans);
  44.          }
  45.      }
  46.      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. jqgrid 不能选中行, 每次点击单元格都自动选中第一行

    最使用jqgrid表格插件写了一个功能.功能完成后显示一切正常,但是经过测试后发现,每次点击数据行时,都会自动选中第一行,无法选中其他数据行.经过一番探索,最终发现是加载进来的字段没有主键导致了这个问 ...

  2. HDU3038【种类并查集】

    题意: 给出m组区间[a,b],以及其区间的和,问有矛盾的有几组: 思路: 种类并查集. 主要是几个关系:同类元素的关系,父亲与儿子的关系,不同类元素的关系: 我们可以类似看作一个前缀和,sum[x] ...

  3. Java 工程师面试题和笔试题整理(一)

    根据自己之前收集的还有一部分自己面试的整理出来,希望能帮到面试的兄弟(2017). 海科融通 笔试题 1.有一个字符串,如果要在其中查找一个子串,都有哪些方式,写出你认为最好的一个. 2.写出线程都有 ...

  4. 牛客寒假6-C.项链

    链接:https://ac.nowcoder.com/acm/contest/332/C 题意: 小B想给她的新项链染色. 现在有m种颜色,对于第i种颜色,小B有a_i单位的颜料,每单位颜料可以染项链 ...

  5. Codeforces 1131G(dp)

    传送门 与Codeforces1107G一起食用 思路 想到要用dp--然后常规地设dp[i]为推倒前i个牌的最小花费 有两种情况:一是当前这个推,二是不推而被别人推.对于第一种,需要找到这个左推(因 ...

  6. Influxdb 时序数据库 windows 安装

    Influxdb 是一款比较火爆的时序数据库,本文介绍如何在 windows 平台下安装. 1.场景: windows 平台的 influxdb 似乎只支持单机非windows 服务的安装方式 适用于 ...

  7. wmq的队伍 BIT优化dp

    http://oj.xjtuacm.com/problem/14/ wmq的队伍 发布时间: 2017年4月9日 17:06   最后更新: 2017年4月9日 17:07   时间限制: 2000m ...

  8. Linux--NiaoGe-Service-06

    Linux网络排错 思路: 硬件问题: 首先排除硬件故障,包括网线.Hub.Switch.Router.网卡.设备配置规则等等. 软件问题: 1.网卡的IP/Netmask设置错误 IP.Netmas ...

  9. 我的NopCommerce之旅(7): 依赖注入(IOC/DI)

    一.基础介绍 依赖注入,Dependency Injection,权威解释及说明请自己查阅资料. 这里简单说一下常见使用:在mvc的controller的构造方法中定义参数,如ICountryServ ...

  10. mongodb 正则

    正则表达式常用来在所有语言中搜索字符串的任何模式或文字.MongoDB还提供了正则表达式功能的字符串模式使用正则表达式$regex操作符.MongoDB使用PCRE(Perl兼容正则表达式)为正则表达 ...