SRM 622 D2L3: Subsets, math, backtrack
题目: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的更多相关文章
- SRM 621 D2L3: MixingColors, math
题目:http://community.topcoder.com/stat? c=problem_statement&pm=10409&rd=15854 利用高斯消元求线性空间的基,也 ...
- SRM 620 D2L3: RandomGraph, dp
称号:http://community.topcoder.com/stat? c=problem_statement&pm=13143&rd=15853 參考:http://apps. ...
- 90. Subsets II (Back-Track, DP)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- topcoder SRM 622 DIV2 FibonacciDiv2
关于斐波那契数列,由于数据量比较小, 直接打表了,代码写的比较戳 #include <iostream> #include <vector> #include <algo ...
- topcoder SRM 622 DIV2 BoxesDiv2
注意题目这句话,Once you have each type of candies in a box, you want to pack those boxes into larger boxes, ...
- SRM 624 D2L3: GameOfSegments, 博弈论,Sprague–Grundy theorem,Nimber
题目:http://community.topcoder.com/stat?c=problem_statement&pm=13204&rd=15857 这道题目须要用到博弈论中的经典理 ...
- SRM 628 D1L3:DoraemonPuzzleGame,math,后市展望,dp
称号:c=problem_statement&pm=13283&rd=16009">http://community.topcoder.com/stat?c=probl ...
- 78. Subsets (Back-Track, DP)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- 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 ...
随机推荐
- vs2008bin下Debug bll Release文件 obj下的Debug bll Release文件区别
Bin目录用来存放编译的结果,bin是二进制binrary的英文缩写,因为最初C编译的程序文件都是二进制文件,它有Debug和Release两个版本,分别对应的文件夹为bin/Debug和bin/Re ...
- bootstrap的栅格系统和响应式工具
关于bootstrap的响应式布局,昨天看了杨老师的视频教学https://www.bilibili.com/video/av18357039豁然开朗,在这里记录一下 一:meta标签的引用 < ...
- bcg库使用心得两则
作者:朱金灿 来源:http://blog.csdn.net/clever101 最近帮同事解决了两个BCG库的使用问题,特记录下来. 一是在outlook风格停靠栏上创建对话框的做法.代码如下: C ...
- oracle 命令记录
监听程序启动停止查看名利: 1.切换到oracle用户:su - oracle 2.查看监听状态:lsnrctl status 3.停止监听:lsnrctl stop 4.启动监听:lsnrctl s ...
- JNDI连接池连接Oracle数据库
今天做了一个评论的小功能,要求用JNDI连接池连接Oracle数据库,以前只是测试了是否连接的上,现在没想到一个JNDI连接池连接Oracle数据库,纠结了好久,原来都是Oracle数据库的问题,这是 ...
- (转)Openlayers 2.X加载高德地图
http://blog.csdn.net/gisshixisheng/article/details/44853881 概述: 前面的有篇文章介绍了Openlayers 2.X下加载天地图,本节介绍O ...
- jsonp的作用
jsonp(即JSON with padding),也就是json填充. 背景: json格式的数据对比xml格式的数据,性能上已经有了很大的提升.但是json可以被本地执行仍然会导致几个重要的性能问 ...
- day11 前端知识简单总结
目录 1.html常用标签 2.css布局 一. html 常用标签 1.head里面的标签,仅仅应用于网页的一些基础信息 1.1 meta 属性http-equiv 向浏览器传达一些有用的信息 与 ...
- shell 结合expect实现ssh登录并执行命令
#!/bin/bash ips=( '127.0.0.1' ) ;i<${#ips[*]};i++)) do expect <<EOF #这里的 expect <<EOF ...
- 爬虫系列(十二) selenium的基本使用
一.selenium 简介 随着网络技术的发展,目前大部分网站都采用动态加载技术,常见的有 JavaScript 动态渲染和 Ajax 动态加载 对于爬取这些网站,一般有两种思路: 分析 Ajax 请 ...