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

题意:

给出下标从1开始,长度为n的A数组,求出下标从2开始,长度为n-1的B数组。满足 Bi =max Aj (i不是j的因子)。

想法:

直接暴力。
将A数组由大到小排序后,外层循环B的下标,内层循环A的下标,只要是因子,就将A的值赋给B。
但是要用结构体稍微优美一下,存储A的数值和位置。

#include<bits/stdc++.h>
using namespace std;
int b[110000];
struct p
{
    int num,pos;
}a[110000];
int cmp(p x,p y)
{
    return x.num>y.num;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        for(int i=1;i<=n;++i)
        {
             scanf("%d",&a[i].num);
             a[i].pos=i;
        }
        sort(a+1,a+n+1,cmp);
        for(int i=2;i<=n;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(a[j].pos%i)
                {
                    b[i]=a[j].num;
                    break;
                }

            }
        }
        for(int i=2;i<=n;++i)
        {///这里注意处理行末空格的问题
            if(i!=2)
                printf(" ");
            printf("%d",b[i]);
        }
        printf("\n");
    }
    return 0;
}

ps:刚开始想复杂了,差点用质因数分解来做o(╯□╰)o

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值