首页 > 代码库 > usaco-mixing milk-pass

usaco-mixing milk-pass

呵呵,通过:

/*ID: qq104801LANG: 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 5001int 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\n",k[i].price,k[i].amount,n-amount);            //printf("%d\n",cost);            fprintf(f,"%d\n",cost);            return;        }           //printf("%d %d\n",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\n");        exit(0);    }    //printf("%d %d\n",n,m);        for(int i=0;i<m;i++)    {        fscanf(fin,"%d %d",&k[i].price,&k[i].amount);        //printf("%d %d\n",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: milkLANG: C++Compiling...Compile: OKExecuting...   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 55 209 403 108 806 30------- test 2 ----0 0------- test 3 ----2 110 2------- test 4 ----100 65 209 403 108 806 300 100------- test 5 ----2000000 10001000 2000000999 2000000998 2000000997 2000000996 2000000995 2000000994 2000000993 2000000992 2000000991 2000000990 2000000989 2000000988 2000000
。。。。。。。。。。

 

usaco-mixing milk-pass