HDU - 6098:Inversion(暴力均摊)

Give an array A, the index starts from 1.
Now we want to know =max ij  Bi=maxi∤jAj , ii≥2 .

InputThe 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 i  Ai .

Limits
T20 T≤20
2n100000 2≤n≤100000
1Ai1000000000 1≤Ai≤1000000000
n700000 ∑n≤700000
OutputFor each test case output one line contains n-1 integers, separated by space, ith number is i+1  Bi+1 .Sample Input

2
4
1 2 3 4
4
1 4 2 3

Sample Output

3 4 3
2 4 4

题意:对于所有的i(i!=1),找最大的a[j],满足j%i!=0;

思路:没有什么对应的算法,居然是暴力,我们从大到小排序,然后找到第一个满足题意的即可。

复杂度均摊下来是线性的,显然过得去。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
struct in{
    int id,num;
    friend bool operator <(in w,in v) {return w.num>v.num;}
}s[100010];
int main()
{
    int T,N;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        rep(i,1,N) scanf("%d",&s[i].num),s[i].id=i;
        sort(s+1,s+N+1);
        rep(i,2,N) {
            rep(j,1,N){
                if(s[j].id%i!=0){
                    printf("%d ",s[j].num); break;
                }
            }
        }
        puts("");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/9806540.html