17多校contest 6- 1003 Inversion ( 模拟

本文介绍了一种通过排序解决特定数组问题的方法。给定数组A,我们关注的是如何找到每个位置i(i≥2)的最大值Bi,其中Bi定义为所有Aj中最大的一个,且j不是i的倍数。通过将数组按降序排列并遍历,可以有效地找到答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题解:

签到题 把数组从大到小排个序就好

AC代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6+10;
struct node {
    int x, pos;
}p[N];
int arr[N], brr[N];
bool cmp(node a,node b)
{
    return a.x > b.x;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--) {
        int n;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++) {
            scanf("%d",&arr[i]);
            p[i].x = arr[i];
            p[i].pos = i;
        }
        int k = 0;
        sort(p+1, p+n+1, cmp);
        for(int i = 2; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(p[j].pos%i != 0) {
                    brr[i] = p[j].x; break;
                }
            }
        }
        for(int i = 2;i <= n; i++) {
            if(i == 2) printf("%d",brr[i]);
            else printf(" %d",brr[i]);
        }
        printf("\n");
    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值