[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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/572752.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php树菜单转化为一维菜单,php树型菜单 - iturtle的个人空间 - OSCHINA - 中文开源技术交流社区...

通过处理array,添加level,is_leaf属性实现 树型菜单公共类通过处理array,添加level,is_leaf属性实现 树型菜单/*树形分类添加节点属性*$data, 要处理的节点数组*$pid,父类ID*$prefix, 字段前缀 如chh_pid*$tree array()**/function getTree($data, $pid,$prefix,$tree) {for…

C#-WebForm-★★★JQuery知识——基础知识、选择器、事件★★★

JQuery 与 JS 之间的转换 将JQuery转换为JS —— get(0)  例如&#xff1a;alert( $("#d1").get(0).offsetwidth ); 将JS 转换为JQuery —— $(" ")  例如&#xff1a;$("#d1").click(function(){}); 1、什么是JQuery&#xff1f;   它就是…

Java编号姓名元宝数密码,通过my Eclipse控制台向数据库(SQL2008)中查找、删除、插入信息...

通过my Eclipse控制台向数据库(SQL2008)中查找、删除、插入信息如果编译程序有什么错误还望大家多多指正代码执行所需数据库、架包及java源文件已上传至文件 文件名 SQl_JDBC.zip用my Eclipse控制台操作数据库之前(SQL 2008)之前 应先引入一个架包(sqljdbc4.jar) 在架包导入之后…

matlab 空矩阵耗时,性能 – 更快的方式通过空矩阵乘法初始化数组? (Matlab)

这很奇怪&#xff0c;我看到f更快&#xff0c;而g比你看到的慢。但是他们对我来说都是一样的。也许不同版本的MATLAB&#xff1f;>> g () zeros(1000, 0) * zeros(0, 1000);>> f () zeros(1000)f ()zeros(1000)>> timeit(f)ans 8.5019e-04>> timeit(…

事件对象及其属性

$(function(){$(input).bind(click,function(e){alert(e); //获取对象//event对象的属性alert(e.type);alert(e.target);alert(e.currentTarget);//得到监听元素的DOM&#xff0c;target是点的那个的DOM});$(input).bind(mouseover,function(e){alert(e.relatedTarget);});$(in…

百度网盘php文件怎么打开,如何通过网页版百度网盘下载大文件

最近老张需要在百度网盘下载一个较大的文件&#xff0c;结果他要我必须下载客户端才行&#xff0c;此背景下&#xff0c;在网站找了各种办法&#xff0c;什么模拟手机&#xff0c;F12查看下载链接都不行&#xff0c;最后找到了可行的办法。步骤如下&#xff1a;一&#xff1a;如…

matlab对信号加噪代码,Matlab给信号加噪声

在信号处理中经常需要把噪声叠加到信号上去&#xff0c;在叠加噪声时往往需要满足一定的信噪比&#xff0c;这样产生二个问题&#xff0c;其一噪声是否按指定的信噪比叠加&#xff0c;其二怎么样检验带噪信号中信噪比满足指定的信噪比。在MATLAB中可以用randn产生均值为0方差为…

洛谷 1057——台阶问题(递推与递归二分)

题目描述 有N级的台阶&#xff0c;你一开始在底部&#xff0c;每次可以向上迈最多K级台阶&#xff08;最少1级&#xff09;&#xff0c;问到达第N级台阶有多少种不同方式。 输入输出格式 输入格式&#xff1a; 输入文件的仅包含两个正整数N&#xff0c;K。 输出格式&#xff1…

在matlab中ungetfile后,11条Matlab实用小技巧

2011-02-25 15:361.在用循环画很多图时&#xff0c;为了减少对内存的占用&#xff0c;可以设置图像不显示&#xff0c;直接保存起来。其实也很简单&#xff0c;就是在plot等语句前加上一句&#xff1a;figure(visible,off)。这样画图时就不显示了&#xff01;2.记得很久以前就有…

ngix 创建新的网站

1. 进入ngix 的目录的conf 目录 的 vhosts 2. 复制一份新的v2.edc.com.conf 3. server_name : v2.edc.com root : /ali/...../目录 4. 重启nginx 服务 │ /etc/init.d/nginx start|stop|restart │ │ …

pkcs1转pkcs8 php,openssl RSA密钥格式PKCS1和PKCS8相互转换

openssl 生成pkcs1格式的私钥&#xff0c;密钥长度2048位, (PKCS1)openssl genrsa -out private.pkcs1.pem 2048PKCS1私钥转换为PKCS8openssl pkcs8 -topk8 -inform PEM -in private.pkcs1.pem -outform pem -nocrypt -out private.pkcs8.pem逆过程&#xff1a;PKCS8格式私钥再…

php上传同一张图片,两种php实现图片上传的方法_PHP

图片上传在项目中经常用到&#xff0c;几乎没有任何一个项目可以脱离图片或者是文件上传。本篇我在这向大家介绍两种常规的上传方式。(注&#xff1a;在这里我们仅仅是对功能的实现&#xff0c;不去做过多的前端的样式)一、利用form表单上传此种方式是最原始的上传方式&#xf…

用C语言实现中文到unicode码的转换

转自: http://blog.csdn.net/qq_21792169/article/details/50379275 源文件用不同的编码方式编写,会导致执行结果不一样 由于本人喜欢用Notepad编辑器&#xff0c;该编辑器的好处是小巧灵活&#xff0c;但是有几个地方做的不足&#xff0c;但是我都能够很好的采取相应的措施来…

java位运算求幂,程序员必学:快速幂算法

前阵子&#xff0c;有小伙伴在我B站的算法教程底下留言小伙伴们有任何疑问或者希望我解说任何内容&#xff0c;都可以在我的小我私家B站或民众号(xmg_mj)留言哦&#xff0c;我会尽我最大能力、只管抽时间去写文章\录视频来回应人人。关于快速幂实在快速幂相关的问题&#xff0c…

java过滤器

过滤器 1、Filter工作原理&#xff08;执行流程&#xff09; 当客户端发出Web资源的请求时&#xff0c;Web服务器根据应用程序配置文件设置的过滤规则进行检查&#xff0c;若客户请求满足过滤规则&#xff0c;则对客户请求&#xff0f;响应进行拦截&#xff0c;对请求头…

memory php 变量,php入门-变量

打印一串字符。echo PHP学到家&#xff0c;走到哪儿都不怕&#xff01;;?>输出236的计算结果echo 236;?>.连接符echo "Good,"."morning!";?>注释//echo "欢迎同学们&#xff01;";echo 12345;?>什么是变量$var "学PHP&quo…

假设检验

假设检验分参数假设和非参数假设。 假设 先假设原假设H0&#xff0c;对应的反面叫做备择假设H1。SAS一般沿用的规则是NEYMAN和PEARSON提出的&#xff1a;在控制犯第一类错误的原则下&#xff0c;是犯第二类错误的概率尽量小&#xff08;即&#xff0c;原假设受到保护&#xff0…

jeesite在eclipse中部署

1&#xff1a;下载下来最新版本的jeesite&#xff0c;首先要在本地安装好maven运行环境 2&#xff1a;运行 bin/eclipse.bat 生成工程文件并下载jar依赖包 如果需要修改默认项目名&#xff0c;请打开pom.xml修改第7行artifactId&#xff0c;然后再执行eclipse.bat文件 3&…

php抖音关注列表,网页PHP抖音批量取消关注JS代码

在很多时候我们抖音关注的人太多&#xff0c;想要批量取消但是一个一个点击太麻烦了&#xff0c;如何解放双手批量取消关注呢&#xff1f;今天分享一段JS代码&#xff0c;可在线批量取消关注&#xff0c;需电脑才能操作。首先打开抖音创作服务平台登录&#xff1a;https://crea…

python 灰度改二值,Python Image 对验证码进行灰度和二值法处理 去掉边框

def binarizing(img, threashold):img img.convert("L") # 转灰度pixdata img.load()w, h img.sizefor y in range(h):for x in range(w):if pixdata[x, y] < threashold:pixdata[x, y] 0else:pixdata[x, y] 255return imgdef removeFrame(img, width)::para…