hdu 6098

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

题解:

多校的题有点难理解啊。
i∤j’表示j%i!=0
排序优化一下,以数据大小排序,同时记录索引值,从2开始枚举,计算b数组的值,O(n)扫排序后的a数组,当遇到第一个j%i!=0,记录即可。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 100000+10;

struct node
{
    int id;
    LL data;
};

bool cmp(node q,node p)
{
    return q.data>p.data;
}

int main()
{
    int T;
    cin>>T;
    LL n;
    node a[maxn];
    vector<LL> b;
    while(T--)
    {
        cin>>n;
        LL tmp;

        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&tmp);
            a[i].data=tmp;
            a[i].id=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].id%i!=0)
               {
                   b.push_back(a[j].data);
                   break;
               }
           }
        }

        int siz = b.size();
        for(int i=0;i<siz-1;i++)
        {
           printf("%lld ",b[i]);
        }
        printf("%lld",b[siz-1]);
        cout<<endl;
        b.clear();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值