北大acm 1011题,(java代码)老是超时求大神帮忙看下!!

zhangxm2015 2013-05-14 08:31:51

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
private static int part;
private static int max;
private static String[] ss;
private static int[] t;
private static boolean[] num;
private static int len;

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int sum = 0;
int n = Integer.parseInt(sc.nextLine());
if(n <= 0)
break;
String s = sc.nextLine();
ss = s.split(" ");
len = n;
num = new boolean[n];
t = new int[n];
for(int i = 0;i < ss.length;i++)
{
t[i] = Integer.parseInt(ss[i]);
sum += t[i];
}
Arrays.sort(t);
for(;n > 0;n--)
{
if(sum % n == 0)
{
part = n;
max = sum / part;
if(search())
{
System.out.println(max);
break;
}
}
}
}
}
public static boolean search()
{
int z = 0;
int sum = 0;
for(int k = len -1;k >= 0;k--)
{
int[] q = new int[len];
int i = 0;
if(t[k]>max)
return false;
if(t[k] == max)
{
num[k] = true;
z++;
continue;
}
if(num[k] == true)
continue;
sum = t[k];
for(int y = 0;y < k;y++)
{
if(num[y] == true)
{
continue;
}
sum += t[y];
q[i++] = y;
if(sum == max)
{
num[y] = true;
z++;
break;
}
else if(sum < max)
{
num[y] = true;
}
else
{
for(int p = 0;p < i;p++)
num[q[p]] = false;
i=0;
sum = t[k];
y--;
}
}
}
if(z == part)
return true;
else
return false;
}

}


总是超时,自己改了好久都没成功,求助啊!!
这里说一下我思路:将输入的木棍段数进行递减,寻找木棍原来的长度及总数;用嵌套的for循环进行优化处理,中间设置了一个数组q用来记录参与本次计算的木棍的值(除最大数即外城循环)以便对不符合的木棍进行状态还原,最后判断木棍的总数
...全文
286 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
groovy2007 2013-05-14
  • 打赏
  • 举报
回复
不好意思,忘了写main函数了,代码重贴一遍

public static void main(String[] args) {
    stickLen(new int[]{5,2,1,5,2,1,5,2,1});
    stickLen(new int[]{1,2,3,4});
}

static int stickLen(int[] parts) {
    int total = 0;
    int maxPart = 0;
    for(int x : parts) {
        total += x;
        if(x > maxPart) maxPart = x;
    }
    for(int target=maxPart; target<=total; target++) {
        if(total % target == 0) {
            int[] sticks = new int[total/target];
            if(partition(parts,sticks, target, 0)) {
                System.out.println("result "+target);
                return target;
            }
        }
    }
    return 0;
}


static boolean partition(int[] parts, int[] sticks, int target, int index) {
    System.out.println(Arrays.toString(parts)+" "
            +Arrays.toString(sticks)+" "+target+" "+index);
    if(sticks[index]==target) index++;
    if(index==sticks.length-1) {
        int temp = 0;
        for(int x : parts) temp += x;
        return temp==target;
    }
    if(sticks[0] == 0) {
        sticks[0] = parts[0];
        parts[0] = 0;
        return partition(parts, sticks, target, index);
    }
    
    for(int i=0; i<parts.length; i++) {
        if(parts[i]==0 || parts[i]+sticks[index] > target) continue;
        int temp = parts[i];
        parts[i] = 0;
        sticks[index] += temp;
        if(partition(parts,sticks,target,index)) return true;
        sticks[index] -= temp;
        parts[i] = temp;
    }
    return false;
}
groovy2007 2013-05-14
  • 打赏
  • 举报
回复
代码拿去,中间执行过程也会打印出来。还有优化的余地,对同样长度的片段归类处理。不过对于题目中的例子已经足够了。

static int stickLen(int[] parts) {
    int total = 0;
    int maxPart = 0;
    for(int x : parts) {
        total += x;
        if(x > maxPart) maxPart = x;
    }
    for(int target=maxPart; target<=total; target++) {
        if(total % target == 0) {
            int[] sticks = new int[total/target];
            if(partition(parts,sticks, target, 0)) {
                System.out.println("result "+target);
                return target;
            }
        }
    }
    return 0;
}


static boolean partition(int[] parts, int[] sticks, int target, int index) {
    System.out.println(Arrays.toString(parts)+" "+Arrays.toString(sticks)+" "+target+" "+index);
    if(sticks[index]==target) index++;
    if(index==sticks.length-1) {
        int temp = 0;
        for(int x : parts) temp += x;
        return temp==target;
    }
    if(sticks[0] == 0) {
        sticks[0] = parts[0];
        parts[0] = 0;
        return partition(parts, sticks, target, index);
    }
    
    for(int i=0; i<parts.length; i++) {
        if(parts[i]==0 || parts[i]+sticks[index] > target) continue;
        int temp = parts[i];
        parts[i] = 0;
        sticks[index] += temp;
        if(partition(parts,sticks,target,index)) return true;
        sticks[index] -= temp;
        parts[i] = temp;
    }
    return false;
}
执行结果 [5, 2, 1, 5, 2, 1, 5, 2, 1] [0, 0, 0, 0] 6 0 [0, 2, 1, 5, 2, 1, 5, 2, 1] [5, 0, 0, 0] 6 0 [0, 2, 0, 5, 2, 1, 5, 2, 1] [6, 0, 0, 0] 6 0 [0, 0, 0, 5, 2, 1, 5, 2, 1] [6, 2, 0, 0] 6 1 [0, 0, 0, 5, 0, 1, 5, 2, 1] [6, 4, 0, 0] 6 1 [0, 0, 0, 5, 0, 0, 5, 2, 1] [6, 5, 0, 0] 6 1 [0, 0, 0, 5, 0, 0, 5, 2, 0] [6, 6, 0, 0] 6 1 [0, 0, 0, 0, 0, 0, 5, 2, 0] [6, 6, 5, 0] 6 2 [0, 0, 0, 5, 0, 0, 0, 2, 0] [6, 6, 5, 0] 6 2 [0, 0, 0, 5, 0, 0, 5, 0, 0] [6, 6, 2, 0] 6 2 [0, 0, 0, 5, 0, 1, 5, 0, 1] [6, 6, 0, 0] 6 1 [0, 0, 0, 0, 0, 1, 5, 0, 1] [6, 6, 5, 0] 6 2 [0, 0, 0, 0, 0, 0, 5, 0, 1] [6, 6, 6, 0] 6 2 result 6 [1, 2, 3, 4] [0, 0] 5 0 [0, 2, 3, 4] [1, 0] 5 0 [0, 0, 3, 4] [3, 0] 5 0 [0, 2, 0, 4] [4, 0] 5 0 [0, 2, 3, 0] [5, 0] 5 0 result 5
zhangxm2015 2013-05-14
  • 打赏
  • 举报
回复
原题: Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero. Input The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero. Output The output should contains the smallest possible length of original sticks, one per line. Sample Input 9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0 Sample Output 6 5

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧