将 子集和问题 运行时间从 200.8s 优化到 0.4s

 

在过去24小时里,一直被这题折腾着。。。

 

题目:
A Math game
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 256000/128000KB (Java/Others)

Problem Description

Recently, Losanto find an interesting Math game. The rule is simple: Tell you a number H, and you can choose some numbers from a set {a[1],a[2],......,a[n]}.If the sum of the number you choose is H, then you win. Losanto just want to know whether he can win the game.

Input

There are several cases.
In each case, there are two numbers in the first line n (the size of the set) and H. The second line has n numbers {a[1],a[2],......,a[n]}.0<n<=40, 0<=H<10^9, 0<=a[i]<10^9,All the numbers are integers.

Output

If Losanto could win the game, output "Yes" in a line. Else output "No" in a line.

 

Sample Input

10 87
2 3 4 5 7 9 10 11 12 13
10 38
2 3 4 5 7 9 10 11 12 13

Sample Output

No
Yes

 

简述之就是子集和问题。

 

AC 之后,发现原来写代码的过程如此有趣。。。。

 

solution 1  直接DFS  运行时间  200.8s

针对数组中的每个元素,要么选中,要么不选。 结果是  Time Limit Exceeded (指超过题目要求的 1000MS)

遍历过程类似于二叉树,复杂度是指数级, 2^n  

 

Solution 2 加点剪枝处理  运行时间 166.7 s

在sulotion 1 的基础上,做个判断,一旦累加的记过超过 H,则不进行后面的累加。

结果仍然 Time Limit Exceeded。 感觉没有根本性的变化! 

(如果超过H的话,就不往下走了,相比第一个方案,效率有了一丢丢提升, 笔记本风扇依然狂转)

 

Solution 3 对输入进行预处理  运行时间 4.1 s

因为输入的数据是比较小的,所以进行排序的时间消耗非常小,但是效率的提升非常明显!  

前两个方案,我等了很久,都没有出现结果。 但是排序之后,等几秒钟就看到结果了。哇,那个激动啊。。。

我画了个图,发现一旦数据是排序的,那么剪枝的效果将会非常明显,所以这导致了效率的大幅提升 

虽然有所提升,但是提交之后,依然  Time Limit Exceeded (此后经过1个多小时的修改测试,还是超时) 深夜了,休息。。。

 

新的一天, 起床,继续 coding~~~

 

 Solution 4 对遍历方向进行处理  运行时间  3.4 s

可能是思维惯性,老想着从无到有进行遍历,所以总是往不选的方向遍历,导致浪费很多无用的时间。

所有修改搜索顺序,往选中的方向进行遍历。

 

 结果超时! 仔细思考之后,发现剪枝的效果不明显,原因在于我是按升序排序的,应该改为 降序排序

 

Solution 5  AC! 运行时间 0.4s

进行降序排序,明显提高剪枝效率,因为一旦数据超过H, 就没必要往下走了。因为是往累加的方向进行,会更快逼近结果。

至此,刚好过去了24小时,把这题的运行时间 从 200.8s 优化到了 0.4s .  总结之:就是DFS遍历应该以最优的方向进行遍历,同时进行剪枝处理

 

想想真的有些感动,原来自己可以如此专注。

编程要对自己有信心。 昨天是个美丽的早晨,早早起来后就开始做网络同步赛,结果一个早上,就因为这题,一直纠结着。昨晚洗澡的时候,心里就想,都有好几个人做出来了,我也一定能够做出来,睡觉的时候,也在想着,明早起来再试试,肯定可以搞出来。

上午搞不定,下午继续搞,下午搞不定,晚上继续搞,晚上搞不定,明天接着搞~~~   WA 不息,Coding 不止。

 

最后附上测试数据和源码

40 256
2 3 4 5 7 9 10 11 12 13 2 3 4 5 7 9 10 11 12 13 2 3 4 5 7 9 10 11 12 13 2 3 4 5 7 9 10 11 12 13

源码:

  1 // 给定一个集合,挑选一些数字,验证能否组成 H
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 
  7 using namespace std;
  8 
  9 int arr[50] = {0};      // 输入的数组
 10 
 11 bool isFound = false;
 12 
 13 void func(const int n, const int H, int &sum, int step)
 14 {
 15     if (isFound)
 16     {
 17         return;
 18     }
 19 
 20     if (n > step && step >= 0 && sum < H)
 21     {
 22         // 先取当前这个数进行累加
 23         sum += arr[step];
 24 
 25         if (sum == H)
 26         {
 27             isFound = true;
 28             return;
 29         }
 30         else if (sum < H)
 31         {
 32             func(n, H, sum, step+1);
 33         }
 34 
 35         sum -= arr[step];       // back trace
 36 
 37         // 不取当前数字
 38         func(n, H, sum, step+1);
 39     }
 40     else
 41     {
 42         // 结束一次选择
 43         //cout<<sum<<endl;
 44         if (sum == H)
 45         {
 46             isFound = true;
 47         }
 48     }
 49 
 50 }
 51 
 52 // 降序处理
 53 bool cmp(const int a, const int b)
 54 {
 55     return a > b;
 56 }
 57 
 58 int main(void)
 59 {
 60     freopen("in.txt","r", stdin);
 61 
 62     int n = 0;
 63     int H = 0;
 64     unsigned long long sum = 0;
 65 
 66     while( scanf("%d %d", &n, &H) != EOF )
 67     {
 68         for(int i=0; i<n; ++i)
 69         {
 70             scanf("%d", &arr[i]);
 71             sum += arr[i];
 72         }
 73 
 74         if (sum < H)
 75         {
 76             printf("No\n");     //这是绝对不可能的。
 77             continue;
 78         }
 79         else if (sum == H)      // 刚刚好全选
 80         {
 81             printf("Yes\n");
 82             continue;
 83         }
 84         // else  sum > H        // 进行遍历
 85 
 86         // 进行分解
 87         int sum = 0;
 88         int step = 0;
 89         isFound = false;
 90 
 91         sort(arr, arr+n, cmp);   // 降序排序
 92 
 93         func(n, H, sum, step);
 94 
 95         if (isFound)
 96         {
 97             printf("Yes\n");
 98         }
 99         else
100         {
101             printf("No\n");
102         }
103 
104     }
105 
106     return 0;
107 }

 

posted @ 2015-05-10 10:03  LV8  阅读(385)  评论(0编辑  收藏  举报