题意:

给出一个数列,和一种操作,以及两个数x和k。

这个操作有两个步骤:

首先把这个数列按照升序排序,然后把所有奇数位上的数字与x异或。

问执行k次操作之后,这个数列的最大值和最小值是多少。

思路:

由于每个数字异或两次之后会变回本身,所以猜测这个数列有可能会循环,所以就可以暴力计算周期,对于每一次出现的数列,看看是否与前面某次出现过的数列相同,这是暴力的思路。玄学复杂度。

更优雅的思路:

发现a[i]的最大值为1000,任意1000以内的两个数字异或不会超过1023,所以可以统计0到1023当中每个数字出现的次数,然后利用前缀和的思想,计算这个数字在下一次操作中对其他数字出现次数的贡献。

假设这个数字之前有n个数字出现,这个数字的出现次数为y,当前数字为cur,操作后的数列为nex:

1.当n为偶数,y为奇数:

nex[cur^x] += (y/2)+1;nex[cur] += y/2;

2.当n为偶数,y为偶数:

nex[cur^x] += (y/2);nex[cur] += y/2;

3.当n为奇数,y为奇数:

nex[cur^x] += (y/2);nex[cur] += y/2+1;

4.当n为奇数,y为偶数:

nex[cur^x] += (y/2);nex[cur] += y/2;

这个思路的复杂度为O(k*1024)。

思路1代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
using namespace std; const int N = 1e5 + ;
int a[N],b[][N]; int judge(int cu,int n)
{
for (int i = ;i < cu;i++)
{
bool f = ;
for (int j = ;j < n;j++)
{
if (b[i][j] != b[cu][j])
{
f = ;
break;
}
} if (!f) return i;
} return -;
} int main()
{
int n,k,x; scanf("%d%d%d",&n,&k,&x); for (int i = ;i < n;i++) scanf("%d",&a[i]);
for (int i = ;i < n;i++) b[][i] = a[i]; int cnt = ;
int pre = cnt-; for (;cnt <= k;cnt++)
{
for (int i = ;i < n;i++)
{
b[cnt][i] = b[cnt-][i];
} sort(b[cnt],b[cnt]+n); for (int i = ;i <= n;i+=)
{
b[cnt][i] ^= x;
} pre = judge(cnt,n); if (pre != -) break;
} if (cnt >= k)
{
sort(b[k],b[k]+n); printf("%d %d\n",b[k][n-],b[k][]);
}
else
{
int ti = cnt - pre; k = (k - pre) % ti; k += pre; sort(b[k],b[k]+n); printf("%d %d\n",b[k][n-],b[k][]);
} return ;
}

思路2代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int N = ; int a[N];
int pre[N];
int nex[N]; int main()
{
int n,k,x; scanf("%d%d%d",&n,&k,&x); for (int i = ;i < n;i++)
{
int y;
scanf("%d",&y);
a[y]++;
} for (int i = ;i < k;i++)
{
memset(nex,,sizeof(nex));
pre[] = ; for (int j = ;j < ;j++)
{
pre[j] = pre[j-] + a[j-];
} for (int j = ;j < ;j++)
{
if (a[j])
{
if (pre[j] % )
{
if (a[j] % )
{
nex[j^x] += a[j] / ;
nex[j] += a[j] / + ;
}
else
{
nex[j^x] += a[j] / ;
nex[j] += a[j] / ;
}
}
else
{
if (a[j] % )
{
nex[j^x] += (a[j]/) + ;
nex[j] += a[j]/;
}
else
{
nex[j^x] += a[j]/;
nex[j] += a[j]/;
}
}
}
} for (int j = ;j < ;j++)
{
a[j] = nex[j];
}
} int mx,mn; for (int i = ;i < ;i++)
{
if (a[i])
{
mn = i;
break;
}
} for (int i = ;i >= ;i--)
{
if (a[i])
{
mx = i;
break;
}
} printf("%d %d\n",mx,mn); return ;
}

codeforces 768c Jon Snow And His Favourite Number的更多相关文章

  1. CodeForces - 768C Jon Snow and his Favourite Number 桶排

    https://vjudge.net/problem/CodeForces-768C 题意:n个数,k次操作,x.每次操作先排序,再让奇数位置上的数据a[i]:=a[i] XOR x;   k< ...

  2. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C - Jon Snow and his Favourite Number

    地址:http://codeforces.com/contest/768/problem/C 题目: C. Jon Snow and his Favourite Number time limit p ...

  3. Codeforces768C Jon Snow and his Favourite Number 2017-02-21 22:24 130人阅读 评论(0) 收藏

    C. Jon Snow and his Favourite Number time limit per test 4 seconds memory limit per test 256 megabyt ...

  4. 【codeforces 768C】Jon Snow and his Favourite Number

    [题目链接]:http://codeforces.com/contest/768/problem/C [题意] 给你n个数字; 让你每次把这n个数字排序; 然后对奇数位的数字进行异或操作,然后对新生成 ...

  5. Jon Snow and his Favourite Number CodeForces - 768C (技巧)

    链接 题意 给定数组, 每次操作先将数组排序, 再将奇数位全部异或x, 求k次操作后数组最大值与最小值 (1 ≤ n ≤ 105, 0 ≤ k ≤ 105, 0 ≤ x ≤ 103) 题解 直接暴力模 ...

  6. codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)

    题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...

  7. 【基数排序】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C. Jon Snow and his Favourite Number

    发现值域很小,而且怎么异或都不会超过1023……然后可以使用类似基数排序的思想,每次扫一遍就行了. 复杂度O(k*1024). #include<cstdio> #include<c ...

  8. C. Jon Snow and his Favourite Number DP + 注意数值大小

    http://codeforces.com/contest/768/problem/C 这题的数值大小只有1000,那么可以联想到,用数值做数组的下标,就是类似于计数排序那样子.. 这样就可以枚举k次 ...

  9. Codeforces I. Producing Snow(优先队列)

    题目描述: C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. activeMQ配置文件

    <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agree ...

  2. 目标检测(3)-SPPNet

    Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 文章地址:https://arxiv.org ...

  3. linux 拷贝软连接文件

    cp -s sourchfile targetfile 这样拷贝软连接文件时,会将其对应指定路径同步修改,即便原来的软连接是相对路径也不会有问题.

  4. Oracle 11g服务详细介绍及哪些服务是必须开启的

    成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为: 1. Oracle ORCL VSS Writer Service: Oracle卷映射拷贝写入服务,VSS(Volume Sh ...

  5. 使用Bootstrap Popover实现一个弹框上三角形的代码记录

          $(function () {        var options = {          trigger: 'manual',          content: function ...

  6. finecms设置伪静态后分享到微信不能访问怎么处理

    finecms设置伪静态后分享到微信不能访问,分享的链接自动增加了一串参数,类似这样的***.html?from=singlemessage&isappinstalled=0,刚开始ytkah ...

  7. dbdeployer 快速安装MySQL8.0各测试环境

    Linux系统必须安装有Go语言: 下载最新的包:https://github.com/datacharmer/dbdeployer/releases     解压:  tar -xzf dbdepl ...

  8. 正交表和TCG的使用

    正交表法是一种有效减少测试用例个数的设计方法. 正交表法的依据是Galois理论,从大量的实验数据中挑选适量的.有代表性的点,从而合理的安排实验的一种科学实验设计方法.在测试用例的设计中,可以从大量的 ...

  9. C# List去重的三种方法

    三种去重的方法 1.List中的元素实现IEquatabe接口,并提供Equals方法和GetHashCode方法. 2.使用表达式 users.Where((x,i)=>users.FindI ...

  10. python的globals()

    以字典的形式返回当前位置的全局变量