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 ...
随机推荐
- Mysql注入小tips --持续更新中
学习Web安全好几年了,接触最多的是Sql注入,一直最不熟悉的也是Sql注入.OWASP中,Sql注入危害绝对是Top1.花了一点时间研究了下Mysql类型的注入. 文章中的tips将会持续更新,先说 ...
- B-树(B树)详解
具体讲解之前,有一点,再次强调下:B-树,即为B树.因为B树的原英文名称为B-tree,而国内很多人喜欢把B-tree译作B-树,其实,这是个非常不好的直译,很容易让人产生误解.如人们可能会以为B-树 ...
- MTD下的Nand驱动
目录 MTD下的Nand驱动 引入 平台设备资源文件 关键数据结构 平台框架 s3c24xx_nand_probe nand_scan s3c2410_nand_add_partition add_m ...
- 轴对称 Navier-Stokes 方程组的点态正则性准则 II
在 [Wei, Dongyi. Regularity criterion to the axially symmetric Navier-Stokes equations. J. Math. Anal ...
- [物理学与PDEs]第1章习题2 均匀带电球面的电场强度与电势
设有一均匀分布着电荷的半径为 $R$ 的球面, 其电荷密度 (即单位面积上的电荷量) 为 $\sigma$. 试求该球面所形成电场的电场强度及电势. 解答: 设 $P$ 距圆心的距离为 $r$, 不妨 ...
- Groovy 设计模式 -- 责任链模式
Chain of Responsibility Pattern http://groovy-lang.org/design-patterns.html#_chain_of_responsibility ...
- java8 按条件过滤集合
//黄色部分为过滤条件list.stream().filter(user-> user.getId() > 5 && "1组".equals(user. ...
- Java8从对象列表中取出某个属性的列表
List<属性值类型> 属性List = 对象List.stream().map(对象::get方法()).collect(Collectors.toList()); 例如: List&l ...
- Nginx--服务部署、基于域名的虚拟主机配置
一.服务部署 1.预处理 安装CentOS ,配置hosts.静态IP.设置必要的安全参数等(略) 1-1.系统环境 [root@vnx ~]# cat /etc/redhat-release Cen ...
- 关于stm32的数据类型
常见的uint16_t.uint32_t.u8.u16等 定义在stm32f10x.h文件中,这个文件是定义相关数据类型和结构体的头文件.