HDU-1394 Minimum Inversion Number

Problem Description
The inversion number of a given number sequence a1, a2, …, an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, …, an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, …, an-1, an (where m = 0 - the initial seqence)
a2, a3, …, an, a1 (where m = 1)
a3, a4, …, an, a1, a2 (where m = 2)

an, a1, a2, …, an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.

Output
For each case, output the minimum inversion number on a single line.

Sample Input
10
1 3 6 9 0 8 5 7 4 2

Sample Output
16

题意:
题意:0~n-1且不一定有序排列的数字串,不断的将第一个数字放到数字串的最后,可以得到n个不同的数字串。每一个数字串都有一个逆序数,求n个数字串中最小的逆序数。
思路:
很简单的线段树,树状数组也可以做,把数拿下来每次往线段树里面查,或者往一个新数组里面插,就xjb些就行了

做这道题的时候初识线段树,竟然敲了一大段代码手写了一个for循环,坑爹的是竟然 842Ms 水过了
后来for循环写了一下 102Ms就过了 —– 菜鸡如我
仔细学习了一下线段树之后理解了线段树的区间查询再写了一次 42 Ms 过了 在这里写一下心得体会,
线段树的区间查询操作之所以是O(lg(n))的复杂度,完全是因为分治,每次都查询一半,如果每一个都遍历一次的话,
跟for循环就一模一样了,而且百度学习线段树的时候还学习到了懒标记的使用,也是线段树的一大重点,能够很好的降低时间复杂度,值得学习

———————————————————-AC code—————————————

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int maxn = 6000+5;
struct segtree{
    int l,r;
    int sum;
}ss[maxn<<2];

int v[maxn];

void build(int l,int r,int rt ) {
    ss[rt].l = l,ss[rt].r = r;
    if ( ss[rt].l == ss[rt].r ) { ss[rt].sum = 0; return;}
    int mid = (l+r)>>1;
    build(lson); build(rson);
    ss[rt].sum = 0;
}

void update(int l,int r,int rt){
    if ( ss[rt].l == ss[rt].r ) { ss[rt].sum++; return; }
    int mid = (ss[rt].l+ss[rt].r)>>1;
    if ( mid >= r ) update(l,r,rt<<1);
    else update(l,r,rt<<1|1);
    ss[rt].sum = ss[rt<<1].sum + ss[rt<<1|1].sum;
}

int query(int l,int r,int rt){
    if ( ss[rt].l == l && ss[rt].r == r ){
        return ss[rt].sum;
    }
    int mid = (ss[rt].l+ss[rt].r)>>1; int s = 0;
    if ( mid >= r ) {
        s += query(l,r,rt<<1);
    } else if ( mid < l ) {
        s += query(l,r,rt<<1|1);
    } else {
        s += query(l,mid,rt<<1);
        s += query(mid+1,r,rt<<1|1);
    }
    return s;
}

int main(){
    int t;
    while(~scanf("%d",&t)){
        build(1,t,1);
        int sum = 0;
        for (int i = 1;i<=t;i++) {
            scanf("%d",&v[i]); v[i]++;
            if ( v[i] != t )
                sum += query(v[i]+1,t,1);
            update(v[i],v[i],1);
        }
        int minn = sum;
        for (int i = 1;i<=t;i++){
            sum = sum + t - 2*v[i] + 1;
            minn = min(minn,sum);
        }
        printf("%d\n",minn);
    }
    return 0;
}

其实树状数组也可以实现

——————————————- AC code ————————————————-

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug puts("ok!");
using namespace std;
//typedef long long ll;
const int maxn = 50000+5;

int bit[maxn],t;

int lowbit(int pos){
    return (pos & (-pos));
}
int query(int pos){
    int ret = 0;
    while(pos > 0){
        ret += bit[pos];
        pos -= lowbit(pos);
    }
    return ret;
}
void update(int pos)
{
    while(pos<=t){
        bit[pos] ++;
        pos += lowbit(pos);
    }
} 

int v[maxn];

int main(){
    while(cin>>t){
        int sum = 0;
        memset(bit,0,sizeof(bit));
        for (int i = 1;i<=t;i++) {
            scanf("%d",&v[i]);    v[i]++;
            sum += query(t) - query(v[i]);
            //printf("%d\n",query(t));
            update(v[i]);
        }
        //printf("%d\n",sum);
        int minn = sum;
        for (int i = 1;i<=t;i++){
            sum = sum + t - 2*v[i] + 1;
            minn = min(minn,sum);
        }
        printf("%d\n",minn);
    }
    return 0;
} 
分数阶傅里叶变换(Fractional Fourier Transform, FRFT)是对传统傅里叶变换的拓展,它通过非整数阶的变换方式,能够更有效地处理非线性信号以及涉及时频局部化的问题。在信号处理领域,FRFT尤其适用于分析非平稳信号,例如在雷达、声纳和通信系统中,对线性调频(Linear Frequency Modulation, LFM)信号的分析具有显著优势。LFM信号是一种频率随时间线性变化的信号,因其具有宽频带和良好的时频分辨率,被广泛应用于雷达和通信系统。FRFT能够更精准地捕捉LFM信号的时间和频率信息,相比普通傅里叶变换,其性能更为出色。 MATLAB是一种强大的数值计算和科学计算工具,拥有丰富的函数库和用户友好的界面。在MATLAB中实现FRFT,通常需要编写自定义函数或利用信号处理工具箱中的相关函数。例如,一个名为“frft”的文件可能是用于执行分数阶傅里叶变换的MATLAB脚本或函数,并展示其在信号处理中的应用。FRFT的正确性验证通常通过对比变换前后信号的特性来完成,比如评估信号的重构质量、信噪比等。具体而言,可以通过计算原始信号与经过FRFT处理后的信号之间的相似度,或者对比LFM信号的关键参数(如初始频率、扫频率和持续时间)是否在变换后得到准确恢复。 在MATLAB代码实现中,通常包含以下步骤:首先,生成LFM信号模型,设定其初始频率、扫频率、持续时间和采样率等参数;其次,利用自定义的frft函数对LFM信号进行分数阶傅里叶变换;接着,使用MATLAB的可视化工具(如plot或imagesc)展示原始信号的时域和频域表示,以及FRFT后的结果,以便直观对比;最后,通过计算均方误差、峰值信噪比等指标来评估FRFT的性能。深入理解FRFT的数学原理并结合MATLAB编程技巧,可以实现对LFM信号的有效分析和处理。这个代码示例不仅展示了理论知识在
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值