CodeForces - 1013B And 与运算暴力
题目链接:
https://vjudge.net/problem/1735275/origin
基本思路:
本题思路比较简单,首先,我们知道 a & x = b, b & x = b; 所以,一个数通过与运算改变只能改变一次!
所以,这里就有一种暴力的写法,三次for循环,时间复杂度是O(3n)。
第一次,从1到n遍历一遍,计算各个元素出现的次数,如果大于等于2,则输出0,结束。
第二次,从1到n遍历,如果存在 vis[ a[i]&k ] >= 2 并且 与k与运算后的值与之前的不同,即a[i] != a[i]&k,于是输出1,结束。
第三次,从1到n遍历,对a[i]&k的元素加一,即vis[ a[i]&k ]++,如果存在vis[ a[i]&k ] >= 2 && (a[i]&k) != a[i],则输出2,结束。
AC代码如下:
#include <iostream>
#include <cstdio> using namespace std;
const int MX = 1e5+;
int a[MX], vis[MX]; int main()
{
int n, k;
scanf("%d%d", &n, &k);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i <= n; ++i)
{
vis[ a[i] ]++; // 第一次统计一下元素的个数
if(vis[ a[i] ] >= )
{
printf("0\n");
return ;
}
}
for(int i = ; i <= n; ++i)
{
// 一次与运算有值并且这个与运算得到的结果和之前的值不同时则说明操作一次即可
if(vis[ a[i]&k ] == && a[i] != (a[i]&k)) // 注意要加括号!
{
printf("1\n");
return ;
}
}
for(int i = ; i <= n; ++i)
{
vis[ a[i]&k ]++; // 操作两次时直接遍历一遍
if(vis[ a[i]&k ] >= && a[i] != (a[i]&k)) // 这里要注意一下与运算重复的现象!
{
printf("2\n");
return ;
}
}
printf("-1\n");
return ;
}
第二种解法用到了flag标记。
第一种情况,未进行与运算就存在,则输出0。
第二种情况, 进行了一次与运算与之前元素重复,或者输入元素与之前与运算元素重复,则与对比1求最小值。
第三种情况, 与运算结果与之前与运算结果相同则与2对比得最小值。
下面是AC代码:
#include <iostream>
#include <cstdio> using namespace std;
const int INF = 0x3f3f3f3f;
const int MX = 1e5+;
bool flag[MX][]; // 一个数有两种状态! int main()
{
int ans = INF;
int n, k;
scanf("%d%d", &n, &k);
for(int i = ; i <= n; ++i)
{
int x;
scanf("%d", &x);
int y = x&k;
if(flag[x][]) // 未进行与运算就存在,则输出0
{
ans = min(ans, );
}
else if(flag[y][] || flag[x][]) //进行了一次与运算与之前元素重复,或者输入元素与之前与运算元素重复,则与对比1求最小值
{
ans = min(ans, );
}
else if(flag[y][]) // 与运算结果与之前与运算结果相同则与2对比得最小值
{
ans = min(ans, );
}
flag[x][] = true;
flag[y][] = true;
}
if(ans == INF) printf("-1\n"); // 都不符合作则输出-1
else printf("%d\n", ans);
return ;
}
如有疑问,欢迎评论指出!
CodeForces - 1013B And 与运算暴力的更多相关文章
- Codeforces 868D Huge Strings - 位运算 - 暴力
		You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ... 
- Codeforces Gym 100015H Hidden Code 暴力
		Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ... 
- Codeforces gym 100685 A. Ariel 暴力
		A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ... 
- Codeforces Gym 100637G G. #TheDress 暴力
		G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ... 
- [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力
		Couple Cover Time Limit: 3000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u Descri ... 
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
		D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ... 
- codeforces 650D D. Image Preview (暴力+二分+dp)
		题目链接: http://codeforces.com/contest/651/problem/D D. Image Preview time limit per test 1 second memo ... 
- Codeforces Gym 100203G Good elements 暴力乱搞
		原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ... 
- Codeforces 839D Winter is here - 暴力 - 容斥原理
		Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n s ... 
随机推荐
- 大数质因解:浅谈Miller-Rabin和Pollard-Rho算法
			2017-07-19 08:54 Amphetamine:能发一下代码吗? 应我那位谜一样好友的邀约,我打算好好看一看Miller-Rabin和Pollard-Rho算法.很奇怪,各种地方有很多代码描 ... 
- 洛谷 P1411 树
			最近在做些树形DP练练手 原题链接 大意就是给你一棵树,你可以断开任意数量的边,使得剩下的联通块大小乘积最大. 样例 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 输出 18 我首先想的 ... 
- Tomcat 日志文件分割
			新到公司, 拿到了前辈们留下的 程序 “病历书” , 上面记载了项目上的一些 经常会犯的毛病, 还有相应的解决方法. 其中有的是因为后台 代码逻辑上的一些原因 , N手代码通病了吧 (这个还是以后再 ... 
- np.array.all()和np.array.any()函数
			np.array.all()是对np.array中所有元素进行与操作,然后结果返回True或False np.array.any()是对np.array中所有元素进行或操作,然后结果返回True或Fa ... 
- POJ 3186 Treats for the Cows (动态规划)
			Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ... 
- JS学习笔记Day10
			一.设置或获取元素对象中(标签中)的属性和自定义属性 对象.属性 对象['属性'] 对象.getAttribute('属性名') 对象.setAttribute('属性名','属性值'); 对象.re ... 
- Pandas系列(十)-转换连接详解
			目录 1. 拼接 1.1 append 1.2 concat 2. 关联 2.1 merge 2.2 join 数据准备 # 导入相关库 import numpy as np import panda ... 
- Maven 学习总结  (四)之 测试
			使用Maven测试 Maven的重要职责之一就是自动运行单元测试.它通过maven-surefire-plugin与主流的单元测试框架JUnit3.JUnit4以及TestNG集成,并且能够自动生成丰 ... 
- JAVA实现C/S结构小程序
			程序功能: 客户端向服务器发送一个本地磁盘中的文件, 服务器程序接受后保存在其他位置. 客户端实现步骤: 创建一个客户端对象Socket,构造方法中绑定服务器的IP地址 和 端口号 使用Socket对 ... 
- opensuse 使用xx-net
			提示安装 [launcher][WARNING] import pynotify fail, please install python-notify if possible. gae_proxy][ ... 
