日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Haybale Guessing (POJ-3657)

發布時間:2025/3/17 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Haybale Guessing (POJ-3657) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

題意:給一段長度為n,每個位置上的數都不同的序列a[1..n]和問答(xi, yi, ri)代表RMQ(a, x, y)=r,求給出最早的有矛盾的那個問答的編號。

思路:

有矛盾的情況有兩種,一是之前已更新區間最小值為x,又要更新此區間或他的子區間的最小值為更小的數;二是兩段區間的最小值相同,但沒有交集。

先用并查集合并再查找有矛盾的情況,由于要求最小的序號,采用二分查找,從1枚舉到m。

Source Program

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 10000001 #define MOD 123 #define E 1e-6 using namespace std; struct Node {int x;int y;int minn; }a[N],b[N]; int father[N]; int ans; int n,m; int read() {int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;for(;isdigit(ch);ch=getchar())x=(x*2)+(x*8)+ch-'0';return x*f; } int Find(int x) {if(father[x]==x)return x;elsereturn father[x]=Find(father[x]); } bool cmp(Node a,Node b) {return a.minn>b.minn; } bool check(int pos) {for(int i=1;i<=n+1;i++)father[i]=i;for(int i=1;i<=pos;i++)b[i]=a[i];sort(b+1,b+pos+1,cmp);int lmin=b[1].x,lmax=b[1].x,rmin=b[1].y,rmax=b[1].y;for(int i=2;i<=pos;i++){if(b[i].minn<b[i-1].minn)//情況一{if(Find(lmax)>rmin)return true;for(int j=lmin;j<=rmax;j++){j=Find(j);father[j]=Find(rmax+1);}lmax=lmin=b[i].x;rmax=rmin=b[i].y;}else if(b[i].minn==b[i-1].minn)//情況二{lmax=max(lmax,b[i].x);lmin=min(lmin,b[i].x);rmax=max(rmax,b[i].y);rmin=min(rmin,b[i].y);if(lmax>rmin)return true;}}if(Find(lmax)>rmin)return true;elsereturn false; } int main() {n=read();m=read();for(int i=1;i<=m;i++){a[i].x=read();a[i].y=read();a[i].minn=read();}int l=1,r=m,mid;while(r-l>1){int mid=(l+r)/2;if(check(mid)){ans=mid;r=mid;}elsel=mid;}printf("%d\n",ans);return 0; }

?

總結

以上是生活随笔為你收集整理的Haybale Guessing (POJ-3657)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。