题目:http://acm.hdu.edu.cn/showproblem.php?pid=6098
题意:给你一个数组下标从1开始的啊,让你求Bi=maxi∤jAj (为所有的j使得i不整除j)
思路:用到rmq(听说set被卡?)
看了下别人题解,可以用结构体排序,也可以直接找最大值的因子,我这个做法好像很麻烦?反正不是我想的:P
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
int a[N],b[N],maxsum[N][20];
void RMQ(int n)
{
for(int i = 1;i <= n;i++)
maxsum[i][0] = a[i];
for(int j = 1;j < 20;j++)
for(int i = 1;i <= n;i++)
if(i + (1 << j) - 1 <= n)
{
maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);
//minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
}
}
int getMax(int l,int r)
{
int k = (int)(log(r - l + 1.0) / log(2.0));
int maxres = max(maxsum[l][k], maxsum[r - (1 << k) + 1][k]);
return maxres;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
RMQ(n);
for(int i = 2;i <= n;i++)
b[i] = a[1];
for(int i = 2,j;i <= n;i++)
{
for(j = i;j <= n;j+=i)
b[i] = max(b[i],getMax(j-i+1,j-1));
if(j > n && j-i < n)
b[i] = max(b[i],getMax(j-i+1,n));
}
printf("%d",b[2]);
for(int i = 3;i <= n;i++)
printf(" %d",b[i]);
printf("\n");
}
return 0;
}