Problem Description
Give an array A, the index starts from 1.
Now we want to know Bi=maxi∤jAj , i≥2.
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
T≤20
2≤n≤100000
1≤Ai≤1000000000
∑n≤700000
Output
For each test case output one line contains n-1 integers, separated by space, ith number is Bi+1.
Sample Input
2
4
1 2 3 4
4
1 4 2 3
Sample Output
3 4 3
2 4 4
题意:
有一个有n个元素的数组,数组的下标从1到n,现在要求的就是从下标2开始的,下标不是它倍数的数值中的最大值。
分析:
因为要找的数值要与下标联系在一起,我们循环的往后找的话时间肯定会超的。
定义一个结构体,一个保存下标,一个保存对应的值,将结构体里面的元素对应的值按照从大到小的顺序排列好之后,循环数组只要找到第一个结构体里的下标不是当前要找的下标的倍数,就把对应的值输出来即可。
代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e5+10;
struct node
{
ll m;///保存值
int x;///保存下标
} a[maxn];
inline bool cmp(node a, node b)
{
return a.m > b.m;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
ll maxn,mm;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%lld", &mm);
a[i].x = i;
a[i].m = mm;
}
maxn = 0;
sort(a + 1,a + n + 1, cmp);
for(int i = 2; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(a[j].x % i != 0)
{
if(i==2)
printf("%lld",a[j].m);
else
printf(" %lld",a[j].m);
break;
}
}
}
printf("
");
}
return 0;
}