Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这题怎么做啊?网上有代码也看不懂。

Posted by yygy at 2014-06-29 21:08:08 on Problem 3677
http://www.acmerblog.com/POJ-3677-Chemical-Weighing-blog-1129.html

//* @author 
import java.util.*;
import java.io.*;

class Main
{
 static int armor[];
 final static int MAX = 1000000;
 static class Node
 {
	Node l = null, r = null;
	int flag, step, value;
	Node(int k, int v)
	{
         flag = 0;
	 step = MAX;
	 value = v;
	 if(k>=0&&v<=30)
	 {
		l = new Node(k, v+armor[k]);
		r = new Node(k-1, v);
	 }
	}
	

  int update(int f, int v, int s)
  {
	if(value>v||l==null&&r==null)return MAX;
	if(f==flag)step = Math.min(s, step);
	else step = s;
	s = step;
	flag = f+1;
	//System.err.println(value+":"+s);
	if(value==v)return s;
	return step = Math.min(l.update(f, v, s+1)+1, r.update(f, v, s));
  }
  int query(int v)
  {
	if(l==null&&r==null)return MAX;
	if(value==v)return step;
	return Math.min(l.query(v), r.query(v));
  }
 }

 public static void main(String[] args) throws IOException
 {
  Scanner in = new Scanner(System.in);
  for(int T = in.nextInt(); T!=0; --T)
  {
	int n = in.nextInt();
	armor = new int[n];
	for(int i=0; i!=n; ++i)
		armor[i] = in.nextInt();
	Arrays.sort(armor);
	int cnt=0;
	Node root = new Node(n-1, 0);
	root.step = 0;
	int k = 0, m = in.nextInt();
	for(int i=0; i!=m; ++i)
		root.update(cnt++, k=in.nextInt(), MAX);
	k = root.query(k);
	if(k==MAX)System.out.println(-1);
	else System.out.println(k);
    }
  }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator