团体程序设计天梯赛-练习集 L2-005
来源:互联网 发布:cf有刷枪软件吗 编辑:程序博客网 时间:2024/04/30 19:02
超时代码:
1.
#include <cstdio>#include <algorithm>#include <set>using namespace std;int n, m;int num[55], numset[55][10005];void print() { for (int i=0; i<n; i++) printf("%d ", num[i]); puts(""); for (int i=0; i<n; i++) for (int j=0; j<num[i]; j++) printf("%d%c", numset[i][j], j==num[i]-1?'\n':' ');}int main(){ //freopen("in.txt", "r", stdin); scanf("%d", &n); for (int i=0; i<n; i++) { scanf("%d", &num[i]); for (int j=0; j<num[i]; j++) scanf("%d", &numset[i][j]); } for (int i=0; i<n; i++) { sort(numset[i], numset[i]+num[i]); int ptr=1; for (int j=1; j<num[i]; j++) { if (numset[i][j]==numset[i][j-1]) continue; numset[i][ptr++]=numset[i][j]; } num[i]=ptr; } scanf("%d", &m); int a, b; for (int i=0; i<m; i++) { scanf("%d%d", &a, &b); a--; b--; int cnt=0; for (int j=0; j<num[a]; j++) { int l=0, r=num[b]-1; int tb=numset[a][j]; while (l<=r) { int mid=(l+r)>>1; int ta=numset[b][mid]; if (ta==tb) { cnt++; break; } else if (ta>tb) { r=mid-1; } else { l=mid+1; } } } set<int> s; for (int i=0; i<num[a]; i++) s.insert(numset[a][i]); for (int i=0; i<num[b]; i++) s.insert(numset[b][i]); double res=(double)cnt/s.size(); res*=100; printf("%.2lf", res); puts("%"); } return 0;}
2.
#include <cstdio>#include <set>#include <algorithm>using namespace std;#define all(x) x.begin(), x.end()#define ins(x) inserter(x, x.begin())int n, m;set<int> numset[55];int main(){ //freopen("in.txt", "r", stdin); scanf("%d", &n); for (int i=0; i<n; i++) { int num; scanf("%d", &num); for (int j=0; j<num; j++) { int temp; scanf("%d", &temp); numset[i].insert(temp); } } scanf("%d", &m); for (int i=0; i<m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; set<int> t1, t2; set_intersection(all(numset[a]), all(numset[b]), ins(t1)); set_union(all(numset[a]), all(numset[b]), ins(t2)); double res=(double)t1.size()/t2.size(); res*=100; printf("%.2lf", res); puts("%"); } return 0;}
AC代码:
#include <cstdio>
#include <algorithm>
using namespace std;
int n, m;
int num[55], numset[55][10005], tot[500005], ha[55][10005], vis[500005];
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d", &n);
for (int i=0; i<n; i++) {
scanf("%d", &num[i]);
for (int j=0; j<num[i]; j++)
scanf("%d", &numset[i][j]);
sort(numset[i], numset[i]+num[i]);
num[i]=unique(numset[i], numset[i]+num[i])-numset[i];
}
int cnt=0;
for (int i=0; i<n; i++)
for (int j=0; j<num[i]; j++)
tot[cnt++]=numset[i][j];
sort(tot, tot+cnt);
cnt=unique(tot, tot+cnt)-tot;
for (int i=0; i<n; i++)
for (int j=0; j<num[i]; j++)
ha[i][j]=lower_bound(tot, tot+cnt, numset[i][j])-tot;
scanf("%d", &m);
for (int i=1; i<=m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
int up=0, down=num[a];
for (int j=0; j<num[a]; j++)
vis[ha[a][j]]=i;
for (int j=0; j<num[b]; j++)
if (vis[ha[b][j]]==i)
up++;
else
down++;
double res=(double)up/down;
res*=100;
printf("%.2lf", res);
puts("%");
}
return 0;
}
- 团体程序设计天梯赛-练习集 L2-005
- 团体程序设计天梯赛-练习集 L2-3. 月饼 贪心
- 团体程序设计天梯赛-练习集 L2-002
- 团体程序设计天梯赛-练习集 L2-007 家庭房产
- 团体程序设计天梯赛-练习集 L2-002. 链表去重
- 团体程序设计天梯赛-练习集 L2-003. 月饼LOVE
- 团体程序设计天梯赛-练习集 L2-007. 家庭房产
- 团体程序设计天梯赛-练习集 L2-009. 抢红包
- 团体程序设计天梯赛-练习集 L2-010. 排座位
- 团体程序设计天梯赛-练习集 L2-001. 紧急救援
- 团体程序设计天梯赛-练习集 L2-001. 紧急救援
- 团体程序设计天梯赛-练习集L2-002. 链表去重
- 团体程序设计天梯赛-练习集L2-003. 月饼
- 团体程序设计天梯赛-练习集L2-007. 家庭房产
- 团体程序设计天梯赛-练习集L2-009. 抢红包
- 团体程序设计天梯赛-练习集L2-010. 排座位
- 团体程序设计天梯赛-练习集-L2-013. 红色警报(dfs)
- 团体程序设计天梯赛-练习集-L2-013. 红色警报(dfs)
- Leetcode no. 237
- Linux学习笔记(3) --- Linux常用命令
- 单调队列1003 HDU 3530 Subsequence
- glOrtho()函数和glViewport()函数的理解
- 第十五周项目:程序填空
- 团体程序设计天梯赛-练习集 L2-005
- mysql grant命令
- python偏函数应用
- 《Algorithms》——删除二叉查找树中的结点
- Eclipse NDK 开发之 HelloWorld
- 数据库连接池的实现及原理
- Underscore学习(2)
- 祝贺:神威太湖之光夺冠Top500,采用国产众核处理器SW26010
- Android小记