POJ - 3657 Haybale Guessing(二分+并查集)
题意:有N个大小各不相同的点,给定Q个询问,格式为q1,q2,A,表示区间q1~q2的最小值是A,问第一个与之前询问结果出现冲突的询问。
分析:
1、二分询问的标号mid,查询1~mid是否出现询问冲突。
2、用并查集判断是否冲突。
3、已知若一区间的最小值是A,A>B,那么若这个区间的某一子区间为B,那么一定是矛盾的。
4、因此将前mid个询问按A从大到小排序,先染色区间里A值较大的区间,若A值较小的区间完全在已染色的区间内,则一定矛盾。
5、此外,由于N值各不相同,所以不可能出现两个彼此无交集的区间A值相同---Find(lr) < rl。
6、将某一区间染色后,这一区间的所有点以这一区间第一个点的前一个点为父亲。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 25000 + 10;
const int MAXT = 1000000 + 10;
using namespace std;
int N, Q;
int fa[MAXT];
struct Node{
int q1, q2, A;
void read(){
scanf("%d%d%d", &q1, &q2, &A);
}
bool operator < (const Node& rhs)const{
return A > rhs.A;
}
}num[MAXN], tmp[MAXN];
int Find(int x){
return fa[x] = (fa[x] == x) ? x : Find(fa[x]);
}
bool judge(int x){
for(int i = 0; i <= N; ++i) fa[i] = i;
for(int i = 1; i <= x; ++i){
tmp[i] = num[i];
}
sort(tmp + 1, tmp + x + 1);
for(int i = 1; i <= x;){
int ll, lr, rl, rr;
ll = rl = tmp[i].q1;
lr = rr = tmp[i].q2;
int j;
for(j = i + 1; j <= x && tmp[j].A == tmp[i].A; ++j){
ll = Min(ll, tmp[j].q1);
rl = Max(rl, tmp[j].q1);
lr = Min(lr, tmp[j].q2);
rr = Max(rr, tmp[j].q2);
}
i = j;
if(Find(lr) < rl) return false;
while(1){
int u = Find(rr);
if(u < ll) break;
fa[u] = ll - 1;
rr = u - 1;
}
}
return true;
}
int solve(){
int l = 1, r = Q;
while(l < r){
int mid = l + (r - l + 1) / 2;
if(judge(mid)) l = mid;
else r = mid - 1;
}
if(l == Q) return 0;
return l + 1;
}
int main(){
scanf("%d%d", &N, &Q);
for(int i = 1; i <= Q; ++i){
num[i].read();
}
printf("%d\n", solve());
return 0;
}
POJ - 3657 Haybale Guessing(二分+并查集)的更多相关文章
- POJ 3657 Haybale Guessing(区间染色 并查集)
Haybale Guessing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2384 Accepted: 645 D ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- 洛谷P2498 [SDOI2012]拯救小云公主 【二分 + 并查集】
题目 英雄又即将踏上拯救公主的道路-- 这次的拯救目标是--爱和正义的小云公主. 英雄来到boss的洞穴门口,他一下子就懵了,因为面前不只是一只boss,而是上千只boss.当英雄意识到自己还是等级1 ...
- 洛谷:P1783 海滩防御(二分+并查集 最短路 最小生成树)
题意: 给定长度为N的海滩,然后有M做防御塔,给出每座塔的位置Xi,到海岸的距离Yi. 求防御塔上最小观测半径Ri,使得海滩被封锁. 思路:要使左边界和右边界连通. 很nice,可以二分+并查集做. ...
- POJ2349二分+并查集,类似最小树的贪心
题意: 给你n个点,你的任务是构建一颗通讯树,然后给你一个s表示可以选出来s个点两两通讯不花钱,就是费用是0,其他的费用就是两点的距离,有个要求就是其他的费用中最大的那个最小. 思路: ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
随机推荐
- 拓扑排序--P2881 [USACO07MAR]排名的牛Ranking the Cows
*传送 FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛(1 ≤ N ≤ 1,000).FJ通过比较,已经知道了M(1 ≤ M ≤ 10,000)对相对关系.每一对关系表示为“X Y”,意指X的 ...
- eshop7-mysql
1. Mysql 安装 执行 yum -y install mysql-server 注意:(1)是否使用sudo 权限执行请根据您具体环境来决定 (2)检查是否已经安装mysql-server rp ...
- html5移动端主流适配方案
1.流式布局(百分比布局) 案例:京东移动端 优点:简单方便,只需要固定高度,宽度自适应: 缺点:大屏幕手机实际显示的不协调. 2.响应式布局 优点:可以节约成本,不用再做专门的web app网 ...
- leetcode102 Binary Tree Level Order Traversal
""" Given a binary tree, return the level order traversal of its nodes' values. (ie, ...
- c# GlobalAddAtom GlobalDeleteAtom
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Spring boot application.properties和 application.yml 初学者的学习
来自于java尚硅谷教程 简单的说这两个配置文件更改配置都可以更改默认设置的值比如服务器端口号之类的,只需再文件中设置即可, properties可能是出现的比较早了,如果你不调你的默认编码,中文可能 ...
- Maven的原理和使用
一.Maven能做什么 1.假设我们有10个项目,都需要引入spring core模块,那么需要十份重复的Spring Core.jar和commons-logging.jar 使用Maven:mav ...
- NO9 Linux快捷键整理及最常用命令
Linux快捷键整理及最常用命令 常用快捷键: Ctrl + u 删除光标之前到行首的字符 Ctrl + k 删除光标之前到行尾的字符 Ctrl + c ...
- HDU - 6205 card card card (尺取法)
题意:有n堆牌,ai表示每堆牌的牌数,bi表示每堆牌的penaltyvalue,操作开始前,可以重复进行将第一堆牌挪到最后一堆这一操作.然后,对于挪完后的牌,从第一堆开始,依次取.对于每一堆牌,首先将 ...
- UVA - 10305 Ordering Tasks(拓扑排序)
题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...