首页 > 代码库 > POJ3264-Balanced Lineup-线段树
POJ3264-Balanced Lineup-线段树
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 53721 | Accepted: 25244 | |
Case Time Limit: 2000MS |
Description
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.
Input
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.
Output
Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
题意就是在一堆数里找一定范围里的最大值和最小值,计算差值。
因为知道这个题肯定不能瞎写就写对,所以还很认真的写了一个代码,真是智障,不管怎么写,只要没用算法,肯定会T的。
线段树大法好,可惜智障,改了12遍(此刻内心¥…&%……*(*&),出现各种问题嘛,存最小值要再有东西存人家啊。。。
最后实在改不出来了,求助了大佬,最后问题还是出在最小值问题上,要求最小值,一开始比较的ans就要比数组里的最大数要大啊。。。要审清题和题意。。。
线段树!!!
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cstdlib> #include<string.h> #include<set> #include<vector> #include<queue> #include<stack> #include<map> #include<cmath> using namespace std; #define N 300000 struct node{ int left,right,maxx,minn; }tree[N*4]; int n,m,ans; void build(int l,int r,int pos){ //建树 tree[pos].left=l; tree[pos].right=r; tree[pos].maxx=0; //。。。 tree[pos].minn=1e9; //一开始写的20000是不对的!!!看清题意。。。 if(tree[pos].left==tree[pos].right)return; int mid=(l+r)>>1; build(l,mid,pos*2); build(mid+1,r,pos*2+1); } void update(int x,int y,int pos){ if(tree[pos].left==x&&tree[pos].right==x){ tree[pos].maxx=y; tree[pos].minn=y; return; } int mid=(tree[pos].left+tree[pos].right)>>1; if(x>mid) update(x,y,pos*2+1); else update(x,y,pos*2); tree[pos].maxx=max(tree[pos*2].maxx,tree[pos*2+1].maxx); tree[pos].minn=min(tree[pos*2].minn,tree[pos*2+1].minn); } int query1(int x,int y,int pos){ //查询最大值 if(tree[pos].left==x&&tree[pos].right==y){ ans=max(ans,tree[pos].maxx); return ans; } int mid=(tree[pos].left+tree[pos].right)>>1; if(y<=mid)query1(x,y,pos*2); else if(x>mid)query1(x,y,pos*2+1); else{ query1(x,mid,pos*2); query1(mid+1,y,pos*2+1); } } int query2(int x,int y,int pos){ //查询最小值 if(tree[pos].left==x&&tree[pos].right==y){ ans=min(ans,tree[pos].minn); return ans; } int mid=(tree[pos].left+tree[pos].right)>>1; if(y<=mid)query2(x,y,pos*2); else if(x>mid)query2(x,y,pos*2+1); else{ query2(x,mid,pos*2); query2(mid+1,y,pos*2+1); } } int main(){ int m,n; int ans1,ans2; while(~scanf("%d%d",&n,&m)){ getchar(); build(1,n,1); int x; for(int i=1;i<=n;i++){ scanf("%d",&x); update(i,x,1); } int a,b; while(m--){ scanf("%d%d",&a,&b); ans=0; ans1=query1(a,b,1); ans=1e9; //一开始写的200000,是不对的!!! ans2=query2(a,b,1); printf("%d\n",ans1-ans2); } } return 0; }
纪念一下我的智障超时代码。。。
代码1:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
const int N=2*1e5+10;
int a[N],b[N],c[N];
int main(){
int n,m,h,k;
int minn,maxx,ans;
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
b[i]=a[i]; //b是数按顺序排
sort(b,b+n);
for(int i=0;i<n;i++){
c[b[i]]=i; //c是数对应的第几大
}
for(int i=0;i<m;i++){
scanf("%d%d",&h,&k);
minn=N;maxx=-1;
for(int i=h-1;i<=k-1;i++){
minn=min(minn,c[a[i]]);
maxx=max(maxx,c[a[i]]);
}
ans=b[maxx]-b[minn];
printf("%d\n",ans);
}
}
return 0;
}
代码2:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
const int N=2*1e5+10;
int a[N],b[N],c[N],d[N];
int main(){
int n,m,h,k;
int minn,maxx,ans;
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
b[i]=a[i]; //b是数按顺序排
sort(b,b+n);
for(int i=0;i<n;i++){
c[b[i]]=i; //c是数对应的第几大
}
for(int i=0;i<m;i++){
scanf("%d%d",&h,&k);
minn=N;maxx=-1;
int j=0;
for(int i=h-1;i<=k-1;i++){
d[j++]=c[a[i]];
sort(d,d+j);
minn=d[0];
maxx=d[j-1];
//minn=min(minn,c[a[i]]);
//maxx=max(maxx,c[a[i]]);
}
ans=b[maxx]-b[minn];
printf("%d\n",ans);
}
}
return 0;
}
基本没什么差别,反正都T。。。
(╯°Д°)╯︵┻━┻
POJ3264-Balanced Lineup-线段树