SRM 622 D2L3: Subsets, math, backtrack
来源:互联网 发布:手机淘宝多图变大代码 编辑:程序博客网 时间:2024/04/26 04:08
题目: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){// addint 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 addif (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 ************************/
0 0
- SRM 622 D2L3: Subsets, math, backtrack
- SRM 594 D2L3:PolygonTraversal2,backtrack
- SRM 617 D2L3: MyVeryLongCake, Math
- SRM 621 D2L3: MixingColors, math
- SRM 572 D2L3:DistinctRemainders,dp,math
- SRM 599 D2L3: SimilarNames2,dp
- SRM 609 D2L3: VocaloidsAndSongs,dp
- SRM 602 D2L3:BlackBoxDiv2,dp
- SRM 604 D2L3:FoxConnection2,dp
- SRM 591 D2L3:YetAnotherTwoTeamsProblem,dp
- SRM 610 D2L3:MiningGoldEasy,dp
- SRM 612 D2L3:PowersOfTwo,dp
- SRM 615 D2L3:MergeStrings,dp
- SRM 620 D2L3: RandomGraph, dp
- SRM 510 D2L3:TheLuckyBasesDivTwo,brute force,optimization
- SRM 571 D2L3:MagicMoleculeEasy,brute force
- SRM 626 D2L3:NegativeGraphDiv2,构造新图
- SRM 627 D2L3: BubbleSortWithReversals, dp, 冒泡排序
- ASP.NET MVC 过滤器(四)
- bzoj3401[Usaco2009 Mar]Look Up 仰望
- 在Centos上安装与配置Tomcat
- Spring缓存简单介绍(Spring Cache)
- uva 10795 - A Different Task (递归+状态转移)
- SRM 622 D2L3: Subsets, math, backtrack
- python的诗歌实战项目
- cocos2dx下应用RapidXml作ConfigService遇到的问题
- html第六章格式化之css
- org.springframework.beans.factory.BeanCreationException
- EditText 的一些软键盘属性和回调
- 目标
- Linux安装Tomcat外部不能访问
- 使用Event Bus模式解耦Android App组件间通信