codeforces 768C Jon Snow and his Favourite Number 【思维】
来源:互联网 发布:java 日志等级 编辑:程序博客网 时间:2024/06/05 17:01
链接:
http://codeforces.com/contest/768/problem/C
题意:
现在要对一个长度为n的数组a[i]进行k次的增强。
每次增强是先对数组a进行排序再间隔的对数组a XOR x
题解:
一开始我是想先那几组数据测试一下每次操作后数组变成什么样,后来发现全部数据都出现了长度2的循环结,于是后来直接顺着这个思路写,后来一提交发现并不是所有数据都会出现长度为2的循环结。
后来参考了官方题解:a[i]中的数据经过操作不会超过1024,因为a[i]<=1000,x<=1000,并且每次操作都是先对数组进行排序再进行异或,所以可以根据数【0,1024】在数组中出现的次数sum[k]来写。假如一个数【0,k-1】在数组中出现了m次,设操作后的数出现次数为new[i],如果m为偶则对数k异或后,new[x^k] += (sum[k]+1)/2,new[k] += (sum[k]/2);若m为奇数new[x^k] += (sum[k]/2),new[k] += (sum[k]+1)/2
所以最后复杂度为O(1024*k)
#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stdlib.h>#include <string.h>#include<queue>#include<set>#include<map>#include<stack>#include<time.h>using namespace std;#define MAX_N 2050#define inf 0x3f3f3f3f#define LL long long#define ull unsigned long longconst LL INF = 9e18;const int mod = 4e4+7;typedef pair<int, int>P;int n, k, x;void cpy(int *a, int *b){ for(int i=0; i<=1024; i++) a[i] = b[i];}int main(){ cin >> n >> k >> x; int a[MAX_N]; int b[MAX_N]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); for(int i=0; i<n; i++) { int x; scanf("%d",&x); a[x]++; } for(int i=0; i<k; i++) { int sum = 0; memset(b, 0, sizeof(b)); for(int j=0; j<=1024; j++) { if(sum & 1) { b[j^x] += ((a[j]+0)/2); b[j] += ((a[j]+1)/2); } else { b[j^x] += ((a[j]+1)/2); b[j] += ((a[j]+0)/2); } sum += a[j]; } cpy(a, b); } int mn, mx; for(int i=0; i<=1024; i++) { if(a[i]) { mn = i; break; } } for(int i=1024; i>=0; i--) { if(a[i]) { mx = i; break; } } cout << mx << " " << mn << endl;}
0 0
- codeforces 768C Jon Snow and his Favourite Number 【思维】
- Codeforces 768C Jon Snow and his Favourite Number【思维】
- Codeforces 768C Jon Snow and his Favourite Number 构造
- Codeforces 768C Jon Snow and his Favourite Number
- 【codeforces 768C】Jon Snow and his Favourite Number
- CodeForces 768C Jon Snow and his Favourite Number
- Codeforces Round #399:C. Jon Snow and his Favourite Number
- C. Jon Snow and his Favourite Number
- codeforces 768 C Jon Snow and his Favourite Number(循环节)
- CodeForces 768C Jon Snow and his Favourite Number 暴力 空间换时间
- Codeforces768C Jon Snow and his Favourite Number
- Codeforces768C-Jon Snow and his Favourite Number
- Jon Snow and his Favourite Number
- Codeforces Round #399 (Div. 1 + Div. 2, combined)C. Jon Snow and his Favourite Number
- Codeforces 387C George and Number【思维】
- Codeforces 768D Jon and Orbs
- codeforces 768D Jon and Orbs
- Codeforces 768D Jon and Orbs (DP)
- Web前端-html页面-做过的一个公司的主页
- VirtualBox Genymotion 提示“无法创建虚拟电脑”
- linux内核驱动API指南(2)----驱动框架
- 服务器安装cuda安装文件选择
- wpf下拉框不能多选的原因
- codeforces 768C Jon Snow and his Favourite Number 【思维】
- 什么是TLB
- 【转载】DC/OS关键技术与应用场景
- Mysql笔记《2》
- Android--进程间通信的几种实现方式
- 【Java每日一题】20170222
- junit单元测试
- Android studio devolpment error
- 机器学习基石笔记--1