poj3264 st表+输入输出挂
来源:互联网 发布:淘宝以旧换新主板骗局 编辑:程序博客网 时间:2024/05/19 06:46
题目:
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 31734251 54 62 2
Sample Output
630
Source
思路:
rmq问题,由于查询次数多,采用O(nlogn)+O(1)的st算法。
代码:
开挂前 3391ms
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<ctype.h> //tower()#include<set> #include<map> #include<iomanip>// cout<<setprecision(1)<<fixed<<a;#include<vector> #include<time.h> #include<assert.h> //assert#include<cmath>#include<algorithm>#include<bitset>#include<limits.h>#include<stack>#include<queue>using namespace std;const int maxn=50010;const int maxm=1001;const int inf=INT_MAX;int n,q,a[maxn],st[maxn][16],sti[maxn][16];void initrmq(){//记录最小值 for(int i=0;i<n;++i) st[i][0]=sti[i][0]=a[i];for(int j=1;(1<<j)<=n;++j){for(int i=0;i+(1<<j)-1<n;++i){//i+2^j-1<n,st[i][j]才有意义 st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);sti[i][j]=max(sti[i][j-1],sti[i+(1<<(j-1))][j-1]);}}}int rmq(int u,int v){int k=(int)(log(v-u+1.0)/log(2.0));//向下取整并转换成int,功能同floor(),保证u+1<<k落在[u,v]内。ceil()向上取整 return max(sti[u][k],sti[v-(1<<k)+1][k])-min(st[u][k],st[v-(1<<k)+1][k]);}int main(){//7164K3391MSint c,d;while(~scanf("%d%d",&n,&q)){for(int i=0;i<n;++i){scanf("%d",&a[i]);}initrmq();while(q--){scanf("%d%d",&c,&d);printf("%d\n",rmq(c-1,d-1));}}return 0;}
开挂后 750ms
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<ctype.h> //tower()#include<set> #include<map> #include<iomanip>// cout<<setprecision(1)<<fixed<<a;#include<vector> #include<time.h> #include<assert.h> //assert#include<cmath>#include<algorithm>#include<bitset>#include<limits.h>#include<stack>#include<queue>using namespace std;const int maxn=50010;const int maxm=1001;const int inf=INT_MAX;int n,q,a[maxn],st[maxn][16],sti[maxn][16];void initrmq(){for(int i=0;i<n;++i) st[i][0]=sti[i][0]=a[i];for(int j=1;(1<<j)<=n;++j){for(int i=0;i+(1<<j)-1<n;++i){st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);sti[i][j]=max(sti[i][j-1],sti[i+(1<<(j-1))][j-1]);}}}int rmq(int u,int v){int k=(int)(log(v-u+1.0)/log(2.0));return max(sti[u][k],sti[v-(1<<k)+1][k])-min(st[u][k],st[v-(1<<k)+1][k]);}void In(int &x){char c;x=0;c=getchar();int sign=1;while(!(c>='0'&&c<='9'||c=='-')) c=getchar();if(c=='-') sign=-1,c=getchar();while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}x*=sign;}void Out(int x){if(x<0){x=-x;putchar('-');}if(x>9) Out(x/10);putchar(x%10+'0');}int main(){//7152K750MSint c,d;while(~scanf("%d%d",&n,&q)){for(int i=0;i<n;++i){In(a[i]);}initrmq();while(q--){In(c); In(d);Out(rmq(c-1,d-1));putchar('\n');//最后才能输出换行 }}return 0;}
- poj3264 st表+输入输出挂
- POJ3264 Balanced Lineup 线段树|ST表
- [poj3264] Balanced Lineup(st表)
- poj3264 Balanced Lineup(ST表)
- 【poj3264】 Balanced Lineup(st表)
- poj3264(ST)
- poj3264(ST算法)
- [学习][poj3264]稀疏表(ST表) Balanced Lineup
- poj3264 Balanced Lineup(RMQ +st)
- ST算法模板题poj3264
- POJ3264 Balanced Lineup (RMQ & ST)
- poj3264 rmq问题——st算法
- RMQ(ST 与 线段树)eg poj3264
- RMQ问题 ST 算法总结 poj3264
- ST算法学习——POJ3264
- poj3264-RMQ问题的ST算法
- RMQ-ST 详细讲解 poj3264为例
- st的输入输出模式
- [leetcode]386. Lexicographical Numbers
- Servlet之ServletContext获取获取web项目的资源文件
- coursera机器学习
- 【jQuery】jQuery小结
- 进程的控制
- poj3264 st表+输入输出挂
- linux编程之GDB调试
- Python-迭代删除不为空的文件夹
- What is a NullPointerException, and how do I fix it?
- [USACO1.1]贪婪的送礼者Greedy Gift Givers
- 集合嵌套之ArrayList嵌套ArrayList
- 设计模式(5) ------- 建造者模式
- ubuntu16下安装sublime-text-3,同时解决不能输入中文的问题。
- 网关热备份-GLBP