HDU - 6098 Inversion (模拟)

Give an array A, the index starts from 1.
Now we want to know
B
i

max
i∤j
A
j
Bi=maxi∤jAj
,
i≥2
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
A
i
Ai
.

Limits
T≤20
T≤20

2≤n≤100000
2≤n≤100000

1≤Ai≤1000000000
1≤Ai≤1000000000

∑n≤700000
∑n≤700000
Output
For each test case output one line contains n-1 integers, separated by space, ith number is
B
i+1
Bi+1
.
Sample Input
2
4
1 2 3 4
4
1 4 2 3
Sample Output
3 4 3
2 4 4
Hint:
i∤j 表示 j % i != 0
问题链接: http://acm.hdu.edu.cn/showproblem.php?pid=6098
问题简述: 给出一个长度为n的序列Ai,求出一个序列Bi = maxi∤jAj,(i>=2)
问题分析: 题目理解困难.jpg 意思如题意还是想了很久,如:第一个样例:B2=max(A1,A3)=maxn(1,3)=3,B3=max(A1,A2,A4)=max(1,2,4)=4,B4=max(A1,A2,A3)=max(1,2,3)=3
所以B={3,4,3}
用结构体将A的权值跟初始位置记录下来,再根据权值排序,一个循环扫下来就可以了
AC通过的C++语言程序如下:

#include <iostream>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include<vector>
#define ll long long
using namespace std;

struct node
{
    int pos;
    ll value;
}a[1000505];

bool cmp(node x,node y)
{
    return x.value>y.value;
}

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i].value;
            a[i].pos=i+1;
        }
        sort(a,a+n,cmp);
        int flag=0;
        for(int i=2;i<=n;i++)
        {
            int k=0;
            while(a[k].pos%i==0) k++;
            if(flag)
                cout<<" "<<a[k].value;
            else cout<<a[k].value;
            flag++;
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44012745/article/details/89451837
今日推荐