HDU6098 Inversion(RMQ,2017 HDU多校联赛 第6场)

来源:互联网 发布:出知西安之咸宁翻译 编辑:程序博客网 时间:2024/06/05 14:07

题目:

Inversion

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 321    Accepted Submission(s): 218


Problem Description
Give an array A, the index starts from 1.
Now we want to know Bi=maxijAj , i2.
 

Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integer n : the size of array A.
Next one line contains n integers, separated by space, ith number is Ai.

Limits
T20
2n100000
1Ai1000000000
n700000
 

Output
For each test case output one line contains n-1 integers, separated by space, ith number is Bi+1.
 

Sample Input
241 2 3 441 4 2 3
 

Sample Output
3 4 32 4 4
 

Source
2017 Multi-University Training Contest - Team 6
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6107 6106 6105 6104 6103 

思路:

题目给了n个数,可以理解为A数组,下标为1~n

现在要求的是B数组,下标为2~n

b[i]为在a数组中筛去i以及i的倍数的所对应的值,在剩下的值里面求最大值。

我们直接利用RMQ预处理,然后枚举区间就行


代码:

#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define N 100200#define ll long longusing namespace std;int maxx[N][20];int a[N];void RMQ(int n){for(int j=1; j<20; j++)for(int i=1; i<=n; i++)if(i+(1<<j)-1<=n){maxx[i][j]=max(maxx[i][j-1],maxx[i+(1<<(j-1))][j-1]);}}int main(){int t,n,x;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1; i<=n; i++){scanf("%d",&x);maxx[i][0]=x;}RMQ(n);for(int i=2; i<=n; i++) //枚举间隔{int num=0;for(int j=1; j<=n; j+=i) //左端点{int p=j+i-2;//右端点if(p>n) p=n;if(p==j)num=max(maxx[j][0],num);else{int k=(int)(log(p-j+1.0)/log(2.0));num=max(num,max(maxx[j][k],maxx[p-(1<<k)+1][k]));}}a[i]=num;}for(int i=2; i<=n; i++){if(i>2)printf(" ");printf("%d",a[i]);}puts("");}return 0;}



阅读全文
0 0
原创粉丝点击