hdu -- 6098 -- Inversion(排序)

Inversion

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

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

题意:给你一个数组 A (下标 j 从1 开始),叫你求数组B(下标 i 从 2 开始).Bi = max( Aj ) , j % i != 0。

解题思路:用结构题把 Ai 的 值 与 位置 记下来,然后按值从大到小排序。然后扫一遍遇到符合条件的记录并跳出。最坏的情况是 i 的倍数排完序后都在前面,但 i 的倍数最多有 lgn 个。所以复杂度为 nlgn。


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;

const int maxn = 1e5 + 5;
int h[maxn];
struct X{
    int v,pos;
}s[maxn];

int cmp(X a,X b){
    return a.v > b.v;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d",&s[i].v);
            s[i].pos = i;
        }
        sort(s + 1,s + 1 + n,cmp);
        for(int i = 2;i <= n;i++){
            for(int j = 1;j <= n;j++){
                if(s[j].pos % i != 0){
                    h[i] = s[j].v;
                    break;
                }
            }
        }
        for(int i = 2;i <= n;i++){
            if(i == n) printf("%d\n",h[i]);
            else printf("%d ",h[i]);
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值