当前位置:网站首页>2019陕西省省赛J-位运算+贪心
2019陕西省省赛J-位运算+贪心
2022-07-25 15:23:00 【塔子哥来了】
题目大意:
给你 n n n个区间,你需要从每个区间中取出一个数。使得他们按位与后最大。
题目思路:
从高位开始尝试置1.然后check每个区间。
我将问题转化为:找到小于 R R R的最大的包含当前 a n s ans ans的数 X X X.然后check它和 L L L的大小关系.
这样就从 区间存在性问题 转化为: 最值问题.这个时候同样贪心做即可.
这样还是带两个log,需要在两处加剪枝,最后时间从T优化为277ms
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define fi first
#define se second
const int maxn = 1e5 + 5;
const int maxb = 30;
const int mod = 1e9 + 7;
struct Node {
int first , second;
}a[maxn];
int bk[maxn] , u , v , tg[maxn];
inline int calc (int up , int x , int id){
if ((up & x) == x) return up; // 剪枝1:防止全1(或很多1)的up出现
for (int i = maxb ; i >= 0 ; i--){
u = (up >> i) & 1;
v = (x >> i) & 1;
if (u == v) continue;
if ((x | (1 << i)) > up) {
// 这种情况代表这个区间往后都不用再check了.
tg[id] = true;
return x | ((1 << i) - 1);
}
x |= (1 << i);
}
assert(x == up);
return x;
}
template <typename T>
void read(T & x){
x = 0;T f = 1;char ch = getchar();while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();}while (isdigit(ch)){
x = x * 10 + (ch ^ 48);ch = getchar();}x *= f;}
template <typename T>
void write(T x){
if(x < 0){
putchar('-');x = -x;}if (x > 9)write(x / 10);putchar(x % 10 + '0');}
int main()
{
int t; read(t);
while (t--){
int n ; read(n);
for (int i = 1 ; i <= n ; i++){
read(a[i].first);
read(a[i].second);
bk[i] = false;
}
int ans = 0;
for (int i = maxb ; i >= 0 ; i--){
ll tmp = ans | (1 << i);
bool ok = true;
for (int j = 1 ; j <= n && ok; j++){
if (bk[j]) continue;
if (tmp > a[j].second) ok = false;
tg[j] = false;
int mx = calc (a[j].second , tmp , j);
if (mx < a[j].first) ok = false;
}
if (ok) {
ans = tmp;
for (int j = 1 ; j <= n ; j++) if (tg[j]) bk[j] = true;
}
}
write(ans);
puts("");
}
return 0;
}
边栏推荐
- How to understand the maximum allowable number of errors per client connection of MySQL parameters in Seata?
- Instance Tunnel 使用
- The implementation process of inheritance and the difference between Es5 and ES6 implementation
- C#精挑整理知识要点10 泛型(建议收藏)
- 从 join on 和 where 执行顺序认识T-sql查询执行顺序
- Ml speech depth neural network model
- Reflection - Notes
- Delayed loading source code analysis:
- Application of object detection based on OpenCV and yolov3
- ios 面试题
猜你喜欢

你准备好脱离“内卷化怪圈”了吗?

How to solve the problem of scanf compilation error in Visual Studio

Yan required executor memory is above the max threshold (8192mb) of this cluster!

谷歌云盘如何关联Google Colab

What is the Internet of things

盒子躲避鼠标

为什么PrepareStatement性能更好更安全?

How to solve the login problem after the 30 day experience period of visual stuido2019

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac

Application of object detection based on OpenCV and yolov3
随机推荐
Single or multiple human posture estimation using openpose
带你详细认识JS基础语法(建议收藏)
苹果内购和Apple Pay 的区别
My creation anniversary
How to solve the login problem after the 30 day experience period of visual stuido2019
记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
MySQL heap table_ MySQL memory table heap Usage Summary - Ninth Five Year Plan small pang
单例模式3--单例模式
Record a redis timeout
Word 样式模板复制到另一文档
BPSK调制系统MATLAB仿真实现(1)
How spark gets columns in dataframe --column, $, column, apply
ML - 语音 - 传统语音模型
SVD奇异值分解推导及应用与信号恢复
matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
你准备好脱离“内卷化怪圈”了吗?
从 join on 和 where 执行顺序认识T-sql查询执行顺序
Graph theory and concept
如何更新更新数据库中的json值?
盒子躲避鼠标