Codeforces Round #836 (Div. 2)题解

A. SSeeeeiinngg DDoouubbllee

这题倒是简单,将字符串正反输出一遍就好了

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define all(a) a.begin(),a.end()

void solve()
{
	string s;
	cin >> s;
	cout << s;
	reverse(all(s));
	cout << s << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

	int t = 1;
	cin >> t;
	while(t--)
		solve();
    
    return 0;
}

B. XOR = Average

这里考虑两种情况:

n n n奇数时,所有数均取 1 1 1,异或结果为 1 1 1,平均数为 1 1 1

n n n偶数时,前 n − 2 n-2 n2个数取 2 2 2,后面两个数分别取 1 1 1 3 3 3,异或结果为 2 2 2,平均值为 2 2 2

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

void solve()
{
	int n;
	cin >> n;
	if(n & 1)
	{
		for(int i = 0;i < n;++i)
			cout << 1 << ' ';
		cout << endl;
	}
	else
	{
		for(int i = 0;i < n - 2;++i)
			cout << 2 << ' ';
		cout << 1 << ' ' << 3 << endl;
	}
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

	int t = 1;
	cin >> t;
	while(t--)
		solve();
    
    return 0;
}

C. Almost All Multiples

首先来看看不存在的情况:

n n n不是 x x x的倍数时,我们无法将所有坐标填满,则不存在

再来看看怎么构造出这个数组:

看看这个例子:

18 3

我们先把 n n n放到 x x x上,构造出一个可行但是字典序不一定是最小的数组

123456789101112131415161718
321845678910111213141516171

我们将18放在了3上,18是3的倍数,这个操作是可行的

然后我们继续往后看,发现 6 , 9 , 12 , 15 6,9,12,15 6,9,12,15也都是 3 3 3的倍数,可以将他们当中的数与 3 3 3交换,来减小字典序。那么,与3交换的下标除了满足是3的倍数这一条件之外,还要是18的因数,这样才能将18放在上面。这样,剩下的还有 6 , 9 6,9 6,9两个数。显然,与6交换时字典序最小的,那么,该数组最终的模样应该是:

123456789101112131415161718
326451878910111213141516171

就这样一直交换下去即可

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define vint vector<int>

void solve()
{
    int n, x;
    cin >> n >> x;
    if (n % x)
    {
        cout << -1 << endl;
        return;
    }
    vint res(n + 1);
    res[1] = x,res[n] = 1;
    int k = x;
    for(int i = 2;i <= n;++i)
    {
    	if(i > x && i % k == 0 && n % i == 0)
    	{
    		res[k] = i;//将该下标的值放到上一个符合要求的下标k上
    		k = i;//更新k值
    	}
    	else
    		res[i] = i;
    }
    res[n] = 1;
    
    for (int i = 1; i <= n; ++i)
        cout << res[i] << ' ';
    cout << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
        solve();

    return 0;
}

D. Range = √Sum

首先来看看n为偶数的情况:

我们选取 [ n − n 2 , n − 1 ] ∪ [ n + 1 , n + n 2 ] [n - \frac{n}{2},n - 1] \cup [n + 1,n + \frac{n}{2}] [n2n,n1][n+1,n+2n]。不难看出,他们的和为 n 2 n^2 n2,最大值和最小值的差为 n n n,满足要求

然后是n为奇数的时候:

此时我们构造出 ( n + 1 ) 2 (n + 1) ^ 2 (n+1)2出来

[ n − n + 1 2 + 2 , n ] ∪ [ n + 2 , n + n + 1 2 + 2 ] [n - \frac{n + 1}{2} + 2,n] \cup [n + 2,n + \frac{n + 1}{2} + 2] [n2n+1+2,n][n+2,n+2n+1+2]。不难看出,他们的和为 ( n + 1 ) 2 (n + 1)^2 (n+1)2,差值为 n + 1 n+1 n+1,满足要求

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

void solve()
{
    int n;
    cin >> n;
    if(n & 1)
    {
    	int k = (n + 1) / 2;
    	for(int i = n - k + 2;i <= n;++i)
    		cout << i << ' ';
    	for(int i = n + 3;i <= n + 2 + k;++i)
    		cout << i << ' ';									
    }
    else
    {
    	for(int i = n - n / 2;i < n;++i)
    		cout << i << ' ';
    	for(int i = n + 1;i <= n + n / 2;++i)
    		cout << i << ' ';
    }
    cout << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
        solve();

    return 0;
}

后面两题不会QAQ

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值