[TC SRM 685 div1 lev1] MultiplicationTable2
转载: https://www.linkedin.com/pulse/topcoder-685-multiplicationtable2-yingwu-zhu
Note: 生成封闭集合方式。
Problem
Fox Ciel is creating a new binary operation.
The operation will be denoted $ and it will be defined on the finite set S = {0, 1, 2, ..., n-1}. I.e., for each ordered pair (i, j) of elements of S the operation (i $ j) will return some element of S.
For example, we can have S = {0, 1}, and we can define that (0 $ 0) = 0, (0 $ 1) = 1, (1 $ 0) = 0, and (1 $ 1) = 0.
Note that Ciel's operation is not necessarily symmetric. In other words, it is possible that for some i and j the operations (i $ j) and (j $ i) return two different values.
A nice concise description of the operation $ is its "multiplication table": a square table where in row i and column j we have the value (i $ j). You are given this "multiplication table" encoded as a int[] table with n^2 elements. For each valid i and j the operation (i $ j) returns the value table[i*n+j].
A subset T of S is called good if it has the following property: for any two elements i and j in T, (i $ j) is also in T.
Find and return the minimal size of a good subset of S. Note that the answer is always defined, as there always are some good subsets. For example, the entire set S is always good.
Constraints:
-- n will be between 1 and 50, inclusive
-- table will contain exactly n*n elements
-- Each element in table will be between 0 and n-1, inclusive
My Solution:
Picture the final solution
Suppose we have found the final solution S. What property does S have?
Yes. All elements in S must be unique in [0, n-1]. More importantly, we can have some element as the smallest element!
So, we can brute force all possible solutions with each element in [0, n-1] as the smallest element in their final solutions. Then take the minimum of all.
Solve a sub-problem
Let a solution S with i as the smallest element.
(1) if i $ i = i, then we are done
otherwise
(2) if i $ i = j, then
(2.1) if j < i, no solution.
Otherwise
(2.2) Then we can use the new element j to find other elements in S.
In implementation, we can use set<int> to maintain the newly found elements, a vector<int> to maintain S. The complexity is O(n^2).
Putting all together
We iterate all i in [0, n-1] and solve each subproblems as described above. We keep the minimum set size. The time complexity = O(n^3).
Example Code:
1 using namespace std; 2 #define REP(i,a,b) for (int i = int(a); i < int(b); i++) 3 int sol2() { 4 int n = (int)sqrt(sizeof(tab) / sizeof(int)); 5 int ret = n; 6 bool flag[n]; 7 REP(i, 0, n) { 8 memset(flag, false, sizeof(flag)); 9 set<int> S; 10 S.insert(i); 11 vector<int> A; 12 bool ok = true; 13 while (!S.empty() && ok && A.size() < ret) { 14 int x = *S.begin(); 15 flag[x] = true; 16 A.push_back(x); 17 S.erase(S.begin()); 18 REP(j, 0, A.size()) { 19 int y = tab[A[j] * n + x]; 20 if (y < i) { 21 ok = false; break; 22 } 23 if (!flag[y]) 24 S.insert(y); 25 y = tab[x*n + A[j]]; 26 if (y < i) { 27 ok = false; break; 28 } 29 if (!flag[y]) 30 S.insert(y); 31 } 32 } 33 if (ok) 34 ret = min(ret, (int)A.size()); 35 } 36 return ret; 37 }
转载于:https://www.cnblogs.com/ivancjw/p/6390040.html
[TC SRM 685 div1 lev1] MultiplicationTable2相关推荐
- TC SRM 655 Div1 Level 3 题解
TC SRM 655 Div1 Level 3 题解 题目传送门 dp+几何 首先可以发现凸包是顺时针排列的一些线段构成的.我们可以预处理每一个蓝点是否都在一个线段 r [ i ] → r [ j ] ...
- 【TC SRM 312 Div1 Level3】CheapestIsland(轮廓线DP)(最小表示法)
传送门 随便抓了一道题来复习轮廓线DP. 听说和[JLOI2009 神秘的生物]本质上是一样的,没看,不清楚. 题解: 给一个棋盘图,每个格子有权值,求权值之和最小的连通块. 没什么难度,熟悉轮廓线的 ...
- SRM 583 DIV1
A 裸最短路. 1 class TravelOnMars { 2 public: 3 int minTimes(vector <int>, int, int); 4 }; 5 vector ...
- TC(SRM)和CF入门教程for ACMer[转载]
一.TC基本介绍TC的网址www.topcoder.com/tc,我们一般提到TC的时候是特指其中的Single Round Match(SRM).SRM的规则总结起来就是一句话:75分钟做完3道难度 ...
- TC(SRM)和CF入门教程for ACMer
一.TC基本介绍 TC的网址www.topcoder.com/tc,我们一般提到TC的时候是特指其中的Single Round Match(SRM). SRM的规则总结起来就是一句话:75分钟做完3道 ...
- TC SRM 573
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 又来水一发.... 结果:过了250,challe ...
- SRM 591 div1 275
topcoder被Appirio收购了 好久没做tc,这个题目挺简单.就是Arena里面看不到图片,只能去tc网站上找题目.http://community.topcoder.com/stat?c= ...
- SRM 590 DIV1
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 水水更健康,终于回到1800+了... DIV2 ...
- topcoder srm 691 div1 -3
1.给定一个$n$个顶点$n$个边的图,边是$(i,a_{i})$,顶点编号$[0,n-1]$.增加一个顶点$n$,现在选出一个顶点集$M$,对于任意的在$M$中 的顶点$x$,去掉边$(x,a_{x ...
最新文章
- NLTK命名实体识别NER
- 倒计时3天!华为畅想未来智能车大赛报名即将截止,已报名选手请提交参赛PPT!
- Navicat连接Oracle的几个问题及解决方案
- C语言:一个数组中只有两个数字是出现一次
- Web网站架构演变—高并发、大数据
- mysql索引图文操作_图文并茂,说说MySQL索引
- Python 中的 if __name__ == '__main__' 该如何理解
- STL常用对象,不会搞得C++跟没学一样
- 深度模型不work?这有一份超全的Debug检查清单
- 我最喜欢的模板jade(pug)学习和使用
- 华为智慧屏鸿蒙系统怎么样,鸿蒙系统初体验,华为智慧屏V65到底值不值得入手?...
- python制作简单文本编辑器
- 【优化调度】基于NSGAII算法的车辆充电调度策略研究含Matlab代码
- 为NodePad++添加CSS格式化功能
- Designing an Encoder for StyleGAN Image Manipulation论文解读
- 51nod1463 找朋友
- 手机收不到验证码问题
- js之捕捉冒泡和事件委托
- 3 分钟搞瘫阿里内网,他是唯一能让马云睡安稳的男人!
- Windows 10 自带App无法使用,打开闪退
热门文章
- php的array_walk,PHP array_walk() 函数详解
- linux自动挂载usb打印机,Linux下使用Usbmount实现USB设备自动挂载
- js java post提交_如何从Express.js发送Post请求到另一个服务器(Java)?
- html读取servlet,简单html与servlet交互(HTML利用servlet读取txt)
- 计算机展望未来网络形态,在学习中展望未来
- 华硕和梅林系统哪个好_RUSHCRM:定制CRM软件系统哪个好?
- 基于java(springboot+mybatis)汽车信息管理系统设计和实现以及文档
- 以30字符宽居中输出python字符串_Python3 字符串
- linux dhcp 负载均衡,dhcp双机负载均衡
- php里面没有mssql,为什么没有正确使用PHP / MSSQL的日期/时间?