1063. Set Similarity (25)
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.
Input Specification:
Each input file contains one test case. Each case first gives a positive integer N (<=50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers in the range [0, 109]. After the input of sets, a positive integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.
Output Specification:
For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.
Sample Input:3 3 99 87 101 4 87 101 5 87 7 99 101 18 5 135 18 99 2 1 2 1 3Sample Output:
50.0% 33.3%
总共N个集合;
每个集合的【初始】元素个数(有重复) 集合元素;【Ps:集合编号1~N,这里输入的时候要注意去除重复,我用set.inset(),会自动去重】
……
接下来K行
集合a 集合b
……
输出K行,每对集合的 交集元素个数(相同原数个数) 占 并集元素个数(两个集合元素合在一起去重复以后的元素个数) 的百分比
下面两个代码,一个ac另一个最后一个测试点超时
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月13日 10:36 | 答案正确 | 25 | 1063 | C++ (g++ 4.7.2) | 147 | 2744 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 180 | 12/12 |
1 | 答案正确 | 1 | 308 | 3/3 |
2 | 答案正确 | 1 | 180 | 3/3 |
3 | 答案正确 | 6 | 436 | 3/3 |
4 | 答案正确 | 147 | 2744 | 4/4 |
#include <iostream> #include <vector> #include<set> using namespace std; void readlnsets(vector<set<int>>*sets, int N) { int M, integers,index; for (index = 0; index < N; index++) { scanf("%d", &M); while (M--) { scanf("%d", &integers); (*sets)[index].insert(integers);/*当数已经在集合里面就不再放入了*/ } } } int main() { int N,K,a,b; double Nt, Nc; scanf("%d", &N); vector<set<int>>sets(N); readlnsets(&sets,N); scanf("%d", &K); while (K--) { scanf("%d%d", &a, &b); a--; b--; Nc = 0.0; for (set<int>::iterator iter = sets[a].begin(); iter != sets[a].end(); iter++) if (sets[b].find((*iter))!= sets[b].end())Nc++; Nt = sets[a].size() + sets[b].size()-Nc; printf("%.1lf%\n", Nc / Nt*100.0); } system("pause"); return 0; } |
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月13日 10:25 | 部分正确 | 21 | 1063 | C++ (g++ 4.7.2) | 19 | 436 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 384 | 12/12 |
1 | 答案正确 | 1 | 308 | 3/3 |
2 | 答案正确 | 1 | 180 | 3/3 |
3 | 答案正确 | 19 | 436 | 3/3 |
4 | 运行超时 | 0/4 |
#include <iostream> #include <vector> #include<set> #include<iomanip> using namespace std; void readlnsets(vector<set<int>>*sets, int N) { int M, integers,index; for (index = 0; index < N; index++) { cin >> M; while (M--) { cin >> integers; (*sets)[index].insert(integers);/*当数已经在集合里面就不再放入了*/ } } } int main() { int N,K,a,b; double Nt, Nc; set<int>Uset; cin >> N; vector<set<int>>sets(N); readlnsets(&sets,N); cin >> K; while (K--) { cin >> a >> b; a--; b--; Uset.insert(sets[a].begin(),sets[a].end()); Uset.insert(sets[b].begin(), sets[b].end()); Nt = Uset.size(); Nc = sets[a].size() + sets[b].size() - Nt; cout << setiosflags(ios::fixed) << setprecision(1) << Nc / Nt *100.0<<"%"<< endl; Uset.clear(); } system("pause"); return 0; }