ABC314 A-D、F

AtCoder Beginner Contest 314 - AtCoder

被期望薄纱了,E卡了一个多小时没啥有用的思路,F没看...后天补完题补上

A - 3.14

题意:

给出π的前100位,输出π保留小数点后n位(超过n位的直接删除)

题解:

签到!

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL N = 1e3 + 10, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
char ch[] = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
void solve()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i <= n + 1; ++i)
		printf("%c", ch[i]);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

B - Roulette

题意:

有n人猜一个0到36的数字,第i个人猜了ci个数字分别为ai,1、ai,2...ai,ci。给出数字x,输出所有猜中数字x并且猜的数字数量最少的人的编号

题解:

先求出所有猜中数字x人中最少的猜的数的数量min,记录所有猜中数字x并且猜的数字数量为min的人的编号,输出。

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL N = 1e2 + 10, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
set<int>st[N];
void solve()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		int x;
		scanf("%d", &x);
		for (int j = 1; j <= x; ++j)
		{
			int y;
			scanf("%d", &y);
			st[i].insert(y);
		}
	}
	int x, minn = INF;
	scanf("%d", &x);
	for (int i = 1; i <= n; ++i)
	{
		if (st[i].find(x) != st[i].end())
			minn = min(minn, (int)st[i].size());
	}
	vector<int>ans;
	for (int i = 1; i <= n; ++i)
	{
		if (st[i].find(x) != st[i].end() && st[i].size() == minn)
			ans.push_back(i);
	}
	printf("%d\n", ans.size());
	for (auto i : ans)
		printf("%d ", i);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

C - Rotate Colored Subsequence

题意:

给出一个长度为n的字符串s,每个字符都有一个颜色ci(1<=ci<=m)。对于每个字符都把它换成左边距离它最近的颜色相同的字符(若不存在则为颜色相同的最右的字符)。

题解:

分别存下每种颜色字符下标的集合然后模拟即可。

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL N = 2e5 + 10, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
vector<int>v[N];
char ch[N], ans[N];
void solve()
{
	int n, m;
	scanf("%d%d%s", &n, &m, ch + 1);
	for (int i = 1; i <= n; ++i)
	{
		int x;
		scanf("%d", &x);
		v[x].push_back(i);
	}
	for (int i = 1; i <= m; ++i)
	{
		int k = v[i].size();
		for (int j = 0; j < k; ++j)
		{
			ans[v[i][j]] = ch[v[i][(j - 1 + k) % k]];
		}
	}
	printf("%s", ans + 1);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

D - LOWER

题意:

给出一个长度为n的由小写或大写字母组成的字符串s,对该字符串进行q次操作(ti,xi,ci):1、ti=1时,将字符串种的第xi个字符换成ci,2、ti=2时,将字符串中的所有大写字母换成小写字母,3、ti=3时,将字符串中的所有小写字母换成大写字母。(对于ti=2,3的情况需要输入xi,ci但是不会被使用)

题解:

暴力模拟时,1操作时间复杂度O(1),2、3操作时间复杂度O(n)。而对于多次23操作时,仅有最后一次操作会起作用。所以我们可以记录23操作的数量后不执行不是最后一次的23操作,使得时间复杂度较大的23操作仅执行一次。

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL N = 5e5 + 10, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
char ch[N], c[N];
int op[N], x[N];
void solve()
{
	int n, q, s = 0;
	scanf("%d%s%d", &n, ch + 1, &q);
	for (int i = 1; i <= q; ++i)
	{
		scanf("%d%d %c", &op[i], &x[i], &c[i]);
		if (op[i] != 1)++s;
	}
	for (int i = 1; i <= q; ++i)
	{
		if (op[i] == 1)ch[x[i]] = c[i];
		else if (!--s)
		{
			for (int j = 1; j <= n; ++j)
			{
				if (op[i] == 2 && ch[j] <= 'Z')
					ch[j] += 'a' - 'A';
				else if (op[i] == 3 && ch[j] >= 'a')
					ch[j] -= 'a' - 'A';
			}
		}
	}
	printf("%s", ch + 1);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

F - A Certain Game

题意:

有N个玩家,刚开始每个玩家都只与自己属于一个队伍。进行n-1场比赛:每场比赛都为人xi与人yi所属的队伍打一架,记队伍x的人数为sx,队伍y的人数为sy,则队伍x获证的概率为sx/(sx+sy),队伍y获胜的概率为sy/(sx+sy),并且在比赛后两个队伍将会合并。

求每个玩家所在队伍获胜场次的期望。

题解:

构造一颗这样的树:(图来自官解:解説 - AtCoder Beginner Contest 314

 对于每两个队伍打一架之后都记录那个队伍的获胜概率(即进计算那一场的胜场数期望值),然后新建一个节点(合并之后的新队伍),并用并查集维护每个人所属的队伍以及队伍的人数。这样建完树之后再从根往下给每个人加上他的胜场数期望就行了。很像克鲁斯卡尔重构树...

只能说...写过克鲁斯卡尔重构树就觉得出了思路之后写起来很顺。但是赛时没开到这题...

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL N = 4e5 + 10, MOD = 998244353, INF = 0x3f3f3f3f;
LL e[N], idx;
vector<int>kids[N];
int p[N], s[N];
int find(int x)//并查集
{
	if (p[x] != x)
		p[x] = find(p[x]);
	return p[x];
}
LL qpow(LL x, LL y)//快速幂
{
	x %= MOD;
	if (y == 0)return 1;
	if (y % 2)
		return qpow(x * x, y / 2) * x % MOD;
	return qpow(x * x, y / 2) % MOD;
}
LL inv(LL x)//求逆元
{
	return qpow(x, MOD - 2);
}
void insert(int x, int y)
{
	x = find(x), y = find(y);//找到x,y所属的团队
	e[x] = s[x] * inv(s[x] + s[y]) % MOD;//计数团队获胜的概率
	e[y] = s[y] * inv(s[x] + s[y]) % MOD;
	p[x] = p[y] = ++idx;//新建一个新的团队
	kids[idx] = { x,y };//记录边
	s[idx] = s[x] + s[y];//更新新团队人数
}
void dfs(int u,int f)//从根往下搜一遍给每个人的胜率加上
{
	e[u] = (e[u] + e[f]) % MOD;
	for (auto v : kids[u])
		dfs(v, u);
}
void solve()
{
	int n;
	scanf("%d", &n);
	idx = n;//初始化新节点编号
	for (int i = 1; i <= 2 * n; ++i)
		p[i] = i, s[i] = 1;//初始化并查集
	for (int i = 1; i < n; ++i)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		insert(x, y);//xy所属团队打一架
	}
	dfs(idx, 0);//
	for (int i = 1; i <= n; ++i)
		printf("%lld ", e[i]);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值