Codeforces Round #822 (Div. 2)(A、B、C)

文章提供了三道Codeforces编程竞赛中的题目解析,包括A题的数组处理,寻找构建等边三角形的最小步数,B题的金字塔点灯问题,以及C题的字符串操作和代价计算。解决方案涉及排序、特定模式的输出和动态规划策略。
摘要由CSDN通过智能技术生成

A题

Problem - A - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1734/problem/A题意:
        给个数组,找出最小的可以凑出等边三角形的步骤数。

思路:

        排序后,每三个处理一次最小值即可。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 310;

int T;
int a[N], s[N];

signed main()
{
	cin >> T;
	while(T--)
	{
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; ++ i) scanf("%d", &a[i]);
		sort(a, a + n + 1);
		int ans = 0x3f3f3f3f, sum = 1, dan = 0;
		for (int i = 1; i <= n - 2; ++ i)
			ans = min(ans, a[i + 2] - a[i]); 
		printf("%d\n", ans);
	}
	return 0;
 } 

B题

Problem - B - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1734/problem/B题意:
        金字塔点灯,要求每层金字塔都需要路径亮灯数相同。

思路:
        根据观察,除了第一二层之外,其余层除了第一个和最后一个是1,都是0即可。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

using namespace std;

int T;

signed main()
{
	cin >> T;
	
	while(T--)
	{
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; ++ i)
		{
			if(i == 1 || i == 2) for(int j = 1; j <= i; ++ j) printf("%d ", 1);
			else {
				for(int j = 1; j <= i; ++ j)
				{
					if(j == 1 || j == i) printf("%d ", 1);
					else printf("%d ", 0);
				}
			}
			puts("");
		}
	}
	
	return 0;
}

C题

Problem - C - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1734/problem/C题意:
        给定一个字符串,代表T数组中没有而S中没有的数,每次可以进行的操作是用某个数的最小倍数来删除从而最终使S -> T,每次使用的数都将加到结果中, 我们需要求的是最小代价。

        例如: 1 2 3 4 5 6 7 和 1 2 4 7,结果为3 + 3 + 5 = 11

思路:

        枚举每个k,直到遇到不能删除的为止(不是第一小的倍数),然后一直往后枚举即可。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>

using namespace std;

const int N = 1e6 + 10;

int T;

void solve() {
    int n;string t;
    cin >> n >> t;
    int64_t ans = 0;
    for (int i = 1;i <= n;i++) {
		for (int j = i;j <= n;j += i) {
      		if (t[j - 1] == '1') break;
      		if (t[j - 1] == '0') {
        		t[j - 1] = '2';
        		ans += i;
      		}
    	}
  	}
  	cout << ans << "\n";
}

signed main()
{
	scanf("%d", &T);
	
	while(T--)
	{
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dawpro_加薪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值