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

贴个JAVA二进制拆分AC代码,庆祝100题

Posted by WonderMouse at 2015-04-09 02:54:21 on Problem 1742
import java.io.*;
public class Main {
	static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static int nextInt() throws IOException
	{
		in.nextToken();
		return (int)in.nval;
	}

	public static void main(String[] args) throws IOException {
		PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
		int[] a=new int[101],c=new int[101];
		int n=nextInt(),m=nextInt();
		while(!(n==0&&m==0))
		{
			for(int i=1;i<=n;i++)
				a[i]=nextInt();
			for(int i=1;i<=n;i++)
				c[i]=nextInt();
			boolean[] dp=new boolean[m+1];
			dp[0]=true;int count=0;
			for(int i=1;i<=n;i++)
			{
				if(m<=a[i]*c[i]) 
					for(int j=a[i];j<=m;j++)  dp[j]|=dp[j-a[i]];
				else 
				{
					int amount=c[i],k=1;
					while(k<amount)
					{
						int v=k*a[i];
						for(int j=m;j>=v;j--)  dp[j]|=dp[j-k*a[i]];
						amount-=k;k<<=1;
					}
					int w=amount*a[i];
					for(int j=m;j>=w;j--)  dp[j]|=dp[j-w];
				}
			}
			for(int i=1;i<=m;i++)  if(dp[i]) count++;
			out.println(count);
			n=nextInt();m=nextInt();
		}
		out.flush();
	}
}
这题不用单调队列JAVA都能过,注意几个细节就好

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