WOJ1133-Candies

来源:互联网 发布:sql库位库存分配 编辑:程序博客网 时间:2024/06/06 18:40

KO loves candies. Most of all, he likes chocolate, strawberry and banana flavored ones. Now, he has N bags full of candies, each bag contains 
some candies of all three flavors. KO knows the number of candies of each flavor in each bag. He wants to put all chocolate ones into one bag, 
all strawberry ones into another bag and all banana ones into yet another bag. He has to move the candies one-by-one, because he always has to 
look at it to determine its flavor. Moving one candy from one bag into another takes 1 second Your task is to select the bag for each flavor, 
so that the total time required for KO to move all the candies into the selected bags would be minimal. Output the minimum total time. 

输入格式

There will be multiple test cases. For each test case, the first line contains a single integer N(3 <= N <= 10000) - the number of bags. Each
of the following N lines consists of three numbers c, s, b (0 <= c, s, b <= 30000) each? the number of chocolates, strawberry and banana
candies of the i-th bag. The bags are numbered from 1 to N in the order in which they appear in the input.

输出格式

For each test case, output an integer which is the minimal total time to move all the candies to the selected bags.

样例输入

510 10 1040 39 4010 20 3030 20 101 2 27

把每个属性前3大的取出来去重,然后从其中(最多9个)暴力选3个出来取max。

#include<stdio.h>int n,sum,candy[10000][3],ko[3][3];void findmax(int x,int y){int i,max=0;for(i=0;i<n;i++){if(y==1){if(i==ko[x][0])continue;}else if(y==2){if(i==ko[x][0]||i==ko[x][1])continue;}if(candy[i][x]>max){max=candy[i][x];ko[x][y]=i;}}}int main(){int i,j,k,min,temp;while(scanf("%d",&n)==1){sum=0;for(i=0;i<n;i++){scanf("%d %d %d",&candy[i][0],&candy[i][1],&candy[i][2]);sum=sum+candy[i][0]+candy[i][1]+candy[i][2];}for(i=0;i<3;i++)for(j=0;j<3;j++)findmax(i,j);min=0x7fffffff;for(i=0;i<3;i++)for(j=0;j<3;j++)for(k=0;k<3;k++){if(ko[0][i]==ko[1][j]||ko[0][i]==ko[2][k]||ko[1][j]==ko[2][k])continue;temp=sum-candy[ko[0][i]][0]-candy[ko[1][j]][1]-candy[ko[2][k]][2];if(temp<min)min=temp; }printf("%d\n",min);}return 0;}  


原创粉丝点击