• 贪心题目(一)


    贪心法(又称贪婪法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,所做出的是在某种意义上的局部最优解。

    贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关。

    HDU 1009(贪心)

    FatMouse' Trade

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 107030    Accepted Submission(s): 37388

    Problem Description

    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

    代码如下:

    #include<stdio.h>
    #include <stdlib.h>
    typedef struct trade_s {
        //注意这里只能是整数
        int javaBean, catFood;
        double normalize;
    } trade_t;
    
    static trade_t w[1100];
    
    //用qsort函数必须注意这个函数返回的是int类型,所以这里会返回1和-1
    int cmp(const void * x, const void * y) {
        //注意这里直接用减法并且返回1和-1
        return ((trade_t*)x)->normalize - ((trade_t*)y)->normalize > 0 ? -1 : 1;
    }
    
    int main() {
        //n是行数,m是总重量
        int n, m, i;
        double sum;
    
        while (scanf("%d %d", &m, &n)) {
            //输入-1结束
            if (m == -1 && n == -1) break;
            //重置sum
            sum = 0;
            //输入n行数据
            for (i = 0; i <= n - 1; i++) {
                //得到javaBean需要付出catFood
                scanf("%d %d", &w[i].javaBean, &w[i].catFood);
                //计算单价购买量
                w[i].normalize = ((double)w[i].javaBean) / w[i].catFood;
            }
            //排序
            qsort(w, n, sizeof(trade_t), cmp);
    
            for (i = 0; i < n; i++) {
                //如果支付得起,则购买
                if (m >= w[i].catFood) {
                    printf("food %d %d
    ", w[i].catFood, w[i].javaBean);
                    //增加获得的重量
                    sum = sum + w[i].javaBean;
                    //减去付出
                    m -= w[i].catFood;
                } else {
                    //支付不起,以单价购买量乘以重量
                    sum = sum + w[i].normalize * m;
                    break;
                }
            }
            printf("%.3lf
    ", sum);
        }
        return 0;
    }

    HDU 1052 (贪心)

    Tian Ji -- The Horse Racing

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 40927    Accepted Submission(s): 12307

    Problem Description

    Here is a famous story in Chinese history.
    "That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."
    "Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."
    "Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."
    "Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."
    "It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"

    Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...
    However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.
    In this problem, you are asked to write a program to solve this special case of matching problem.

    Input

    The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.

    Output

    For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

    Sample Input

    3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0

    Sample Output

    200 0 0

    解题思路:

    一、如果t的最慢速度大于k的最慢速度,则直接t的最慢与k的最慢比一场,赢得比赛

    二、如果t的最慢速度小于k的最慢速度,则用t的最慢浪费k的最快,输一场

    三、如果t的最慢速度等于k的最慢速度,则:

    1、如果t的最快速度大于k的最快速度,则用t的最快与k的最快比一场,赢得比赛

    2、如果t的最快速度小于k的最快速度,则用t的最慢浪费k的最快,输一场

    3、如果t的最快速度等于k的最快速度,即t的最慢等于k的最慢,t的最快等于k的最快,则:

    a、如果t的最慢小于k的最快,则用t的最慢浪费k的最快,输一场

    b、如果t的最慢等于k的最快,则t的最慢、最快,k的最慢、最快均相等,说明所有马速度均相等,直接结束比赛

    代码如下:

    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    using namespace std;
    int T[1004],K[1004],n,win,lose;
    void read(){
        for(int i=0;i<n;i++)
            scanf("%d",&T[i]);
        for(int i=0;i<n;i++)
        scanf("%d",&K[i]);
        sort(T,T+n);
        sort(K,K+n);
    }
    void race(){
        win=lose=0;
        int t_slow=0,t_fast=n-1;
        int k_slow=0,k_fast=n-1;
        while(t_slow<=t_fast){
            if(T[t_slow]>K[k_slow]){
                win++;
                t_slow++;
                k_slow++;
            }
            else if(T[t_slow]<K[k_slow]){
                lose++;
                t_slow++;
                k_fast--;
            }
            else{
                if(T[t_fast]>K[k_fast]){
                    win++;
                    t_fast--;
                    k_fast--;
                }
                else 
                {
                    if(T[t_slow]<K[k_fast])
                    lose++;
                    t_slow++;
                    k_fast--;
                }
                
            }
        }
    }
    int main(){
        while(scanf("%d",&n),n){
            read();
            race();
            printf("%d
    ",200*(win-lose));
        }
        return 0;
    }
    天晴了,起飞吧
  • 相关阅读:
    IIS常见500错误解决方案
    发送邮件代码
    IIS站点/虚拟目录中访问共享目录(UNC)
    简简单单,一目了然C#与Matlab
    [转载]C#——DataGridView分页功能的实现
    博客之旅
    ASP.Net, Php , Java/Java EE?好困惑
    【转载】DataGridView中虚拟模式(Virtual Mode)用法
    selenium4.0降级为3版本
    web自动化中影响页面定位的场景有哪些?
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/11173438.html
Copyright © 2020-2023  润新知