HDU 6098 Inversion

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

Recommend
liuyiding | We have carefully selected several similar problems for you: 6119 6118 6117 6116 6115

b[i]表示a數組中下標不能被i整除的最大值。
經過排序之後,之後那麼每次找到的第一個數就是答案了,時間複雜度低於O(n*n)

#include<iostream>
#include<algorithm>
using namespace std;
//不要小瞧人生啊
const int N = 1e5 + 10;
int T, n, a[N], id[N];
int main() 
{
    scanf("%d", &T);
    for(int i=0;i<T;i++)
    {
        scanf("%d", &n);
        for(int i=1;i<n+1;i++)
            scanf("%d", a + i), id[i] = i;
        sort(id + 1, id + 1 + n, [&](int x, int y) 
        {
            return a[x]>a[y]; 
        });//又學習到了cmp的另一種套路 //[&](int x,int y){return a[x]>a[y]}
        for(int i=2;i<n+1;i++)//暴力
        {
            int f = 0;
            for(int j=1;j<n+1;j++)
                if (id[j] % i != 0) //下標
                {
                f = a[id[j]];
                break;
                }
            printf("%d%c", f, " \n"[i == n]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值