首页 > 专题系列 > Java解POJ > POJ 3104 Drying [解题报告] Java
2013
11-12

POJ 3104 Drying [解题报告] Java

Drying

问题描述 :

It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.

Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.

There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.

Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).

The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

输入:

The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).

输出:

Output a single integer — the minimal possible number of minutes required to dry all clothes.

样例输入:

sample input #1
3
2 3 9
5

sample input #2
3
2 3 6
5

样例输出:

sample output #1
3

sample output #2
2

解题代码:

//* @author: [email protected]
import java.io.*;
public class Main
{
	static int[] p;
	static int a,k;
	public static void main(String[] args) throws IOException
	{
		InputStreamReader is=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(is);
		a=Integer.parseInt(in.readLine());
		p=new int[a];
		String[] ss=in.readLine().split(" ");
		long total=0;
		for(int i=0;i< a;i++){
			p[i]=Integer.parseInt(ss[i]);
			total+=p[i];
		}
			
		k=Integer.parseInt(in.readLine());
		int v=k+a-1;
		long min=total/v;
		long high=total;
		while(high>min)
		{
			long mid=(high+min)/2;
			if(f(mid)) high=mid;
			else min=mid+1;
		}
		System.out.println(min);
	}
	static boolean f(long t)
	{
		long u=t;
		for(int i=0;i< a;i++)
		{
			if(p[i]<=t)continue;
			long sy=p[i]-t;
			u-=sy/(k-1);
			if(sy%(k-1)!=0)
				u--;
		}
		if(u< 0) return false;
		else return true;
	}
}

  1. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法

  2. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  4. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?

  5. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。