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 ...
随机推荐
- elasticsearch更改mapping(不停服务重建索引)
转载地址:http://donlianli.iteye.com/blog/1924721?utm_source=tuicool&utm_medium=referral Elasticsearc ...
- Mock5 moco框架中post请求如何加入cookies
接着Mock4中的json文件,再往里面添加一个post 请求. 前面写法不变,后面的请求数据用的是json关键字,返回的response也是json的格式 [ { "description ...
- 第八节:常见安全隐患和传统的基于Session和Token的安全校验
一. 常见的安全隐患 1. SQL注入 常见的案例: String query = "SELECT * FROM T_User WHERE userID='" + Request ...
- SQL注入绕过技巧总结
1.SQL注入过程中的处理# 终端payload编码------>web服务器解码-------->CGI脚本解码------>web应用解码----->数据库解码 浏览器.代 ...
- bilibili存储型xss (绕过长度限制打乱顺序限制)
在个人空间的我的收藏中可编辑视频收藏的名称,之后尝试写入标签. http://space.bilibili.com/ 发现输出到前端的尖括号被转义了,不过出现了一个json接口,他的Content-T ...
- FM(工程实现)
摘自: https://www.cnblogs.com/AndyJee/p/8032553.html 一.FM模型函数 二.FM对参数求导结果 三.算法实现 主要超参数有:初始化参数.学习率.正则化稀 ...
- JS获取当月第一天和最后一天
/** * 获取当前月的第一天 */function getCurrentMonthFirst(){ var date=new Date(); date.setDate(1); return date ...
- Linux设置SSH登录(SecureCrt)
背景 每次登录需要输入复杂的密码,而且不停的有人在尝试登录root账户.感觉心慌慌,所以不得不设置更加安全的登录方式. 配置SSH无密码登录需要4步 准备工作 生成公钥和私钥 导入公钥到认证文件,更改 ...
- PHP content-type为"application/json"的post过来的数据$_POST接受不到的问题
ajax默认是以application/x-www-form-urlencoded方式提交.也就是常见的表单提交方式.在PHP中使用$_POST方式可以轻松获取. 但如果将ajax的请求头强制指定为a ...
- Mac ---- markdown 转 html\word\pdf
在Mac上,有一个软件,叫iA writer,是一个文字编辑器,可以进行md到word的转换,但它是收费的,RMB68元. 如果只是临时用一下,不想购买,你可以使用pandoc. 在mac下,使用方法 ...