HDU6098-Inversion

来源:互联网 发布:石材行业做效果图软件 编辑:程序博客网 时间:2024/05/17 21:41

Inversion

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 562 Accepted Submission(s): 378

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

题目大意:Bi=maxijAj,i2
解题思路:用结构体保存数列a的值与坐标,按值从大到小排序,对每个i暴力找到第一个不被i整除的数即可。

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;typedef long long LL;const int MAXN=1e5+5;struct node{    int v,id;}a[MAXN];bool cmp(node x,node y){    return x.v>y.v;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        scanf("%d",&n);        for(int i=1;i<=n;++i)        {            scanf("%d",&a[i].v);            a[i].id=i;        }        sort(a+1,a+n+1,cmp);        bool flag=true;        for(int i=2;i<=n;++i)        {            for(int j=1;j<=n;j++)            {                if(a[j].id%i!=0)                {                    if(!flag) {printf(" %d",a[j].v);break;}                    else {printf("%d",a[j].v);flag=false;break;}                }            }        }        printf("\n");    }    return 0;}
原创粉丝点击