Inversion
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 605 Accepted Submission(s): 403
Problem Description
Give an array A, the index starts from 1.
Now we want to know Bi=maxi∤jAj , i≥2 .
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
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
Recommend
题意:
从A序列里 下标不为i倍数的 元素中找最大值。
思路:
排序。
标程:
1003 Inversion
需要统计的区间最大值次数是 O(nlog(n)) 级别的。
时间复杂度 O(nlog(n))。
优秀的多个O(log(n))的做法也是可以卡过的。
这里介绍一下优雅的暴力做法。将A数组按值从大到小排序,对于每个下标i暴力找到最大的不被i整除的数。
时间复杂度 O(nlog(n))。
AC代码:
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
struct node {
ll val;
int p;
} a[100005];
bool cmp(node x,node y) {
return x.val<y.val;
}
ll b[100005];
int main() {
int t,n;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%lld",&a[i].val);
a[i].p=i;
}
sort(a+1,a+n+1,cmp);
for(int i=2; i<=n; i++) {
for(int j=n; j>=1; j--) {
if((a[j].p%i)!=0) {
b[i]=a[j].val;
break;
}
}
}
for(int i=2; i<n; i++) {
printf("%lld ",b[i]);
}
printf("%lld\n",b[n]);
}
return 0;
}