Educational Codeforces Round 42 (Rated for Div. 2)

题目链接:传送门

A. Equator

(水题)

题意:给出一个n(1~n),然后是n个数a[n],问你1~x个值的总和是全部值总和的一半或一半以上的最小x是多少,输出x

解法:水题不提

代码如下

#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<iostream>
using namespace std;
#define ll long long
const int maxn = 3e5+500;
ll a[maxn];
int main()
{
	int n; ll sum = 0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		sum += a[i];
	}
	if (sum % 2 != 0)sum++;
	sum /= 2;
	ll x = 0;
	for (int i = 1; i <= n; i++)
	{
		x += a[i];
		if (x >= sum)
		{
			cout << i << endl; break;
		}
	}
	return 0;
}

B. Students in Railway Carriage

(水题)

题意:给出一个n,a,b,再给出一串字符串s,其中n表示字符串s的长度,a表示第一种人的数量,b表示另一种人的数量,

字符串s只含有"." and "*"。“.”表示座位空着,“*”表示该座位有人了。题目要求a和b不能坐在一起,要求最多能坐下多少人?

解法:扫一遍字符串s,把所有连续的"."的长度保存下来,之后一条条取出,往里面放a或b,相隔着坐,因此只要判断当前段"."的长度的奇偶和a,b的大小与该段一半长度等关系(if,else)几次就可以暴力过了。

代码如下

#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<iostream>
using namespace std;
#define ll long long
const int maxn = 3e5 + 500;
char s[maxn];
int x[maxn];
int main()
{
	int n, a, b;
	scanf("%d%d%d", &n, &a, &b);
	scanf("%s", &s);
	s[strlen(s)] = '*';
	s[strlen(s) + 1] = '\0';
	int cnt = 0, t = 1, MAX = 0;
	for (int i = 0; i < strlen(s); i++)
	{
		if (s[i] == '.')
		{
			cnt++; t = 1;
		}
		else
		{
			if (t == 1)
			{
				x[cnt]++;
				MAX = max(MAX, cnt);
			}
			cnt = 0;
			t = 0;
		}
	}
	int ans = 0;
	for (int i = MAX; i >= 1; i--)
	{
		while (x[i])
		{
			if (i % 2 == 0)
			{
				if (a >= i / 2) { ans += i / 2; a -= i / 2; }
				else { ans += a; a = 0; }
				if (b >= i / 2) { ans += i / 2; b -= i / 2; }
				else { ans += b; b = 0; }
			}
			else
			{
				if (a > b)
				{
					if (a >= (i / 2) + 1) { ans += (i / 2) + 1; a -= (i / 2) + 1; }
					else { ans += a; a = 0; }
					if (b >= i / 2) { ans += i / 2; b -= i / 2; }
					else { ans += b; b = 0; }
				}
				else
				{
					if (a >= i / 2) { ans += i / 2; a -= i / 2; }
					else { ans += a; a = 0; }
					if (b >= (i / 2) + 1) { ans += (i / 2) + 1; b -= (i / 2) + 1; }
					else { ans += b; b = 0; }
				}
			}
			x[i]--;
		}
	}
	cout << ans << endl;
	return 0;
}

C. Make a Square

(dfs)

题意:给你一个n,n是一个不含前导零的数,问你最少删掉n中的几个字符,可以使n成为一个不含前导零的数且是可以开平方(也就是某个整数的平方数)如果没办法输出-1,否则输出最少操作数。

解法:判断是否是平方数用(ll)sqrt(x)*(ll)sqrt(x) == x即可,至于删除字符的话,由于这个数n小于1e9,因此最多9位数,对每个数进行0或1的判断进行搭配一共有2^9种结果,只需要dfs即可,在return 前判断当前搭配是否满足是”平方数“,满足就记录最小即可。

代码如下

#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<iostream>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 3e5 + 500;
char ss[20];
int ans, len;
void dfs(int x, ll s, int cnt)
{
	if (x == len)
	{
		ll t = 0, k = s; while (k) { k /= 10; t++; }
		if ((ll)sqrt(s)*(ll)sqrt(s) == s&&s != 0 && t == cnt)
			ans = min(ans, len - cnt);
		return;
	}
	dfs(x + 1, 10 * s + ss[x] - '0', cnt + 1);
	dfs(x + 1, s, cnt);
}
int main()
{
	cin >> ss;
	ll x = atoi(ss);
	if ((ll)sqrt(x)*(ll)sqrt(x) == x)
		cout << "0" << endl;
	else
	{
		len = strlen(ss);
		ans = inf;
		dfs(0, 0, 0);
		if (ans != inf)
			cout << ans << endl;
		else
			cout << -1 << endl;
	}
	return 0;
}

D. Merge Equals

(结构体+优先队列)

题意:给你一个n表示n个数a[n],这是一个集合群,集合可能含有2个或以上的相同数,我们需要不断对集合从左到右进行操作:对于2个相同的数,将第一个出现的加到第二个上(不一定要相邻,第一个数去掉),问你无限次操作后该集合剩几个数,分别输出它们。

解法:(第一次看到就想到使用优先队列不递减的顺序进行判断与合并可以得到最终题目要的目标集合个数和元素,可是一时间脑残没想到怎么标记下标进行移动,改用map+priority_queue来写不幸超时了)赛后补题看到队友就是用我原先的方法写的,不过定义了个结构体保存下标,每次合并把小的大的下标赋到合成的2*x结果上放回队列,之后输出时安装它们的下标从小到大输出即可。

代码如下

#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 3e5 + 500;
struct node
{
	ll x;
	int index;
	node() {}
	node(ll xx, int y) :x(xx), index(y) {}
	bool operator <(const node &a) const {
		if (x != a.x)
			return x > a.x;   //按照x从小到大
		return index > a.index; //index从小到大排序
	}
};
bool cmp(node u, node v)
{
	return u.index < v.index;
}
priority_queue<node>q;
vector<node>ans;
int main()
{
	int n, key;
	ll x;
	while (~scanf("%d", &n))
	{
		while (!q.empty()) q.pop();
		ans.clear();
		for (int i = 0; i < n; i++)
		{
			scanf("%lld", &x);
			q.push(node(x, i));
		}
		while (!q.empty())
		{
			node u = q.top(); q.pop(); key = 1;
			if (q.size() >= 1)
			{
				node v = q.top();
				if (u.x == v.x)
				{
					q.push(node(u.x * 2, max(u.index, v.index)));
					q.pop();
					key = 0;
				}
			}
			if (key)ans.push_back(u);
		}
		sort(ans.begin(), ans.end(), cmp);
		int res = ans.size();
		cout << res << endl;
		for (int i = 0; i < res; i++)
			printf("%lld%c", ans[i].x, i == res - 1 ? '\n' : ' ');
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值