USACO——Healthy Holsteins
来源:互联网 发布:淘宝赌石 编辑:程序博客网 时间:2024/06/04 19:34
Burch & Kolstad
Farmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for the cows. Help Farmer John feed his cows so they stay healthy while minimizing the number of scoops that a cow is fed.
Given the daily requirements of each kind of vitamin that a cow needs, identify the smallest combination of scoops of feed a cow can be fed in order to meet at least the minimum vitamin requirements.
Vitamins are measured in integer units. Cows can be fed at most one scoop of any feed type. It is guaranteed that a solution exists for all contest input data.
PROGRAM NAME: holstein
INPUT FORMAT
Line 1:integer V (1 <= V <= 25), the number of types of vitaminsLine 2:V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each dayLine 3:integer G (1 <= G <= 15), the number of types of feeds availableLines 4..G+3:V integers (0 <= each one <= 1000), the amount of each vitamin that one scoop of this feed contains. The first line of these G lines describes feed #1; the second line describes feed #2; and so on.SAMPLE INPUT (file holstein.in)
4100 200 300 400350 50 50 50200 300 200 300900 150 389 399
OUTPUT FORMAT
The output is a single line of output that contains:
- the minimum number of scoops a cow must eat, followed by:
- a SORTED list (from smallest to largest) of the feed types the cow is given
SAMPLE OUTPUT (file holstein.out)
2 1 3思路:由于测试数据小,可以直接枚举各种可能,用二进制表示各种可能,各个位数上1代表喂养,0代表不喂养即可
/*ID: youqihe1PROG: holsteinLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <string.h>#include<algorithm>using namespace std;struct food{ int vit[30]; int choose;}A[20];void binary(int n,char C[]){ int i=0; char B[30]; memset(B,'\0',sizeof(B)); while(n) { int k=n%2; B[i++]=k+'0'; n/=2; } int t=0,j; for(j=i-1;j>=0;j--,t++) C[j]=B[t];}struct CHO{ int sum; int val;}choose[65536];int S=0;int pow(int a,int n){ if(n==0) return 1; int b=a; while(--n) a*=b; return a;}bool cmp(CHO a,CHO b){ if(a.sum==b.sum) return a.val<b.val; else return a.sum<b.sum;}int main() { FILE *fin = fopen ("holstein.in", "r"); FILE *fout = fopen ("holstein.out", "w"); food need; int V,G,i,j,k; fscanf(fin,"%d",&V); for(i=0;i<V;i++) fscanf(fin,"%d",&need.vit[i]); fscanf(fin,"%d",&G); for(i=0;i<G;i++) { for(j=0;j<V;j++) fscanf(fin,"%d",&A[i].vit[j]); } for(i=1;i<pow(2,G);i++) { char C[20]; memset(C,'\0',sizeof(C)); binary(i,C); int len=strlen(C); food N; for(j=0;j<V;j++) N.vit[j]=0; for(j=0;j<len;j++) { if(C[len-j-1]=='1') { for(k=0;k<V;k++) N.vit[k]+=A[j].vit[k]; } } int flag=1; for(k=0;k<V;k++) if(N.vit[k]<need.vit[k]) { flag=0; break; } if(flag) { int hehe=0; for(k=0;k<len;k++) if(C[k]=='1') hehe++; choose[S++].sum=hehe; choose[S-1].val=i; } } sort(choose,choose+S,cmp); int CC[20]; int SS=0; char C[20]; memset(C,'\0',sizeof(C)); binary(choose[0].val,C); int len=strlen(C); for(i=len-1;i>=0;i--) { if(C[i]=='1') CC[SS++]=len-i; } //printf("%d",SS); fprintf(fout,"%d",SS); for(i=0;i<SS;i++) //printf(" %d",CC[i]); fprintf(fout," %d",CC[i]); fprintf(fout,"\n"); return 0;}
- USACO——Healthy Holsteins
- 【搜索】【USACO】Healthy Holsteins
- usaco training-Healthy Holsteins
- USACO Healthy Holsteins
- usaco Healthy Holsteins
- [USACO]Healthy Holsteins
- USACO:Healthy Holsteins
- USACO-Healthy Holsteins
- USACO Healthy Holsteins
- USACO 2.1 Healthy Holsteins
- USACO 2.1-Healthy Holsteins
- USACO 2.1 Healthy Holsteins
- usaco Healthy Holsteins
- USACO Healthy Holsteins
- usaco Healthy Holsteins
- USACO :Healthy Holsteins 解题报告
- USACO 2.1.5 Healthy Holsteins
- USACO 2.1.4Healthy Holsteins
- Web应用及XHTML基础篇
- mina简单编解码器示例
- linux学习(八) XShell上传、下载本地文件到linux服务器
- js监测ie678 浏览器
- 【ADO.NET】 概述以及connection与command
- USACO——Healthy Holsteins
- HDU1728 逃离迷宫
- 2006 求奇数的乘积
- 第一个设置a的bit3,第二个清除a的bit3
- 多态的预习
- 链接网址收藏
- Android的数据存储方式
- POJ 2240 Bellman算法判正权回路 floyd算法
- MySQl数据库备份与恢复