日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[TC SRM 685 div1 lev1] MultiplicationTable2

發布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。