I - Inversion
HDU-6098
Give an array A, the index starts from 1.
Now we want to know Bi=maxi∤jAjBi=maxi∤jAj , i≥2i≥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 AiAi.
Limits
T≤20T≤20
2≤n≤1000002≤n≤100000
1≤Ai≤10000000001≤Ai≤1000000000
∑n≤700000∑n≤700000
Output
For each test case output one line contains n-1 integers, separated by space, ith number is Bi+1Bi+1.
Sample Input
2
4
1 2 3 4
4
1 4 2 3
Sample Output
3 4 3
2 4 4
题意:给你一个序列A,求序列B,B[i]是A[j]的最大值,其中i是j的因子
解法:把A按从大到小排序,然后从前往后取A的值,如果之前的索引是B索引的倍数,则break掉
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int b[100010];
struct node{
int v,p;
}a[100010];
bool cmp(node x,node y){
return x.v>y.v;
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].v;
a[i].p=i;
}
sort(a+1,a+n+1,cmp);
for(int i=2;i<=n;i++)
for(int j=1;j<=n;j++){
if(a[j].p%i!=0){
b[i]=a[j].v;
break;
}
}
for(int i=2;i<=n;i++){
if(i!=2)
cout<<" ";
cout<<b[i];
}
cout<<endl;
}
}
网友评论