Inversion
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 221 Accepted Submission(s): 150
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
Source
2017 Multi-University Training Contest - Team 6
题意:给你一个数组 A (下标 j 从1 开始),叫你求数组B(下标 i 从 2 开始).Bi = max( Aj ) , j % i != 0。
解题思路:用结构题把 Ai 的 值 与 位置 记下来,然后按值从大到小排序。然后扫一遍遇到符合条件的记录并跳出。最坏的情况是 i 的倍数排完序后都在前面,但 i 的倍数最多有 lgn 个。所以复杂度为 nlgn。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
int h[maxn];
struct X{
int v,pos;
}s[maxn];
int cmp(X a,X b){
return a.v > b.v;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d",&s[i].v);
s[i].pos = i;
}
sort(s + 1,s + 1 + n,cmp);
for(int i = 2;i <= n;i++){
for(int j = 1;j <= n;j++){
if(s[j].pos % i != 0){
h[i] = s[j].v;
break;
}
}
}
for(int i = 2;i <= n;i++){
if(i == n) printf("%d\n",h[i]);
else printf("%d ",h[i]);
}
}
}