USACO 2.1 - Healthy Holsteins | Jack Neus


USACO 2.1 – Healthy Holsteins | Jack Neus
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.
http://www.cnblogs.com/liushang0419/archive/2012/06/08/2541426.html
农民JOHN以拥有世界上最健康的奶牛为傲。他知道每种饲料中所包含的牛所需的最低的维他命量是多少。请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少。
给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少。
维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解。

格式

PROGRAM NAME: holstein
INPUT FORMAT:(file holstein.in)
第1行:一个整数V(1<=V<=25),表示需要的维他命的种类数。
第2行:V个整数(1<=每个数<=1000),表示牛每天需要的每种维他命的最小量。
第3行:一个整数G(1<=G<=15),表示可用来喂牛的饲料的种数。
下面G行,第n行表示编号为n饲料包含的各种维他命的量的多少。
OUTPUT FORMAT:(file holstein.out)
输出文件只有一行,包括
牛必需的最小的饲料种数P
后面有P个数,表示所选择的饲料编号(按从小到大排列)。
如果有多个解,输出饲料序号最小的(即字典序最小)。

SAMPLE INPUT

4
100 200 300 400
3
50 50 50 50
200 300 200 300
900 150 389 399

SAMPLE OUTPUT

2 1 3

分析:
这个题目是找一个最优方案,由于数据比较小,直接枚举是直接可以过掉的,但是在枚举的实现上有两种思路。
第一种是按照乘法原理的思路,一共15种药,每一种要么选择,要么不选择,所以一共枚举次数的计算表达式为 2^15 这么多种。
第二种是按照组合原理的思路,一共15种药物,我分15次逐层深入遍历,需要的枚举次数最多为 C(15,1)+C(15,2).。。。+C(15,15).其值和2^15是一样的。

这两种方法都能通过代码实现,而且很容易,但是在效率上有比较大的区别,打个比方,假设一共只有三种药物,
那么按照第一种思路,枚举顺序为 001,010,011,100,101,110,111 (0,代表不选择,1代表选择,比如001即表示,只选择第三种药物)
按照第二种思路 ,枚举序列为 1,2,3,12,13,23,123 (可以发现1,2,3,属于C(3,1),12,13,23 属于C(3,2),123则属于C(3,3))

如果说那种思路好的话,那么根据这道题目的具体情况,肯定是第二种要好,因为本题对输出上有要求“如果有多个解,输出饲料序号最小的(即字典序最小)。”,可以发现第二种是正好按照题目要求的顺序来的,第一种思路的输出虽然也是“字典序”,但是他不符合“药物总数”从小达到大的输出要求,比如011在100前面,但是011表示两种药物,100表示一种药物。

我的代码是第二种思路,逐层生成组合序列,虽然每一层用的是DFS,但是整体上有点BSF的特点,因为只要找到就输出。后面的曾就不用搜索了。
x. DFS
https://github.com/TomConerly/Competition-Programming/blob/master/USACO/Chapter2/holstein.java
http://acm.sdibt.edu.cn/blog/?p=1035

public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("holstein.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("holstein.out")));
V = sc.nextInt();
min = new int[V];
for(int i = 0; i < V;i++)
{
min[i] = sc.nextInt();
}
G = sc.nextInt();
scoop = new int[G][V];
for(int i = 0; i < G;i++)
{
for(int j = 0; j < V;j++)
{
scoop[i][j]= sc.nextInt();
}
}
best = null;
bestC = Integer.MAX_VALUE;
recur(0,new boolean[G],0,new int[V]);
ArrayList<Integer> ans = new ArrayList<Integer>();
for(int i = 0; i < G;i++)
{
if(best[i]) ans.add(i+1);
}
Collections.sort(ans);
out.print(ans.size());
for(int a: ans)
out.print(" "+a);
out.println();
out.close();
}
static int[] min;
static int[][] scoop;
static int V,G;
static boolean[] best;
static int bestC;
public static void recur(int at, boolean[] used,int count,int[] vit)
{
if(at == used.length)
{
for(int i = 0; i < V;i++)
{
if(vit[i] < min[i]) return;
}
if(better(count,used))
{
bestC = count;
best = used.clone();
}
return;
}
recur(at+1,used,count,vit);
for(int i = 0; i < V;i++){
vit[i] += scoop[at][i];
}
used[at] = true;
recur(at+1,used,count+1,vit);
for(int i = 0; i < V;i++){
vit[i] -= scoop[at][i];
}
used[at] = false;
}
public static boolean better(int count, boolean[] used)
{
if(count < bestC) return true;
if(count > bestC) return false;
for(int i = 0; i < G;i++)
{
if(used[i] && !best[i]) return true;
if(!used[i] && best[i]) return false;
}
return false;
}
http://lujunda.cn/2012/08/02/usacosection-2-1%E6%90%9C%E7%B4%A2-healthy-holsteins/

int V,G,v[25],g[15][25],total;
//total表示最优方案所选择的饲料的数量。
bool mark[25];
//对选择的饲料进行标记。
void input()
//输入
{
    scanf("%d",&V);
    for(int i=0;i<V;i++)
    {
        scanf("%d",&v[i]);
        mark[i]=true;
        //默认选择全部饲料。
    }
    scanf("%d",&G);
    for(int i=0;i<G;i++)
        for(int j=0;j<V;j++)
            scanf("%d",&g[i][j]);
    total=G;
    //默认选择全部饲料。
}
void output()
//输出结果
{
    printf("%d",total);
    for(int i=0,j=0;j!=total;i++)
    //输出前total个被标记的饲料。
        if(mark[i])
        {
            printf(" %d",i+1);
            j++;
        }
    printf("\n");
}
void updata(int n,int t)
//根据选择或除去第n个饲料来更新v[]。
//选择或除去由参数t决定。
{
    for(int i=0;i<V;i++)
        v[i]+=g[n][i]*t;
}
bool check()
//检查v[]的所有元素是否都不大于0。
{
    for(int i=0;i<V;i++)
        if(v[i]>0)
            return false;
    return true;
}
bool dfs(int n,int s)
//按饲料种类进行深搜。
//参数n表示第n个饲料。
//参数s表示已选择的饲料数量。
{
    if(s==total)
    //若选择的饲料数量不小于已有最优方案的选择数量。
        return false;
    if(check())
    //若有更优方案。
    {
        total=s;
        return true;
    }
    if(n==G)
    //若已没有更多饲料可供选择。
        return false;
    bool check_temp=false;
    updata(n,-1);
    //使用当前饲料。
    if(dfs(n+1,s+1))
    {
        check_temp=true;
        mark[n]=true;
        //对第n个饲料进行标记。
    }
    updata(n,1);
    //不使用当前饲料。
    if(dfs(n+1,s))
    {
        check_temp=true;
        mark[n]=false;
        //取消第n个饲料的标记。
    }
    return check_temp;
}

X. 二进制枚举 http://simone-chou.iteye.com/blog/2015115
   直接用二进制枚举,0代表没选上,1代表选上。最多也就选择15种,故从从 1 枚举到pow(2,n)判断即可,加上剪枝。DFS会爆栈。

X. BFS
http://www.conlan.cc/2011/10/26/usaco-2-14-healthy-holsteins%EF%BC%88bfs/
Read full article from USACO 2.1 – Healthy Holsteins | Jack Neus

Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts