LG2880 [USACO07JAN]平衡的阵容Balanced Lineup 和 BZOJ1702 [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列*

Balanced Lineup

题目背景

题目描述:

每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别.

输入:

第1行:N,Q

第2到N+1行:每头牛的身高

第N+2到N+Q+1行:两个整数A和B,表示从A到B的所有牛。(1<=A<=B<=N)

输出:

输出每行一个数,为最大数与最小数的差

题目描述

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

一个农夫有N头牛,每头牛的高度不同,我们需要找出最高的牛和最低的牛的高度差。

输入输出格式

输入格式:

Line 1: Two space-separated integers, N and Q.

Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i

Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

输出格式:

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

输入输出样例

输入样例#1: 复制
6 3
1
7
3
4
2
5
1 5
4 6
2 2
输出样例#1: 复制
6
3
0

给一个序列,求一段区间的极差。

看看n<=50000,必定是用O(nlogn)时间复杂度的RMQ。

但RMQ的实现方式有许多种,树状数组、线段树、ST表都可以。

鉴于前面的大佬只给出了线段树与ST表做法,我就来一发常数小、代码短的树状数组,据说速度一交就排rank3,180ms,还是很快的。

用树状数组原理同时维护区间最大值最小值即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define ll long long
#define re register
#define il inline
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
const int N=50005;
int n,q,c[N],b[N],s[N]; //b维护最大值,s维护最小值
il int gi() {
    re int x=0,t=1;
    re char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') t=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
    return x*t;
}
il void add(re int x,re int k) { //区间维护最大最小值
    for(; x<=n; x+=x&-x) b[x]=max(b[x],k),s[x]=min(s[x],k);
}
il int Query(re int l,re int r) { //区间查询最大最小值
    re int mn=2e9,mx=-1;
    while(l<=r) {
        for(; r-(r&-r)>=l; r-=r&-r) mx=max(mx,b[r]),mn=min(mn,s[r]);
        mx=max(c[r],mx);
        mn=min(mn,c[r]);
        r--;//还有一部分区间未涉及
    }
    return mx-mn;
}
int main() {
    memset(s,63,sizeof(s));
    n=gi();
    q=gi();
    fp(i,1,n) {
        c[i]=gi();
        add(i,c[i]);
    }
    fp(i,1,q) {
        re int l=gi(),r=gi();
        printf("%d\n",Query(l,r));
    }
    return 0;
}

Gold Balanced Lineup

Problem 1702. -- [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 864   Solved: 348
[ Submit][ Status][ Discuss]

Description

Farmer John's N cows (1 <= N <= 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared by his cows to a list of only K different features (1 <= K <= 30). For example, cows exhibiting feature #1 might have spots, cows exhibiting feature #2 might prefer C to Pascal, and so on. FJ has even devised a concise way to describe each cow in terms of its "feature ID", a single K-bit integer whose binary representation tells us the set of features exhibited by the cow. As an example, suppose a cow has feature ID = 13. Since 13 written in binary is 1101, this means our cow exhibits features 1, 3, and 4 (reading right to left), but not feature 2. More generally, we find a 1 in the 2^(i-1) place if a cow exhibits feature i. Always the sensitive fellow, FJ lined up cows 1..N in a long row and noticed that certain ranges of cows are somewhat "balanced" in terms of the features the exhibit. A contiguous range of cows i..j is balanced if each of the K possible features is exhibited by the same number of cows in the range. FJ is curious as to the size of the largest balanced range of cows. See if you can determine it.

N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,
每头牛有多种特色,用二进制01表示它的特色ID。比如特色ID为13(1101),
则它有第1、3、4种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内
拥有次数相同。求最大的[i,j]段长度。

Input

* Line 1: Two space-separated integers, N and K.

* Lines 2..N+1: Line i+1 contains a single K-bit integer specifying the features present in cow i. The least-significant bit of this integer is 1 if the cow exhibits feature #1, and the most-significant bit is 1 if the cow exhibits feature #K.

Output

* Line 1: A single integer giving the size of the largest contiguous balanced group of cows.

Sample Input

7 3
7
6
7
2
1
4
2

INPUT DETAILS:

The line has 7 cows with 3 features; the table below summarizes the
correspondence:
Feature 3: 1 1 1 0 0 1 0
Feature 2: 1 1 1 1 0 0 1
Feature 1: 1 0 1 0 1 0 0
Key: 7 6 7 2 1 4 2
Cow #: 1 2 3 4 5 6 7

Sample Output

4

OUTPUT DETAILS:

In the range from cow #3 to cow #6 (of size 4), each feature appears
in exactly 2 cows in this range:
Feature 3: 1 0 0 1 -> two total
Feature 2: 1 1 0 0 -> two total
Feature 1: 1 0 1 0 -> two total
Key: 7 2 1 4
Cow #: 3 4 5 6

HINT

鸣谢fjxmyzwd

Source

[ Submit][ Status][ Discuss]

HOME Back

N头牛,一共K种特色。每头牛有多种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内拥有次数相同。求最大的[i,j]段长度。n≤100000,k≤30。

得到式子:a[i][l]-a[j][l]=a[i][l-1]-a[j][l-1],l在2..k之间,移项得a[i][l]-a[i][l-1]=a[j][l]-a[j][l-1],l在2..k之间,故可以定义一个结构体,里面包含所有的a[i][l]-a[i][l-1],l在2..k之间,然后用set查找满足所有元素相等的最小j即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define maxn 100010
using namespace std;

inline int read(){
    char ch=getchar(); int f=1,x=0;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return f*x;
}
int sum[maxn][50],n,k,ans;
struct nd{
    int id; int num[50];
    bool operator < (const nd &a)const{inc(i,1,k-1)if(num[i]!=a.num[i])return num[i]<a.num[i]; return 0;}
};
multiset<nd>st; nd a;
int main(){
    n=read(); k=read();
    inc(i,1,n){
        int x=read(); inc(j,1,k)sum[i][j]=sum[i-1][j]; for(int j=k-1;j>=0;j--)if(x&(1<<j))sum[i][j+1]++;
    }
    inc(j,1,k-1)a.num[j]=0; a.id=0; st.insert(a);
    inc(i,1,n){
        inc(j,1,k-1)a.num[j]=sum[i][j]-sum[i][j+1]; a.id=i;
        multiset<nd>::iterator sti=st.find(a); if(sti==st.end())st.insert(a);else{
            ans=max(ans,i-sti->id);
        }
    }
    printf("%d",ans); return 0;
}

猜你喜欢

转载自www.cnblogs.com/autoint/p/10848464.html