在过去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. magent编译安装及常见错误

    安装magent到/usr/local/下 cd /usr/local mkdir magent cd magent/ wget http://memagent.googlecode.com/file ...

  2. 自定义按照index和key访问的List

    List<T>用起来比较方便,但是有时候要按照Index来访问List中的对象有些繁琐,所以想是不是扩展一下,既能按照Index来访问,又能按照Key访问. 实现方法: public cl ...

  3. 纯CSS绘制三角形

    扒segmentfault的导航栏时候发现的,用了个span标签写了个三角形出来,第一次发现,好神奇,查了下还有挺多种玩法的.基本的用法就是将盒子的width和height设为0,然后用border搭 ...

  4. WebView的基本使用

    ---恢复内容开始--- 一.实例化WebView 通过xml实例化 xml <WebView android:id="@+id/webview" android:layou ...

  5. HTML 5 JavaScript初步 编译运行.doc

    编译运行 解释运行 JavaScript:只有一种变量类型,var.数据类型:整型,小数,字符串,布尔型 1.如何把数值型字符串变成数字型: parseInt("字符串")——把字 ...

  6. Java与C++相异的地方

    继承标识:Java使用extends/implement,C++使用: super:调用父类的某些东西 instanceof:RTTI机制(A is instanceif B) final:类似于C+ ...

  7. Normalize.css 介绍与源码解读

    开始 Normalize.css 是一个可定制的 CSS 文件,使浏览器呈现的所有元素,更一致和符合现代标准;是在现代浏览器环境下对于CSS reset的替代. 它正是针对只需要统一的元素样式.该项目 ...

  8. 前端开发面试题收集 HTML

    Doctype是什么?说明特点. <!doctype>声明必须处于HTML文档的头部,在标签之前. <!doctype>声明不是一个HTML标签,是一个用于告诉浏览器使用哪种H ...

  9. win8 VS2010 配制OpenGL

    glut下载地址: http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip glut.h  ---> C:\Progr ...

  10. Android——仿QQ聊天撒花特效

    实现这样的效果,你要知道贝塞尔曲线,何谓贝塞尔曲线?其实就是曲线,嘿嘿,关于曲线的概念大家可以去 Android绘图机制(二)——自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和 ...