[USACO08JAN]Haybale Guessing(LuoguP2898)
The cows, who always have an inferiority complex about their intelligence, have a new guessing game to sharpen their brains.A designated 'Hay Cow' hides behind the barn and creates N (1 ≤ N ≤ 1,000,000) uniquely-sized stacks (conveniently numbered 1..N) of hay bales, each with 1..1,000,000,000 bales of hay.The other cows then ask the Hay Cow a series of Q (1 ≤ Q ≤ 25,000) questions about the the stacks, all having the same form:What is the smallest number of bales of any stack in the range of stack numbers Ql..Qh (1 ≤ Ql ≤ N; Ql ≤ Qh ≤ N)?The Hay Cow answers each of these queries with a single integer A whose truthfulness is not guaranteed.Help the other cows determine if the answers given by the Hay Cow are self-consistent or if certain answers contradict others.
并查集+二分。
有两种产生矛盾的情况。
题目中的两个元素的值没有任何数量是一样的,所以如果两个最小值相同的区间没有交集,则这两个区间产生矛盾。
如果一个区间包含一个最小值比其小的区间,则产生矛盾。
可以二分答案判断到此是否可行。
为方便处理矛盾,二分检查时我们按照预估最小值大到小排序。注意并查集合并区间的细节处理。当区间左右端点相同时,并查集找左右端点是显然会找到相同元素且这合法。并查集合并区间时,是将区间内的元素的父亲指向右端点的后一个元素,如果查询区间的右端点是本身,该点实际并未合并。其余情况如果要检查的区间被完全包含则存在矛盾。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rint register int
inline void getint(int &x)
{
char c;
for(c=getchar();c>'9'||c<'0';c=getchar());
for(x=0;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+c-'0';
}
const int MAXN=1000000+5;
struct Seg
{
int l,r,v;
}e[MAXN];
int lim,n;
int srt[MAXN],fa[MAXN];
bool cmp(int a,int b)
{
if(e[a].v==e[b].v)
return e[a].l==e[b].l?e[a].r<e[b].r:e[a].l<e[b].l;
return e[a].v>e[b].v;
}
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool check(int mid)
{
for(rint i=1;i<=mid;++i)
srt[i]=i;
for(rint i=1;i<=lim;++i)
fa[i]=i;
sort(srt+1,srt+mid+1,cmp);
int x,y;
for(int i=1;i<=mid;++i)
{
int t=e[srt[i]].v,l=e[srt[i]].l,
r=e[srt[i]].r,lm=e[srt[i]].l,rm=e[srt[i]].r;
while(i+1<=mid&&t==e[srt[i+1]].v)
{
++i;
if(e[srt[i]].l>r)
return 0;
l=max(l,e[srt[i]].l);
r=min(r,e[srt[i]].r);
lm=min(lm,e[srt[i]].l);
rm=max(rm,e[srt[i]].r);
}
int x=find(l),y=find(r);
if((l!=r&&x==y&&y!=r)||(l==r&&y!=r)) //注意此处判断
return 0;
x=lm,y=rm;
int rg=find(y+1);
while(rg!=find(x))
{
int fx=find(x);
fa[fx]=fa[fx+1];
}
}
return 1;
}
int main()
{
scanf("%d%d",&lim,&n);
for(int i=1;i<=n;++i)
getint(e[i].l),getint(e[i].r),getint(e[i].v);
int l=1,r=n+1;
while(r-l>1)
{
int mid=l+r>>1;
if(check(mid))
l=mid;
else r=mid;
}
printf("%d",l==n?0:l+1);
return 0;
}
[USACO08JAN]Haybale Guessing(LuoguP2898)的更多相关文章
- 题解—P2898 [USACO08JAN]Haybale Guessing G
pre 首先注意一下翻译里面并没有提到的一点,也是让我没看懂样例的一点,就是这个长度为 \(n\) 的数组里面的数各不相同. 有很多人用并查集写的这道题,题解里面也有一些用线段树写的,不过我认为我的做 ...
- 洛谷 P2898 [USACO08JAN]haybale猜测Haybale Guessing 解题报告
[USACO08JAN]haybale猜测Haybale Guessing 题目描述 给一段长度为\(n\),每个位置上的数都不同的序列\(a[1\dots n]\)和\(q\)和问答,每个问答是\( ...
- POJ 3657 Haybale Guessing(区间染色 并查集)
Haybale Guessing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2384 Accepted: 645 D ...
- Haybale Guessing
Haybale Guessing Time Limit: 1000MS Memory Limit: 65536K Description The cows, who always ha ...
- [USACO08JAN]haybale猜测Haybale Guessing
题目描述 The cows, who always have an inferiority complex about their intelligence, have a new guessing ...
- 【[USACO08JAN]haybale猜测Haybale Guessing】
抄题解.jpg 完全完全不会啊,这道题简直太神了 不过抄题解可真开心 首先这道题目保证了每一个位置上的数都是不同的,那么就能得到第一种判断不合法的方式 如果两个区间的最小值一样,但是两个区间的交集为空 ...
- [USACO 08JAN]Haybale Guessing
Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...
- poj-3657 Haybale Guessing(二分答案+并查集)
http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...
- POJ - 3657 Haybale Guessing(二分+并查集)
题意:有N个大小各不相同的点,给定Q个询问,格式为q1,q2,A,表示区间q1~q2的最小值是A,问第一个与之前询问结果出现冲突的询问. 分析: 1.二分询问的标号mid,查询1~mid是否出现询问冲 ...
随机推荐
- PAT (Basic Level) Practice (中文)1021 个位数统计 (15 分)
给定一个 k 位整数 1 (0, ,, dk−1>0),请编写程序统计每种不同的个位数字出现的次数.例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3. 输入格式: 每个输入包含 ...
- Selenium3+python自动化016-Selenium Grid
一.Selenium Grid介绍 1.概念 Selenium Grid组件专门用于远程分布式测试或并发测试,通过并发执行测试用例的方式可以提高测试用例的执行速度和效率,解决界面自动化测试执行速度过慢 ...
- springboot~gradle4.7之后的lombok引用方法
在gradle4.7以后对于加入依赖lombok方式发生变化,gradle4.7版本以前,可以直接如下引用: compile("org.projectlombok:lombok:1.18.2 ...
- C++->以读或写方式打开一个文件
以读或写方式打开一个文件 #include<iostream.h> //.h以C|非C标准引用库文件 #include<fstream.h> #include<std ...
- 怎么压缩PPT大小?
PPT体积过大有几个原因: 1.母版内版式过多.解决方法:①点击“视图”选项卡下的“幻灯片母版”:②删除左边没必要的版式. 2.图片质量太大.解决方法:①选中任意一张图片:②点击“图片工具”的“格式” ...
- [CF2B] The least round way - dp
给定由非负整数组成的n×n 的正方形矩阵,你需要寻找一条路径: 以左上角为起点 每次只能向右或向下走 以右下角为终点 并且,如果我们把沿路遇到的数进行相乘,积应当是最小"round" ...
- Selenium3+python自动化009- 多选框
多选框 # 随机选择多选框# sports=driver.find_elements_by_name("sport")# maxnum=len(sports)# num=rando ...
- Spring Boot整合Dubbo2.x,解决其中遇到的坑
Dubbo了解 a high-performance, java based open source RPC framework. Dubbo官网 源码 文档 快速知道用法 本地服务 Spring 配 ...
- 三分钟快速上手TensorFlow 2.0 (后续)——扩展和附录
TensorFlow Hub 模型复用 TF Hub 网站 打开主页 https://tfhub.dev/ ,在左侧有 Text.Image.Video 和 Publishers 等选项,可以选取关注 ...
- Python爬虫连载8-JS加密(一)
一.JS加密 1.有的反爬虫策略采用js对需要传输的数据进行加密处理. 2.经过加密,传输的就是密文 3.加密函数或者过程一定是在浏览器完成,也就是一定会把代码(js代码)暴露给使用者 4.通多阅读加 ...