zoukankan      html  css  js  c++  java
  • usaco-mixing milk-pass

    呵呵,通过:

    /*
    ID: qq104801
    LANG: C++
    TASK: milk
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    
    /* for debug only:counter
    */
    void debug_dummy(void)
    {
        return;
    }
    
    #define NMAX 5001
    
    int n,m;
    typedef struct _bb
    {
        int price;
        int amount;
    }bb;
    bb k[NMAX];
    
    int cmp(const void *a,const void *b)
    {
        bb *pa=(bb*)a;
        bb *pb=(bb*)b;
        if (pa->price!=pb->price) 
            return (pa->price - pb->price);
        else return (pa->amount -pb->amount);
    }
    
    void test(FILE* f)
    {
        int cost=0,amount=0;
        for(int i=0;i<m;i++)
        {   
            if ((n-amount)>k[i].amount)
            {
                amount+=k[i].amount;
                cost+=k[i].amount * k[i].price;    
            }
            else
            {
                cost+=(n-amount) * k[i].price;
                //printf("%d %d  %d
    ",k[i].price,k[i].amount,n-amount);
                //printf("%d
    ",cost);
                fprintf(f,"%d
    ",cost);
                return;
            }   
            //printf("%d %d
    ",k[i].price,k[i].amount);
        }
    }
    
    main () {    
        FILE *fin = fopen ("milk.in", "r");
        FILE *fout = fopen ("milk.out", "w"); 
        fscanf(fin,"%d %d",&n,&m);
        if(n==0)
        {
            fprintf(fout,"0
    ");
            exit(0);
        }
        //printf("%d %d
    ",n,m);    
        for(int i=0;i<m;i++)
        {
            fscanf(fin,"%d %d",&k[i].price,&k[i].amount);
            //printf("%d %d
    ",k[i].price,k[i].amount);
        }        
        qsort(k,m,sizeof(k[0]),cmp);    
        test(fout);
        
        fclose(fin);
        fclose(fout);
        exit (0);
    }

    测试数据记录一下:

    USER: ll tom [qq104801]
    TASK: milk
    LANG: C++
    
    Compiling...
    Compile: OK
    
    Executing...
       Test 1: TEST OK [0.011 secs, 3532 KB]
       Test 2: TEST OK [0.003 secs, 3540 KB]
       Test 3: TEST OK [0.008 secs, 3532 KB]
       Test 4: TEST OK [0.011 secs, 3532 KB]
       Test 5: TEST OK [0.008 secs, 3532 KB]
       Test 6: TEST OK [0.016 secs, 3532 KB]
       Test 7: TEST OK [0.014 secs, 3532 KB]
       Test 8: TEST OK [0.016 secs, 3532 KB]
    
    All tests OK.
    
    Your program ('milk') produced all correct answers! This is your submission #3 for this problem. Congratulations!
    
    Here are the test data inputs:
    
    ------- test 1 ----
    100 5
    5 20
    9 40
    3 10
    8 80
    6 30
    ------- test 2 ----
    0 0
    ------- test 3 ----
    2 1
    10 2
    ------- test 4 ----
    100 6
    5 20
    9 40
    3 10
    8 80
    6 30
    0 100
    ------- test 5 ----
    2000000 1000
    1000 2000000
    999 2000000
    998 2000000
    997 2000000
    996 2000000
    995 2000000
    994 2000000
    993 2000000
    992 2000000
    991 2000000
    990 2000000
    989 2000000
    988 2000000
    。。。。。。。。。。
    /***********************************************

    看书看原版,原汁原味。

    不会英文?没关系,硬着头皮看下去慢慢熟练,才会有真正收获。

    没有原书,也要网上找PDF来看。

    网上的原版资料多了去了,下载东西也到原始下载点去看看。

    你会知其所以然,呵呵。

    ***********************************************/

  • 相关阅读:
    Spring声明式事务可能出现的问题
    SpringBoot自定义starter
    查找和最小的K对数字
    SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器
    Redis的数据结构及应用场景
    高考还没结束,这份试卷已经流出,你能拿多少分?
    Linux C/C++编程之(十六)进程及进程控制
    Dom4j修改xml文档引入
    淘宝,京东,苏宁易购技术架构(路线)分析和比较
    android 解析服务器数据使用json还是xml方式
  • 原文地址:https://www.cnblogs.com/dpblue/p/3947397.html
Copyright © 2011-2022 走看看