[USACO08JAN]haybale猜测Haybale Guessing
题目描述
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.
给一段长度为n,每个位置上的数都不同的序列a[1..n]和q和问答,每个问答是(x, y, r)代表RMQ(a, x, y) = r, 要你给出最早的有矛盾的那个问答的编号。
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and Q
- Lines 2..Q+1: Each line contains three space-separated integers that represent a single query and its reply: Ql, Qh, and A
输出格式:
- Line 1: Print the single integer 0 if there are no inconsistencies
among the replies (i.e., if there exists a valid realization of the hay
stacks that agrees with all Q queries). Otherwise, print the index from
1..Q of the earliest query whose answer is inconsistent with the answers
to the queries before it.
输入输出样例
3
以下题解摘自洛谷题解,非常清楚
出现矛盾的区间符合两个条件之一:
1.题目中的两个干草堆没有任何数量是一样的,所以如果两个区间没有交集并且它们的最小值相同,则这两个区间产生矛盾
2.如果一个区间包含另一个区间,被包含的区间的最小值大于另一个区间,则两个区间产生矛盾
考虑对原先问答的顺序进行二分答案,对于一个二分出的mid作如下处理:
为了方便处理矛盾2,将从1到mid的每个区间的值按照从大到小进行排序
对于值相同的区间,求出并集和交集的范围,如果不存在并集,则mid不可行
维护一颗线段树,将交集的区间覆盖为1
查询并集的区间是否被覆盖为1,如果是,则mid不可行
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Ask
{
int l,r,x;
}a[],b[];
int c[],n,q;
bool cmp(Ask a,Ask b)
{
return a.x>b.x;
}
void build(int rt,int l,int r)
{
if (l==r)
{
c[rt]=;
return;
}
int mid=(l+r)/;
build(rt*,l,mid);
build(rt*+,mid+,r);
c[rt]=c[rt*]&c[rt*+];
}
void pushdown(int rt)
{
if (c[rt])
{
c[rt*]=c[rt];
c[rt*+]=c[rt];
}
}
void update(int rt,int l,int r,int L,int R)
{
if (l>=L&&r<=R)
{
c[rt]=;
return;
}
int mid=(l+r)/;
pushdown(rt);
if (L<=mid) update(rt*,l,mid,L,R);
if (R>mid) update(rt*+,mid+,r,L,R);
c[rt]=c[rt*]&c[rt*+];
}
int query(int rt,int l,int r,int L,int R)
{
if (c[rt]) return ;
if (l>=L&&r<=R)
{
return c[rt];
}
int mid=(l+r)/;
int ll=,rr=;
if (L<=mid) ll=query(rt*,l,mid,L,R);
if (R>mid) rr=query(rt*+,mid+,r,L,R);
c[rt]=c[rt*]&c[rt*+];
return ll&rr;
}
bool check(int mid)
{int i,j,l1,l2,r1,r2,k;
for (i=;i<=mid;i++)
b[i]=a[i];
build(,,n);
sort(b+,b+mid+,cmp);
for (i=;i<=mid;i=j)
{
j=i;
while (j<=mid&&b[j].x==b[i].x) j++;
l1=2e9;r2=2e9;l2=-;r1=-;
for (k=i;k<j;k++)
{
l1=min(l1,b[k].l);
r1=max(r1,b[k].r);
l2=max(l2,b[k].l);
r2=min(r2,b[k].r);
}
if (l2>r2) return ;
if (query(,,n,l2,r2)) return ;
update(,,n,l1,r1);
}
return ;
}
int main()
{int i;
cin>>n>>q;
for (i=;i<=q;i++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].x);
}
int l=,r=q,ans=;
while (l<=r)
{
int mid=(l+r)/;
if (check(mid))
{
ans=mid;
l=mid+;
}
else r=mid-;
}
cout<<(ans+)%(q+);
}
[USACO08JAN]haybale猜测Haybale Guessing的更多相关文章
- 洛谷 P2898 [USACO08JAN]haybale猜测Haybale Guessing 解题报告
[USACO08JAN]haybale猜测Haybale Guessing 题目描述 给一段长度为\(n\),每个位置上的数都不同的序列\(a[1\dots n]\)和\(q\)和问答,每个问答是\( ...
- 【[USACO08JAN]haybale猜测Haybale Guessing】
抄题解.jpg 完全完全不会啊,这道题简直太神了 不过抄题解可真开心 首先这道题目保证了每一个位置上的数都是不同的,那么就能得到第一种判断不合法的方式 如果两个区间的最小值一样,但是两个区间的交集为空 ...
- python猜数脚本(电脑猜测)(二分法)
# coding=utf-8# 猜数# 记录猜数的过程import randomcom_result=[] #存放电脑结果,数组com_count=0 #存放电脑猜测次数ran=random.ran ...
- [USACO08JAN]Haybale Guessing(LuoguP2898)
The cows, who always have an inferiority complex about their intelligence, have a new guessing game ...
- 题解—P2898 [USACO08JAN]Haybale Guessing G
pre 首先注意一下翻译里面并没有提到的一点,也是让我没看懂样例的一点,就是这个长度为 \(n\) 的数组里面的数各不相同. 有很多人用并查集写的这道题,题解里面也有一些用线段树写的,不过我认为我的做 ...
- 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 ...
- [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 ...
随机推荐
- 用jQuery.delegate()将事件绑定在父元素上面
1.先看看官方的示例: <html> <head> <script type="text/javascript" src="/jquery/ ...
- bug终结者 团队作业第四、五周
bug终结者 团队作业第四.五周 博客编辑者:20162322朱娅霖 本周学习任务: 团队协作完成<需求规格说明书> 工作流程 第四周 团队成员各自完成蓝墨云班课中<需求规格说明书& ...
- 201621123057 《Java程序设计》第7周学习总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 答: ...
- Python 图片转字符画
Python 图片转字符画 一.课程介绍 1. 课程来源 原创 2. 内容简介 本课程讲述怎样使用 Python 将图片转为字符画 3. 前置课程 Python编程语言 Linux 基础入门(新版) ...
- Twisted 延迟调用
延迟(defer)是twisted框架中实现异步的编程体系,使程序设计可以采用事件驱动的机制 1.基本使用 defer可以看作一个管理回调函数的对象,可以向该对象添加需要的回调函数同时也可以指定该组函 ...
- initializer element is not a compile-time constant
初始化一个全局变量或static变量时,只能用常量赋值,不能用变量赋值! 如下就会报这个错误(KUIScreenWidth)是变量 static CGFloat const topButtonWidt ...
- 深入浅出 SSL 管理配置实战
我们生活在一个信息大爆炸的时代,几乎每天都在和互联网打交道,购物.网银转账.支付宝付款.搜索信息.查看邮件.观看视频.微信聊天.上网冲浪.阅读新闻等,无不时时刻刻在和网络打交道.那如何保护网络安全就相 ...
- 识别图片中文字(百度AI)
这个是百度官方的文档 https://ai.baidu.com/docs#/OCR-API/top 通用的文字识别,如果是其他的含生僻字/含位置信息的版本,请参考官方的文档,只 ...
- 帧动画的创建方式 - 纯Java代码方式
废话不多说,先看东西 帧动画的创建方式主要以下2种: * 用xml创建动画: * 纯Java代码创建动画: 本文内容主要关注 纯java代码创建帧动画 的方式: 用xml创建帧动画:http:// ...
- python 面向对象之多态与绑定方法
多态与多态性 一,多态 1,多态指的是一类事物有多种形态(python里面原生多态) 1.1动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.AB ...