HHTC_学校集训编程题目(3)(贪心)

HHTC_学校集训编程题目(3)(贪心)

FatMouse’ Trade

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

Output

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

Sample Input

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output

13.333
31.500

这道题就是常规的贪心~
按照性价比排好序,然后开始添加~
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

struct node{
    int x,y;
    double z;
}date[1005];

bool cmp(node a,node b){
    return a.z>b.z;
}

int main()
{
    int n,m;
    while(cin>>n>>m){
        if(n == -1 && m == -1)
            break;
        double sum = 0,ans = 0;
        for(int i=0;i<m;i++){
            cin>>date[i].x>>date[i].y;
            date[i].z = date[i].x*1.0/date[i].y;
        }
        sort(date,date+m,cmp);
        for(int i=0;i<m;i++){
            if(sum <= n){
                if(sum + date[i].y <= n){
                    ans += date[i].x;
                    sum += date[i].y;
                }else{
                    ans += (n - sum) * date[i].z;
                    sum = n;
                }
            }
        }
        printf("%.3f\n",ans);
    }
    return 0;
}

Protecting the Flowers

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

Input

Line 1: A single integer N
Lines 2… N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow’s characteristics

Output

Line 1: A single integer that is the minimum number of destroyed flowers

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

Hint

FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.

关键是如何进行排序,按照时间来排序肯定是不行的,要考虑综合情况~
所以按照(花朵数/时间数)来进行排序,
如果相等也不用考虑,因为他们的效率是一样的
同时在加花朵时肯定不能用两层for循环,否则会超时,采用前缀和的思想~
记得使用long long,不然会wrong(跪在这里了)~
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

struct node{
    int x,y;
    double z;
}d[100005];

bool cmp(node a,node b){
    return a.z > b.z;
}

int main()
{
    int n;
    while(cin>>n){
        long long sum = 0;
        for(int i=0;i<n;i++){
            cin>>d[i].x>>d[i].y;
            d[i].z = d[i].y*1.0/d[i].x;
            sum += d[i].y;
        }
        sort(d,d+n,cmp);
        long long num = 0;
        for(int i=0;i<n;i++){
            sum = sum - d[i].y;
            num += sum * d[i].x * 2;
        }
        cout<<num<<endl;
    }
    return 0;
}

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.

Input

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.

Output

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.

Sample Input

4 5
88 200
89 400
97 300
91 500

Sample Output

126900

Hint

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.

主要就是比较之前生产的实际价格和当天生产的价格
看看谁的便宜就加谁~
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

struct node{
    long long x,y;
}d[10005];

int main()
{
    int n,m;
    while(cin>>n>>m){
        for(int i=0;i<n;i++)
            cin>>d[i].x>>d[i].y;
        long long sum = d[0].x * d[0].y;
        long long maxx,minx,k = 0;
        for(int i=1;i<n;i++){
            maxx = d[i].x * d[i].y;
            minx = (d[k].x + m*(i - k))*d[i].y;
            if(maxx > minx)
                sum += minx;
            else{
                sum += maxx;
                k = i;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值