Codeforces Round #642 (Div. 3) 总结

Codeforces Round #642 (Div. 3) 总结

一. Most Unstable Array

在这里插入图片描述在这里插入图片描述

题意:给定两个整数 n 和 k,构造一个只包含非负整数的序列 a,使得相邻的两个数差的绝对值的和最大。
分析:当n=1时,那么结果是0; 当n=2时,结果是m。当n>2时,结果是2*m。
代码
#include <iostream>
#include <cstdio>
using namespace std;
int t,n,m;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
    	scanf("%d%d",&n,&m);
    	if(n==1) 
			printf("0\n");
    	else if(n==2) 
			printf("%d\n",m);
    	else 
			printf("%d\n",2*m);
    }
	return 0;
}

二. Two Arrays And Swaps

在这里插入图片描述在这里插入图片描述

题意:给定两个序列 a 和 b,你最多可以进行 k 次操作,每次交换a,b同一位置的元素,要求进行操作完 a 序列的和最大。
分析:对于a,b两个数组分别按照不同的排序方法进行排序,a从小到大,b从大到小。然后顺序比较a,b的值,如果a小于b交换。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int N = 37;
int a[N],b[N];
int t,n,k;
bool cmp(int x, int y)
{
	return x > y;
}
int main()
{
	cin >> t;
	while(t--)
	{
		int cnt = 0;
		int sum = 0;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		cin >> n >> k;
		for(int i = 1; i <= n; i++)
			cin >> a[i];
		for(int i = 1; i <= n; i++)
			cin >> b[i];
		if(k==0)
		{
			for(int i = 1; i <= n; i++)
				sum += a[i];
			cout << sum << endl;
			continue;
		}
		sort(a+1,a+n+1);
		sort(b+1,b+n+1,cmp);
		for(int i = 1; i <= n; i++)
		{
			if(a[i]<b[i])
			{
				swap(a[i],b[i]);
				cnt++;
			}
			if(cnt==k)
				break;
		}
		for(int i = 1; i <= n; i++)
			sum += a[i];
		cout << sum << endl;
	}
	return 0;
}

三. Board Moves

在这里插入图片描述在这里插入图片描述

题意:给定一个n*n的格子,每个位置都有一个数,我们需要把所有的数字移动到同一个格子里面,问所需要的最小的移动次数。
分析:很容易想到是往最中间那个格子里面移动所需要的移动次数最少,当我们把每个格子所需要的最小移动次数写出来之后,规律其实就比较明显了,从最中间那个格子开始每一圈上的位置所需要的最小移动次数是相同的,是从1到(n-1)/2,同时,每一圈格子的个数是8的倍数,循环累加就行了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int t,n;
int main()
{
	cin >> t;
	while(t--)
	{
    	cin >> n;
    	if(n==1) 
		{
			cout << "0" << endl;
			continue;
		} 
    	long long sum = 0;
		long long node = (n-1)/2;
   		for(long long i = 1; i <= node; i++)
    		sum += i * i * 8 ;
    	cout << sum << endl;
 	}
	return 0;
}
总结:前三个题目是真的不难,但是做的时候没有全部作对,发现在做题的过程中总是把一些简单问题想复杂了,然后导致实现过程非常繁琐,然后浪费时间,太麻烦还容易出错,再次浪费时间,最后就心态爆炸了,以后做前面几个题不能想的太复杂。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值