码迷,mamicode.com
首页 > 其他好文 > 详细

usaco-mixing milk-pass

时间:2014-08-31 11:42:41      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   for   数据   div   

呵呵,通过:

/*
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\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: 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
。。。。。。。。。。

 

usaco-mixing milk-pass

标签:style   blog   color   os   io   ar   for   数据   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!