[USACO08JAN]haybale猜测Haybale Guessing

本文介绍了一种通过查询干草堆中最小值来检验答案一致性的算法问题。利用区间查询和线段树实现,解决干草堆游戏中可能出现的矛盾情况。通过对问题的巧妙转化和高效的数据结构应用,确保了算法的有效性和快速响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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.

输入输出样例

输入样例#1: 复制
20 4
1 10 7
5 19 7
3 12 8
11 15 12
输出样例#1: 复制
3

以下题解摘自洛谷题解,非常清楚

出现矛盾的区间符合两个条件之一:

1.题目中的两个干草堆没有任何数量是一样的,所以如果两个区间没有交集并且它们的最小值相同,则这两个区间产生矛盾

2.如果一个区间包含另一个区间,被包含的区间的最小值大于另一个区间,则两个区间产生矛盾

考虑对原先问答的顺序进行二分答案,对于一个二分出的mid作如下处理:

为了方便处理矛盾2,将从1到mid的每个区间的值按照从大到小进行排序

对于值相同的区间,求出并集和交集的范围,如果不存在并集,则mid不可行

维护一颗线段树,将交集的区间覆盖为1

查询并集的区间是否被覆盖为1,如果是,则mid不可行

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 using namespace std;
  7 struct Ask
  8 {
  9   int l,r,x;
 10 }a[25001],b[25001];
 11 int c[4000001],n,q;
 12 bool cmp(Ask a,Ask b)
 13 {
 14   return a.x>b.x;
 15 }
 16 void build(int rt,int l,int r)
 17 {
 18   if (l==r)
 19     {
 20       c[rt]=0;
 21       return;
 22     }
 23   int mid=(l+r)/2;
 24   build(rt*2,l,mid);
 25   build(rt*2+1,mid+1,r);
 26   c[rt]=c[rt*2]&c[rt*2+1];
 27 }
 28 void pushdown(int rt)
 29 {
 30   if (c[rt])
 31     {
 32       c[rt*2]=c[rt];
 33       c[rt*2+1]=c[rt];
 34     }
 35 }
 36 void update(int rt,int l,int r,int L,int R)
 37 {
 38   if (l>=L&&r<=R)
 39     {
 40       c[rt]=1;
 41       return;
 42     }
 43   int mid=(l+r)/2;
 44   pushdown(rt);
 45   if (L<=mid) update(rt*2,l,mid,L,R);
 46   if (R>mid) update(rt*2+1,mid+1,r,L,R);
 47   c[rt]=c[rt*2]&c[rt*2+1];
 48 }
 49 int query(int rt,int l,int r,int L,int R)
 50 {
 51   if (c[rt]) return 1;
 52   if (l>=L&&r<=R)
 53     {
 54       return c[rt];
 55     }
 56   int mid=(l+r)/2;
 57   int ll=1,rr=1;
 58   if (L<=mid) ll=query(rt*2,l,mid,L,R);
 59   if (R>mid) rr=query(rt*2+1,mid+1,r,L,R);
 60   c[rt]=c[rt*2]&c[rt*2+1];
 61   return ll&rr;
 62 }
 63 bool check(int mid)
 64 {int i,j,l1,l2,r1,r2,k;
 65   for (i=1;i<=mid;i++)
 66     b[i]=a[i];
 67   build(1,1,n);
 68   sort(b+1,b+mid+1,cmp);
 69   for (i=1;i<=mid;i=j)
 70     {
 71       j=i;
 72       while (j<=mid&&b[j].x==b[i].x) j++;
 73       l1=2e9;r2=2e9;l2=-1;r1=-1;
 74       for (k=i;k<j;k++)
 75     {
 76       l1=min(l1,b[k].l);
 77       r1=max(r1,b[k].r);
 78       l2=max(l2,b[k].l);
 79       r2=min(r2,b[k].r);
 80     }
 81       if (l2>r2) return 0;
 82       if (query(1,1,n,l2,r2)) return 0;
 83       update(1,1,n,l1,r1); 
 84     }
 85   return 1;
 86 }
 87 int main()
 88 {int i;
 89   cin>>n>>q;
 90   for (i=1;i<=q;i++)
 91     {
 92       scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].x);
 93     }
 94   int l=1,r=q,ans=0;
 95   while (l<=r)
 96     {
 97       int mid=(l+r)/2;
 98       if (check(mid)) 
 99     {
100       ans=mid;
101       l=mid+1;
102     }
103       else r=mid-1;
104     }
105   cout<<(ans+1)%(q+1);
106 }

 

转载于:https://www.cnblogs.com/Y-E-T-I/p/7743999.html

### USACO P2035 iCow 题目解析 #### 问题描述 Farmer John 购买了一台新的 MP3 播放器 iCow,其中存储了 N (1 ≤ N ≤ 1,000) 首歌曲,每首歌都有一个初始权值 Ri (1 ≤ Ri ≤ 10,000)[^4]。播放顺序由 FJ 设计的独特算法决定: - 下一首播放的是当前所有未播放过的歌曲中权值最高的那一首;若有多个最高权值,则选择编号最小的一首。 - 当某首歌曲播放完成后,其权值会均匀分配给其余 N − 1 首歌曲,并将其自身的权值设为零。 - 如果该歌曲的权值不能被 N − 1 整除,则剩余部分将以 1 单位的形式依次给予排名靠前但尚未获得额外权重的歌曲。 任务是求出按照上述规则最先播放的 T (1 ≤ T ≤ 1000) 首歌曲的具体情况。 #### 解决方案思路 为了模拟这个过程并找到最开始播放的 T 首歌曲,可以采用优先队列(最大堆)来管理待播列表及其对应的权重。每次取出具有最大权重的元素作为即将播放的对象,在更新其他成员的新权重之后重新加入到队列当中继续循环直至达到所需次数为止。 具体步骤如下: - 初始化数据结构:创建一个包含所有歌曲 ID 和它们各自起始分数的最大堆; - 输出此目标的信息; - 更新剩余项目的得分并将已处理项移回至集合内等待下次轮转; 下面给出完整的 C++ 实现代码示例: ```cpp #include <iostream> #include <queue> using namespace std; struct Song { int id; long score; }; bool operator<(const Song& a, const Song& b){ return !(a.score > b.score || (a.score == b.score && a.id < b.id)); } int main(){ priority_queue<Song> pq; int n,t,r; cin>>n>>t; for(int i=1;i<=n;++i){ cin >> r; pq.push({i,r}); } while(t--){ auto top_song=pq.top(); cout<<top_song.id<<"\n"; vector<int> remainders; pq.pop(); if(top_song.score%(n-1)!=0){ for(int j=0;j<top_song.score%(n-1);++j) remainders.push_back(j); } while(!pq.empty()){ Song current = pq.top(); pq.pop(); current.score += top_song.score/(n-1); if (!remainders.empty()) { current.score++; remainders.erase(remainders.begin()); } pq.push(current); } // Reinsert the played song with zero points back into queue. pq.push({top_song.id, 0}); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值