HDU 6098 Inversion 思维

  题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6098

  题目描述: 给出一个序列, 让你求序列中所有不能整除i的最大值并输出  i  属于 2 ~ n

  解题思路: 这道题我犯蠢了......我想的是找规律, 所有质数先求出来再单独抠倍数, 其实只要排序就好了.....

  代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
const int maxn = 1e5 + 100;
int a[maxn];
int pos[maxn];

int cmp( int i, int j ) {
    return a[i] > a[j];
}

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 );
            pos[i] = i;
        }
        sort( pos+1, pos+n+1, cmp );
        int flag = 1;
        for( int i = 2; i <= n; i++ ) {
            int j;
            for( j = 1; j <= n; j++ ) {
                if(pos[j] % i != 0) break;
            }
            if( flag ) {
                flag = 0;
                printf( "%d", a[pos[j]] );
            }
            else {
                printf( " %d", a[pos[j]] );
            }
        }
        printf( "\n" );
    }
    return 0;
}

  思考: 遇到取最大第一反应应该是排序的啊....

时间: 2024-10-07 18:32:26

HDU 6098 Inversion 思维的相关文章

hdu 6098 Inversion

Inversion Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 339    Accepted Submission(s): 230 Problem Description Give an array A, the index starts from 1.Now we want to know Bi=max {  Aj  |  i?j

HDU 4911 Inversion 求逆序数对

点击打开链接 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1106    Accepted Submission(s): 474 Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent num

hdu 4911 Inversion

Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 197    Accepted Submission(s): 82 Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent numbers for

HDU 4911 Inversion(归并求逆序对)

HDU 4911 Inversion 题目链接 题意:给定一个数组,可以相邻交换最多k次,问交换后,逆序对为多少 思路:先利用归并排序求出逆序对,然后再减去k就是答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 100005; int n, num[N], save[N], sn; void init() { for

hdu 4911 Inversion(归并)

题目链接:hdu 4911 Inversion 题目大意:给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 解题思路:每交换一次一定可以减少一个逆序对,所以问题转换成如何求逆序对数. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll k

2014多校第五场1001 || HDU 4911 Inversion (归并求逆序数)

题目链接 题意 : 给你一个数列,可以随意交换两相邻元素,交换次数不超过k次,让你找出i < j 且ai > aj的(i,j)的对数最小是多少对. 思路 : 一开始想的很多,各种都想了,后来终于想出来这根本就是求逆序数嘛,可以用归并排序,也可以用树状数组,不过我们用树状数组做错了,也不知道为什么.求出逆序数来再减掉k次,就可以求出最终结果来了.求逆序数链接1,链接2 1 #include <stdio.h> 2 3 int left[250003], right[250003];

hdu 4911 Inversion(归并排序求逆序对数)

Inversion                                                                             Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two 

hdu 4911 Inversion(求逆序数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 228 Problem Description bobo has a sequence a1,a2,

HDU 4911 Inversion(归并排序求逆序数)

归并排序求逆序数,然后ans-k与0取一个最大值就可以了. 也可以用树状数组做,比赛的时候可能姿势不对,树状数组wa了.. Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 578    Accepted Submission(s): 249 Problem Description bobo has a seque