题目:http://community.topcoder.com/stat?c=problem_statement&pm=10554&rd=15855

符合条件的集中非1的元素个数是非常少的,能够用回溯加剪枝。实际执行速度非常快。

代码:

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip> #include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std; #define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair
#define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) /*************** Program Begin **********************/ class Subsets {
public:
vector <int> numbers;
int ones_cnt;
int res;
int nextdiff[1005];
void backtrack(int sum, int prod, int pos)
{
// add
int cur = numbers[pos];
int next_sum = sum + cur;
int next_prod = prod * cur;
if (next_sum + ones_cnt > next_prod) {
res += next_sum + ones_cnt - next_prod;
if (pos + 1 < numbers.size()) {
backtrack(next_sum, next_prod, pos + 1);
}
} // not add
if (nextdiff[pos] < numbers.size()) {
backtrack(sum, prod, nextdiff[pos]);
}
} int findSubset(vector <int> numbers) {
sort(numbers.begin(), numbers.end());
this->numbers = numbers;
int n = numbers.size(); for (int i = 0; i < n; i++) {
nextdiff[i] = n;
for (int j = i + 1; j < n; j++) {
if (numbers[i] != numbers[j]) {
nextdiff[i] = j;
break;
}
}
} ones_cnt = count(numbers.begin(), numbers.end(), 1);
res = max(ones_cnt - 1, 0);
if (ones_cnt < n) {
backtrack(0, 1, ones_cnt);
} return res;
} }; /************** Program End ************************/

SRM 622 D2L3: Subsets, math, backtrack的更多相关文章

  1. SRM 621 D2L3: MixingColors, math

    题目:http://community.topcoder.com/stat? c=problem_statement&pm=10409&rd=15854 利用高斯消元求线性空间的基,也 ...

  2. SRM 620 D2L3: RandomGraph, dp

    称号:http://community.topcoder.com/stat? c=problem_statement&pm=13143&rd=15853 參考:http://apps. ...

  3. 90. Subsets II (Back-Track, DP)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  4. topcoder SRM 622 DIV2 FibonacciDiv2

    关于斐波那契数列,由于数据量比较小, 直接打表了,代码写的比较戳 #include <iostream> #include <vector> #include <algo ...

  5. topcoder SRM 622 DIV2 BoxesDiv2

    注意题目这句话,Once you have each type of candies in a box, you want to pack those boxes into larger boxes, ...

  6. SRM 624 D2L3: GameOfSegments, 博弈论,Sprague–Grundy theorem,Nimber

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=13204&rd=15857 这道题目须要用到博弈论中的经典理 ...

  7. SRM 628 D1L3:DoraemonPuzzleGame,math,后市展望,dp

    称号:c=problem_statement&pm=13283&rd=16009">http://community.topcoder.com/stat?c=probl ...

  8. 78. Subsets (Back-Track, DP)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  9. Cognition math based on Factor Space (2016.05)

    Cognition math based on Factor Space Wang P Z1, Ouyang H2, Zhong Y X3, He H C4 1Intelligence Enginee ...

随机推荐

  1. [转]rdlc报表中表达式的使用--switch和IIF范例

    本文转自:http://hi.baidu.com/oypx1234/item/5b35dec4e03a3ad697445266 =Switch( Fields!MLWHLO.Value = " ...

  2. 6.10---mybatis的实体---接口---接口映射---主配置文件

  3. Android中Button四种点击事件实现方式

    1.Xml添加监听属性,这里添加的doClick. <Button android:id="@+id/bt1" android:layout_width="wrap ...

  4. 使用Sql Server Management Studio 2008将数据导出到Sql文件中

      最近需要将一个Sql Server 2005数据库中的数据导出,为了方便,就希望能导出成Sql文件,里面包含的数据是由Insert 语句组成的. 在Sql Server Management St ...

  5. 【Oracle】to_char技巧

    Select to_char(sysdate,'ss') from dual;      取当前时间秒部分 Select to_char(sysdate,'mi') from dual;      取 ...

  6. dhtmlxtree动态加载节点数据的小随笔

    最近做了一个这个东西,颇有些感触,随笔记录一下自己的过程. 首先特别感谢:https://blog.csdn.net/cfl20121314/article/details/46852591,对我的帮 ...

  7. centos开机运行级别更改

    1.使用命令切换运行级别/目标 # systemctl isolate multi-user.target //切换到运行级别3,该命令对下次启动无影响,等价于telinit 3 # systemct ...

  8. C++版的LLC代码

    图像稀疏编码总结:LLC和SCSPM,文章对稀疏编码讲解非常详细. <Locality-constrained Linear Coding for Image Classification> ...

  9. ROS和OpenCV的对接cv_bridge

    做一个诚实的ROS教程搬运工............................. 官网链接:http://wiki.ros.org/cv_bridge 一.Package Summary Rel ...

  10. 使用replace pioneer批量修改文件名

    shell的正则表达式还是很难记忆的,也没有沉静的心情看文档,于是使用了replace pioneer. 1.  启动replace pioneer,Tools->batch runner  , ...