Codeforces768C Jon Snow and his Favourite Number

在一场与异鬼的战斗中,Jon Snow希望通过对他手下的游骑兵的力量进行特定操作来提高他们的战斗力。具体来说,他会将游骑兵按力量升序排列,并将其与他的幸运数字进行异或运算,以期获得更强的战士。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Jon Snow and his Favourite Number
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jon Snow now has to fight with White Walkers. He has n rangers, each of which has his own strength. Also Jon Snow has his favourite number x. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number x, he might get soldiers of high strength. So, he decided to do the following operation k times:

  1. Arrange all the rangers in a straight line in the order of increasing strengths.
  2. Take the bitwise XOR (is written as ) of the strength of each alternate ranger with x and update it's strength.
Suppose, Jon has 5 rangers with strengths [9, 7, 11, 15, 5] and he performs the operation 1 time with x = 2. He first arranges them in the order of their strengths, [5, 7, 9, 11, 15]. Then he does the following:
  1. The strength of first ranger is updated to , i.e. 7.
  2. The strength of second ranger remains the same, i.e. 7.
  3. The strength of third ranger is updated to , i.e. 11.
  4. The strength of fourth ranger remains the same, i.e. 11.
  5. The strength of fifth ranger is updated to , i.e. 13.
The new strengths of the 5 rangers are [7, 7, 11, 11, 13]

Now, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations k times. He wants your help for this task. Can you help him?

Input

First line consists of three integers nkx (1 ≤ n ≤ 1050 ≤ k ≤ 1050 ≤ x ≤ 103) — number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively.

Second line consists of n integers representing the strengths of the rangers a1, a2, ..., an (0 ≤ ai ≤ 103).

Output

Output two integers, the maximum and the minimum strength of the rangers after performing the operation k times.

Examples
input
5 1 2
9 7 11 15 5
output
13 7
input
2 100000 569
605 986
output
986 605

—————————————————————————————————————
题目的意思是把一组数从小到大排列,奇数位与给定的数异或,重复m次,问最后最大的数和最小的数

可以找循环节,因为数字小也可以纯暴力跑。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
using namespace std;
int cnt[2][2005];
int n,k,m;



int main()
{
    int x;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(cnt,0,sizeof(cnt));

        for(int i=0; i<n; i++)
        {
            scanf("%d",&x);
            cnt[0][x]++;
        }
        for(int i=0; i<m; i++)
        {
            bool flag=0;
            for(int j=0; j<2000; j++)
            {
                if(cnt[0][j]>0||cnt[1][j]>0)
                {
                    if(cnt[0][j]%2==0)
                    {
                        int s=j^k;
                        if(s<=j)
                            cnt[0][s]+=cnt[0][j]/2;
                        else
                            cnt[1][s]+=cnt[0][j]/2;
                        cnt[0][j]/=2;
                        cnt[0][j]+=cnt[1][j];
                        cnt[1][j]=0;
                    }
                    else
                    {
                        int s=j^k;
                        if(flag==0)
                        {
                            if(s<=j)
                                cnt[0][s]+=cnt[0][j]/2+1;
                            else
                                cnt[1][s]+=cnt[0][j]/2+1;
                            cnt[0][j]/=2;
                            cnt[0][j]+=cnt[1][j];
                            cnt[1][j]=0;
                            flag=1;
                        }
                        else
                        {
                            if(s<=j)
                                cnt[0][s]+=cnt[0][j]/2;
                            else
                                cnt[1][s]+=cnt[0][j]/2;
                            cnt[0][j]/=2;
                            cnt[0][j]++;
                            cnt[0][j]+=cnt[1][j];
                            cnt[1][j]=0;
                            flag=0;
                        }

                    }
                }
            }
        }
        int mn,mx;
        for(int i=0;i<=2000;i++)
            {
                if(cnt[0][i]>0)
                {
                    mn=i;
                    break;
                }
            }
             for(int i=2000;i>=0;i--)
            {
                if(cnt[0][i]>0)
                {
                    mx=i;
                    break;
                }
            }
        printf("%d %d\n",mx,mn);
    }
    return 0;
}






### 解题思路 #### 问题描述 Codeforces 1678C - Tokitsukaze and Strange Inequality 是一道关于排列组合与前缀和的应用问题。给定一个长度为 \( n \) 的排列数组 \( p \),需要统计满足条件 \( a < b < c < d \) 并且 \( p_a < p_c \) 同时 \( p_b > p_d \) 的四元组数量。 --- #### 核心思想 由于数据规模较小 (\( n \leq 5000 \)),可以直接通过枚举的方式解决问题。为了降低时间复杂度,引入 **前缀和** 技术来加速计算过程[^3]。 具体来说: - 枚举变量 \( a \) 和 \( c \),固定它们之后,目标是快速找到符合条件的 \( b \) 和 \( d \)。 - 使用预处理好的前缀和数组 `num` 来高效查询某个范围内满足特定关系的数量。 - 定义辅助数组 `sum` 表示对于固定的区间范围内的某些约束条件下的累积计数结果。 --- #### 实现细节 ##### 步骤一:构建前缀和数组 `num` 定义二维数组 `num[i][j]`,其中 `num[i][j]` 表示在序列的前 \( i \) 项中,有多少个元素大于 \( j \)。 该数组可以通过如下方式初始化: ```python n = len(p) max_val = max(p) # 初始化 num 数组 num = [[0] * (max_val + 2) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(max_val + 1, -1, -1): # 反向遍历以保持正确性 if p[i - 1] > j: num[i][j] = num[i - 1][j] + 1 else: num[i][j] = num[i - 1][j] ``` 上述代码的时间复杂度为 \( O(n \cdot m) \),其中 \( m \) 是数组中的最大值。 --- ##### 步骤二:定义并填充辅助数组 `sum` 定义另一个二维数组 `sum[i][j]`,它表示当 \( a=i \), \( c=j \) 时,在区间 \([a+1, c-1]\) 中满足 \( p[b] > p[d] \) 的总贡献次数。 利用动态规划的思想逐步更新此数组: ```python sum_ = [[0] * (n + 1) for _ in range(n + 1)] bucket = [0] * (max_val + 1) for l in range(n - 1, 0, -1): bucket[p[l]] += 1 for r in range(l + 2, n + 1): sum_[l][r] = sum_[l][r - 1] + (num[r - 1][p[r - 1]] - num[l][p[r - 1]]) ``` 这里的关键在于如何有效累加当前区间的合法贡献,并借助之前已经计算的结果减少重复运算。 --- ##### 步骤三:枚举所有可能的 \( a \) 和 \( c \) 最后一步是对所有的 \( a \) 和 \( c \) 进行双重循环,并将对应位置上的 `sum[a][c]` 加入最终答案中: ```python result = 0 for a in range(1, n - 2): for c in range(a + 2, n): result += sum_[a][c] print(result) ``` 整个算法的核心部分即完成以上三个阶段的操作即可实现高效的解决方案。 --- ### 总结 本题主要考察的是对多重嵌套结构的有效简化以及合理运用前缀和技巧的能力。通过巧妙设计的数据结构能够显著提升程序运行效率至可接受水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值