HDU 6186 CS Course
点击打开链接
CS Course
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1699 Accepted Submission(s): 726
Problem Description
Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.
Here is the problem:
You are giving n non-negative integers a1,a2,⋯,an, and some queries.
A query only contains a positive integer p, which means you
are asked to answer the result of bit-operations (and, or, xor) of all the integers except ap.
Input
Each test case begins with two positive integers n and p
in a line, indicate the number of positive integers and the number of queries.
2≤n,q≤105
Then n non-negative integers a1,a2,⋯,an follows in a line, 0≤ai≤109 for each i in range[1,n].
After that there are q positive integers p1,p2,⋯,pqin q lines, 1≤pi≤n for each i in range[1,q].
Output
题意:
给定n个数,然后是q次询问,每次询问为去掉第i个数,输出剩下n-1个数与、或、异或的结果
解法1:
前缀数组和后缀数组
用p1[i]表示 [1,i] 所有数与的结果,b1[i]表示 [i,n] 所有数与的结果
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int p1[maxn],b1[maxn];//and
int p2[maxn],b2[maxn];//or
int p3[maxn],b3[maxn];//xor
int main()
{ios::sync_with_stdio(0);int n,q,p;while(cin>>n>>q){cin>>a[1];p1[1]=p2[1]=p3[1]=a[1];for(int i=2;i<=n;i++){cin>>a[i];p1[i]=p1[i-1]&a[i];p2[i]=p2[i-1]|a[i];p3[i]=p3[i-1]^a[i];}b1[n]=b2[n]=b3[n]=a[n];for(int i=n-1;i>=1;i--){b1[i]=b1[i+1]&a[i];b2[i]=b2[i+1]|a[i];b3[i]=b3[i+1]^a[i];}while(q--){cin>>p;if(p==1)printf("%d %d %d\n",b1[p+1],b2[p+1],b3[p+1]);else if(p==n)printf("%d %d %d\n",p1[p-1],p2[p-1],p3[p-1]);elseprintf("%d %d %d\n",p1[p-1]&b1[p+1],p2[p-1]|b2[p+1],p3[p-1]^b3[p+1]);}}return 0;
}
解法2:
用num数组记录n个数转为二进制后,每一位上1的个数之和,
当去掉一个数后,更新num数组,然后遍历num数组
如果num[i]==n-1,那么这一位与的结果为1,否则为0(与运算:全1为1,有0为0)
如果num[i]>=1,那么这一位或的结果为1,否则为0,(或运算:全0为0,有1为1)
对于异或运算:
先让n个数异或的结果为XOR,然后XOR和去掉的数字异或,结果就是n-1个数字异或的结果(异或运算的逆运算)
#include<bits/stdc++.h>
#include<bitset>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int num[32];int main()
{ios::sync_with_stdio(0);int n,q,p;while(cin>>n>>q){int XOR=0;fill(num,num+32,0);for(int i=1;i<=n;i++){cin>>a[i];XOR^=a[i];bitset<32>bit(a[i]);for(int j=0;j<32;j++)if(bit[j]==1)num[j]++;}while(q--){cin>>p;bitset<32>bit(a[p]);bitset<32>AND;//n-1个数字与的结果bitset<32>OR;//n-1个数字或的结果for(int j=0;j<32;j++){if(num[j]-bit[j]==(n-1))//这一位有n-1个1,与的结果为1AND.set(j);//把第j位置1elseAND.reset(j);//把第j位置0if(num[j]-bit[j]>=1)//这一位上有1,或的结果为1OR.set(j);elseOR.reset(j);}printf("%d %d %d\n",AND,OR,XOR^a[p]);}}return 0;
}
HDU 6186 CS Course相关推荐
- HDU - 6186 CS Course(维护前缀+后缀)
题目链接:点击查看 题目大意:给出n个数以及m个查询,每个查询包括一个数字,要求输出除了该数字之外的位运算的"或和","与和"和"异或和" ...
- HDU 6186 CS Course(线段树区间操作)
Little A has come to college and majored in Computer and Science. Today he has learned bit-operation ...
- 【HDU - 6186】CS Course(按位与,按位或,按位异或的区间统计,二进制拆位)
题干: Little A has come to college and majored in Computer and Science. Today he has learned bit-opera ...
- HDU 6186 2017广西邀请赛:CS Course
题意: n个数,m次查询,每次给出一个p,求出除了第p个数以外其它所有数的且和,或和,异或和 统计下每个二进制位1的数量,例如z[5] = x表示有x个数第5个二进制位为1 之后每次查询只要check ...
- CS Course HDU - 6186
Little A has come to college and majored in Computer and Science. Today he has learned bit-operation ...
- 2017ACM/ICPC广西邀请赛
2017ACM/ICPC广西邀请赛(感谢广西大学) 题号 题目 考点 难度 A A Math Problem 数论 签到题 B Color it C Counting Stars D Covering ...
- hdu 1174:爆头(计算几何,三维叉积求点到线的距离)
爆头 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...
- poj 2096 , zoj 3329 , hdu 4035 —— 期望DP
题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...
- HDU 4685. Prince and Princess
链接 http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意 nnn 个王子,mmm 个公主,每个王子可以娶他喜欢的公主中的一个,每个公主只能嫁个一个王子, ...
最新文章
- 一文运维zookeeper
- OpenCV视频分析背景提取与前景提取
- HttpWebRequest采集读取网站挂载Cookie的通用方法
- Java与嵌入式数据库SQLite的结合
- tf.name_scope()和tf.variable_scope()
- IDEA简单配置教程
- 解密OpenShift内部通信网络
- 途家民宿4月26日后停止20城直营业务
- 计算机第二显示器黑屏的处理,电脑显示器黑屏的维修处理办法
- 使用jxls模板解析导出数据时添加单元格样式
- 2022软件测试技能 Fiddler HTTP抓包调试工具完整详细教程
- 突破某些网站限制只能由微信打开的尴尬场景
- Gerrit项目权限设置总结
- 身份证号码验证——C语小程序言
- jquery delay_jQuery delay()函数
- stc15f2k60s2.h
- Java| MANIFEST.MF讲解
- 苹果玩对峙2显示打不开服务器,苹果对峙2为什么进不去
- Matlab在一张图上画多条曲线或分别画
- 介绍理想工作计算机 英语作文,理想工作的英语作文6篇
热门文章
- 2018-06-25-Python全栈开发day21-part2-time模块介绍
- C#总结项目《影院售票系统》编写总结二
- 前端开发工具vue.js开发实践总结
- 自己封装一个MySignal函数,方便以后直接copy.
- [转载]ASP.NET开发经验积累
- vue商城项目源码_CMS全栈项目之Vue和React篇(下)(含源码)
- python read函数_Python read()函数:读入指定长度的文本
- js特效 在服务器显示变形,使一行文字变形产生弯曲弧度特效的jQuery插件 - Arctext.js...
- nginx https 访问http_Nginx之Http模块系列之访问控制模块
- nio java 内核拷贝_大文件拷贝,试试NIO的内存映射