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 ...
随机推荐
- Laravel5.1 学习笔记2, 路由
安装的说明请看文档, laravel 安装 #基本路由 你将在 app/Http/routes.php 文件定义大部分路由, 这些路由将被App\Providers\RouteServiceProvi ...
- 课上练习 script
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Oracle11g自带的SQL developer无法打开解决
在安装完Oracle Database 11g Release 2数据库,想试一下Oracle自带的SQL Developer工具,在操作系统菜单的所有程序中找到SQL Developer如下所示,并 ...
- XML知识总结
1.XML概念及作用? XML( eXtensible Markup Language,可扩展标记语言)是一种简单的数据存储语言 作用:用来存储和交换数据 无法描述页面的排版和显示形式 2.XML和X ...
- EF MySql:Specified key was too long; max key length is 767 bytes解决方案
[DbConfigurationType(typeof(MySqlEFConfiguration))]//添加特性 public partial class Model1 : DbContext { ...
- url 传参数时出现中文乱码
1.前端通过 url 传递参数,但是参数又有中文,在下一个页面接受参数的时候中文会乱码 解决方案为: 定义和用法 decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解 ...
- 09 Django组件之用户认证组件
没有学习Django认证组件之前使用装饰器方法 from django.shortcuts import render, HttpResponse, redirect from app01.MyFor ...
- python tips:最内嵌套作用域规则,闭包与装饰器
在作用域与名字空间提到,python是静态作用域,变量定义的位置决定了变量作用的范围.变量沿着local,global,builtins的路径搜索,直觉上就是从里到外搜索变量,这称为最内嵌套作用域规则 ...
- RabbitMQ出现服务启动几秒退出问题
最近在学习rebbitmq, 1.首先安装了otp_win64_20.3, 2.erlang安装完成需要配置erlang环境变量: 这个是新建的 文档是:ERLANG_HOME D:\develop\ ...
- 09.正则表达式re-1.正则表达式
1.正则表达式概述 正则表达式(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念. 正则表达式使用单个字符串来描述.匹配一系列匹配某 ...