SRM 574 D1L2:PolygonTraversal,dp
来源:互联网 发布:手机淘宝进旺信 编辑:程序博客网 时间:2024/05/18 08:49
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12477&rd=15494
参考:http://apps.topcoder.com/wiki/display/tc/SRM+574
与上一题一样,只是限制条件变成了18,不能再用暴力法了,必须使用dp。
注意位操作,之前在isIntersect()函数中把 if ( mask & (1 << i) ) 写成了 if (mask >> i) ,这个bug调了很久才调出来。
代码:
#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/*************** Program Begin **********************/long long dp[1 << 18][18];class PolygonTraversal {public:int N, start, end;inline int addPoint(int mask, int p){return ( mask | (1 << p) );}bool isIntersect(int mask, int p1, int p2){bool f1 = false, f2 = false;if (p1 > p2) {swap(p1, p2);}for (int i = 0; i < N; i++) {if ( mask & (1 << i) ) {if (i > p1 && i < p2) {f1 = true;} else if (i < p1 || i > p2) {f2 = true;}}}return f1 && f2;}long long rec(int mask, int p){long long & res = dp[mask][p];if (res != -1) {return res;}if (mask == end) {// base caseif (isIntersect(mask, p, start) ) {res = 1;return 1;} else {res = 0;return 0;}}res = 0;for (int i = 0; i < N; i++) {if ( !( mask & (1 << i) ) ) {// 若点未访问if (isIntersect(mask, p, i)) {// 若符合,加入该点res += rec( addPoint(mask, i), i );}}}return res;}long long count(int N, vector <int> points) {long long res = 0;this->N = N;end = (1 << N) - 1;start = points[0] - 1;int mask = 0;for (int i = 0; i < points.size(); i++) {--points[i];mask = addPoint(mask, points[i]);}memset(dp, -1, sizeof(dp));res = rec(mask, points[ points.size() - 1 ]);return res;}};/************** Program End ************************/
1 0
- SRM 574 D1L2:PolygonTraversal,dp
- SRM 629 D1L2: CandyCollection, dp, dfs
- SRM 615 D1L2: LongLongTripDiv1
- SRM 620 D1L2: CandidatesSelection, Greedy
- SRM 621 D1L2: TreesAnalysis,DFS
- SRM 571 D1L2:MagicMolecule,brute force
- SRM 573 D1L2:SkiResorts,最短路径算法
- SRM 572 D1L2:EllysBulls,Brute Force,meet in the middle
- SRM 626 D1L2:CatchTheBeat,Longest increasing subsequence,O(NlogN) 算法
- SRM 624 D1L2:DrivingPlans,求解所有的最短路径
- SRM 627 D1L2:GraphInversions,DFS,求所有指定长度的路径, Binary indexed tree (BIT)
- SRM 440 WickedTeacher (DP)
- srm 711 DerangementsDiv2(dp)
- SRM 574
- SRM 599 D2L3: SimilarNames2,dp
- SRM 609 D2L3: VocaloidsAndSongs,dp
- SRM 602 D1L1:TypoCoderDiv1,dp
- SRM 602 D2L3:BlackBoxDiv2,dp
- spring3.0 断网情况下 程序报错Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的
- 黑马程序员 Java基础学习笔记7 类
- 1827 tarjan+缩点
- 马云发内部信: 2014阿里巴巴ALL IN移动电商
- tomcat设置https双向协议
- SRM 574 D1L2:PolygonTraversal,dp
- 用System.Attribute扩展元数据
- visibility: hidden和display: none 有什么不同
- objective -c OC语法学习小结
- visio 2007 把ER图转换成sql脚本的解决方案
- 同一台机子搭建两个tomcat,共用confs配置
- android手机remount没有root权限问题
- spring MVC视图解析
- 并行与并发的区别