在过去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

源码:

 // 给定一个集合,挑选一些数字,验证能否组成 H

 #include <iostream>
#include <cstdio>
#include <algorithm> using namespace std; int arr[] = {}; // 输入的数组 bool isFound = false; void func(const int n, const int H, int &sum, int step)
{
if (isFound)
{
return;
} if (n > step && step >= && sum < H)
{
// 先取当前这个数进行累加
sum += arr[step]; if (sum == H)
{
isFound = true;
return;
}
else if (sum < H)
{
func(n, H, sum, step+);
} sum -= arr[step]; // back trace // 不取当前数字
func(n, H, sum, step+);
}
else
{
// 结束一次选择
//cout<<sum<<endl;
if (sum == H)
{
isFound = true;
}
} } // 降序处理
bool cmp(const int a, const int b)
{
return a > b;
} int main(void)
{
freopen("in.txt","r", stdin); int n = ;
int H = ;
unsigned long long sum = ; while( scanf("%d %d", &n, &H) != EOF )
{
for(int i=; i<n; ++i)
{
scanf("%d", &arr[i]);
sum += arr[i];
} if (sum < H)
{
printf("No\n"); //这是绝对不可能的。
continue;
}
else if (sum == H) // 刚刚好全选
{
printf("Yes\n");
continue;
}
// else sum > H // 进行遍历 // 进行分解
int sum = ;
int step = ;
isFound = false; sort(arr, arr+n, cmp); // 降序排序 func(n, H, sum, step); if (isFound)
{
printf("Yes\n");
}
else
{
printf("No\n");
} } return ;
}

将 子集和问题 运行时间从 200.8s 优化到 0.4s的更多相关文章

  1. K - Large Division 判断a是否是b的倍数。 a (-10^200 ≤ a ≤ 10^200) and b (|b| > 0, b fits into a 32 bit signed integer). 思路:取余;

    /** 题目:K - Large Division 链接:https://vjudge.net/contest/154246#problem/K 题意:判断a是否是b的倍数. a (-10^200 ≤ ...

  2. NYOJ-20 吝啬的国度 AC 分类: NYOJ 2014-01-23 12:18 200人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> #include<vector> using namespace std; int pre[1 ...

  3. APP被苹果APPStore拒绝的各种原因 分类: ios相关 app相关 2015-06-25 17:27 200人阅读 评论(0) 收藏

    APP被苹果APPStore拒绝的各种原因 1.程序有重大bug,程序不能启动,或者中途退出. 2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币. 3.游戏里有实物奖励的话,一定要说清楚,奖励 ...

  4. 008_AuditionCC系列1

    一.一二章编辑音频文件. (1)在音轨编辑页面,按鼠标滚轮或键盘上的+个-号可实现Zoom in(放大),Zoom out(缩小) (2)①次声波频率在<20Hz和>20000Hz之间②人 ...

  5. List 重载添加-add,删除-remove方法,以及获取子集方法

    package seday12; import java.util.ArrayList;import java.util.List; /*** @author xingsir* List重载了一对ad ...

  6. KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解

    KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解 哦淦我已经菜到被ABC吊打了. A - Century 首先把当前年 ...

  7. BZOJ 1688: Disease Manangement (子集枚举)

    Disease Manangement Q - 枚举子集 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  8. Python爬虫 - 爬取百度html代码前200行

    Python爬虫 - 爬取百度html代码前200行 - 改进版,  增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...

  9. BZOJ1688|二进制枚举子集| 状态压缩DP

    Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) ...

随机推荐

  1. caffe源代码分析--data_layer.cpp

    dataLayer作为整个网络的输入层, 数据从leveldb中取. leveldb的数据是通过图片转换过来的. 网络建立的时候. datalayer主要是负责设置一些參数,比方batchsize.c ...

  2. CF 444A(DZY Loves Physics-低密度脂蛋白诱导子图)

    A. DZY Loves Physics time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. springmvc 传递和接收数组参数

    java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...

  4. QT-Creator C/C++ 打地鼠小游戏

    废话少说先上图: 这个游戏纯属土鳖思路,没有用到什么游戏引擎. 1.使用按钮或QLabel铺满窗口. 2.通过简单算法随机动态的设置按钮矩阵中某个按钮的背景图像. 3.同步2过程反复设置多个按钮背景实 ...

  5. (一)使用Fragment实现QQ的底部按钮

    版权声明:本文出自郭霖的博客,转载必须注明出处. 转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragmen ...

  6. 蜗牛爱课 -- iOS 设置UIButton的字体的大小、显示位置、大小

    /设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 ...

  7. yum node.js

    很久之前安装过windows下以及Mac下的node,感觉还是很方便的,不成想今天安装linux下的坑了老半天,特此记录. 首先去官网下载代码,这里一定要注意安装分两种,一种是Source Code源 ...

  8. 轻松解决ubuntu系统引导问题

    什么是ppa PPA,表示 Personal Package Archives,也就是个人软件包集. 有很多软件因为种种原因,不能进入官方的 Ubuntu 软件仓库. 为了方便 Ubuntu 用户使用 ...

  9. mysql表结构设计

    允许NULL值的字段,数据库在进行比较操作时,会先判断其是否为NULL,非NULL时才进行值的必对.因此基于效率的考虑,所有字段均不能为空,即全部NOT NULL: 对于变长表,由于记录大小不同,在其 ...

  10. 【JQ学习笔记】提示的效果

    <p><a href="#" class="tooltip" title="这是我的超链接提示1.">提示1.< ...