Haybale Guessing

Haybale Guessing
Time Limit: 1000MS Memory Limit: 65536K
   

Description

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 Q l.. Qh (1 ≤ Q lN; Q lQhN)?

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.

Input

* 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

Output

* 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.

Sample Input

20 4
1 10 7
5 19 7
3 12 8
11 15 12

Sample Output

3
分析:二分答案,然后按A从大到小遍历;
   对于相同的值A,判断区间交是否成立,然后覆盖区间并;
   覆盖的过程可以用线段树或并查集实现;
   注意离散化过程排除[a,b]!=[a,a]U[b,b](a>b+1);
   可以排序后再在相邻的差值>1的点之间插值;

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e6+10;
const int N=2e5+10;
using namespace std;
int id(int l,int r){return l+r|l!=r;}
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,mi[maxn<<1],tag[maxn<<1],q,ql[maxn],qr[maxn],qt[maxn],ip[maxn],cnt;
double a[maxn];
bool cmp(int x,int y){return qt[x]>qt[y];}
void pdw(int x,int y,int rt)
{
    int mid=x+y>>1,ls=id(x,mid),rs=id(mid+1,y);
    mi[ls]=mi[rs]=tag[rt];
    tag[ls]=tag[rs]=tag[rt];
    tag[rt]=0;
}
void pup(int x,int y,int rt)
{
    int mid=x+y>>1;
    mi[rt]=min(mi[id(x,mid)],mi[id(mid+1,y)]);
}
void init(int l,int r,int rt)
{
    mi[rt]=0;
    tag[rt]=0;
    if(l==r)return;
    int mid=l+r>>1;
    init(l,mid,id(l,mid));
    init(mid+1,r,id(mid+1,r));
}
void upd(int x,int y,int z,int l,int r,int rt)
{
    if(x==l&&y==r)
    {
        mi[rt]=z;
        tag[rt]=z;
        return;
    }
    int mid=l+r>>1;
    if(tag[rt])pdw(l,r,rt);
    if(y<=mid)upd(x,y,z,l,mid,id(l,mid));
    else if(x>mid)upd(x,y,z,mid+1,r,id(mid+1,r));
    else upd(x,mid,z,l,mid,id(l,mid)),upd(mid+1,y,z,mid+1,r,id(mid+1,r));
    pup(l,r,rt);
}
int gao(int x,int y,int l,int r,int rt)
{
    if(x==l&&y==r)return mi[rt];
    int mid=l+r>>1;
    if(tag[rt])pdw(l,r,rt);
    if(y<=mid)return gao(x,y,l,mid,id(l,mid));
    else if(x>mid)return gao(x,y,mid+1,r,id(mid+1,r));
    else return min(gao(x,mid,l,mid,id(l,mid)),gao(mid+1,y,mid+1,r,id(mid+1,r)));
}
bool ok(int x)
{
    int i,j;
    init(1,cnt,id(1,cnt));
    rep(i,1,x)ip[i]=i;
    sort(ip+1,ip+x+1,cmp);
    for(i=1;i<=x;)
    {
        int l=ql[ip[i]],r=qr[ip[i]],xl=l,xr=r;
        j=i;
        while(j+1<=x&&qt[ip[j+1]]==qt[ip[i]])l=max(l,ql[ip[j+1]]),r=min(r,qr[ip[j+1]]),xl=min(xl,ql[ip[j+1]]),xr=max(xr,qr[ip[j+1]]),j++;
        if(l>r)return false;
        if(gao(l,r,1,cnt,id(1,cnt))>qt[ip[i]])return false;
        upd(xl,xr,qt[ip[i]],1,cnt,id(1,cnt));
        i=j+1;
    }
    return true;
}
int main()
{
    int i,j;
    scanf("%d%d",&n,&q);
    rep(i,1,q)
    {
        scanf("%d%d%d",&ql[i],&qr[i],&qt[i]);
        if(ql[i]>qr[i])swap(ql[i],qr[i]);
        a[++cnt]=ql[i],a[++cnt]=qr[i];
    }
    sort(a+1,a+cnt+1);
    for(i=cnt;i>=1;i--)if(a[i]>a[i-1]+1)a[++cnt]=a[i-1]+1;
    sort(a+1,a+cnt+1);
    cnt=unique(a+1,a+cnt+1)-a-1;
    rep(i,1,q)
    {
        ql[i]=lower_bound(a+1,a+cnt+1,ql[i])-a;
        qr[i]=lower_bound(a+1,a+cnt+1,qr[i])-a;
    }
    int l=1,r=q,ret;
    while(l<=r)
    {
        int mid=l+r>>1;
        if(ok(mid))ret=mid,l=mid+1;
        else r=mid-1;
    }
    assert(ret>=1&&ret<=q);
    printf("%d\n",ret+1<=q?ret+1:0);
    return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/6666243.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that! Here's an example implementation of the GuessingGame in Java: ```java // GuessingGame.java import java.util.Scanner; public class GuessingGame { private String secretWord; private int guessesLeft; public GuessingGame(String secretWord, int maxGuesses) { this.secretWord = secretWord; this.guessesLeft = maxGuesses; } public void play() { String guessedWord = ""; for (int i = 0; i < secretWord.length(); i++) { guessedWord += "*"; } Scanner scanner = new Scanner(System.in); while (guessesLeft > 0) { System.out.println("Guess a letter (" + guessesLeft + " guesses left):"); String guess = scanner.nextLine(); // Check if guess is correct boolean correctGuess = false; for (int i = 0; i < secretWord.length(); i++) { if (secretWord.charAt(i) == guess.charAt(0)) { guessedWord = guessedWord.substring(0, i) + guess + guessedWord.substring(i + 1); correctGuess = true; } } if (!correctGuess) { guessesLeft--; } System.out.println("Guessed word: " + guessedWord); if (guessedWord.equals(secretWord)) { System.out.println("Congratulations, you guessed the word!"); return; } } System.out.println("Sorry, you ran out of guesses. The word was " + secretWord); } } ``` And here's an example usage in a main class: ```java // Main.java public class Main { public static void main(String[] args) { GuessingGame game = new GuessingGame("house", 10); game.play(); } } ``` In this implementation, the `GuessingGame` class takes two parameters: the secret word to guess, and the maximum number of guesses allowed. The `play` method runs the game loop, prompting the user for guesses and updating the guessed word accordingly. If the guessed word matches the secret word, the game is won; if the user runs out of guesses, the game is lost.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值