COJ 1156 Switching bulbs

贪心...

/*Accepted 2352 kb 292 ms C++ 1013 B 2012-04-14 21:39:44 */



#include<cstdio>

#include<cstring>

#include<cstdlib>

#define MAXN 100005



int N, M;

typedef struct

{

    int color, val;

}T;



T t[MAXN];



int cmp( const void *_p, const void *_q)

{

    T *p = ( T *)_p;

    T *q = ( T *)_q;

    return p->val - q->val;

}



int main()

{

    while( scanf( "%d%d", &N, &M) == 2)

    {

        int ans = 0, min = 0x3f3f3f3f;

        for( int i = 0; i < N; i ++)

            scanf( "%d", &t[i].color);

        for( int i = 0; i < N; i ++)

        {

            scanf( "%d", &t[i].val);

            if( t[i].color == 1) {

                ans += t[i].val;

                t[i].val = -t[i].val;

            }

            else t[i].val = t[i].val;

            if( abs(t[i].val) < min)  min = abs(t[i].val);

        }

        qsort( t, N, sizeof t[0], cmp);

        int i;

        for( i = N - 1; i >= 0; i --)

        {

            if( t[i].val < 0 || M == 0) break;

            M --;

            ans += t[i].val;

        }

        if( M % 2)  ans += -min;



        printf( "%d\n", ans);

    }

    return 0;

}

 

你可能感兴趣的:(switch)