CS Course HDU 6186

26 篇文章 0 订阅
2 篇文章 0 订阅

CS Course

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1964    Accepted Submission(s): 812


Problem Description
Little A has come to college and majored in Computer and Science.

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  , 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  .
 

Input
There are no more than 15 test cases.  

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.



Then n non-negative integers    follows in a line,    for each i in range[1,n].

After that there are q positive integers  in q lines,    for each i in range[1,q].
 

Output
For each query p, output three non-negative integers indicates the result of bit-operations(and, or, xor) of all non-negative integers except    in a line.
 

Sample Input

3 3

1 1 1

1

3

 

Sample Output

1 1 0
1 1 0
1 1 0
 

Source

题解:

这题就是要求一大堆数中除了某一个数的和、或、异或,如果根据我们最朴素的算法经验,如果存在 和、异或、或的反运算就可以直接全部算完了在对某一个数做反运算,但是发现只有异或存在反运算,与和或都不存在反运算,那么如果想不计算某一个数,只能把他前面的数和他后面的数分别做运算,然后再将这两堆数合并起来,类似于前缀和后缀和,但是这不能叫前缀和,得叫前缀或,前缀异或(自己瞎起的名字不要放在心上)

这题还有个小坑点,就是不能频繁的使用cin cout,虽然写起来的时候倒是爽了,但是会超时,一开始就是这么错的,感觉这么优秀的算法都超时不太符合社会主义核心价值观啊,后来改成scanf printf就行了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 100000
struct arr{
	int A,O,X;
} a[N+5],b[N+5];
int R[N+5];
int n,q;
int main(){
	//freopen("E.in","r",stdin);
	while(cin>>n>>q){
	for (int i=1;i<=n;i++)
		scanf("%d",&R[i]);

	a[1].A = R[1];
	a[1].O = R[1];
	a[1].X = R[1];
	b[n].A=R[n];
	b[n].O=R[n];
	b[n].X=R[n];
	for (int i=1;i<=n;i++){
		if (i>1){
			a[i].A=R[i] & a[i-1].A;
			a[i].O=R[i] | a[i-1].O;
			a[i].X=R[i] ^ a[i-1].X;
		}
		if (i<n){
			b[n-i].A=R[n-i] & b[n-i+1].A;
			b[n-i].O=R[n-i] | b[n-i+1].O;
			b[n-i].X=R[n-i] ^ b[n-i+1].X;
		}
	}
	for (int i=1;i<=q;i++){
		int k;
		cin >> k;
		if (k>1 && k<n) printf("%d %d %d\n" ,(a[k-1].A & b[k+1].A) ,(a[k-1].O | b[k+1].O),(a[k-1].X ^ b[k+1].X ));
		else if (k==1) printf("%d %d %d\n" ,b[k+1].A , b[k+1].O , b[k+1].X );
		else if (k==n) printf("%d %d %d\n",a[k-1].A,a[k-1].O,a[k-1].X );
	}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值