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来看。

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

你会知其所以然,呵呵。

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

原文地址:https://www.cnblogs.com/dpblue/p/3947397.html