HDU_5147 Sequence II [逆序数]

本文介绍了一种使用树状数组求解逆序数的方法来解决SequenceII问题,该问题是寻找符合条件的四元组数量。通过两次遍历序列并利用树状数组计算逆序数,最终得出所有满足条件的四元组总数。

传送门:5147

Sequence II


Problem Description
Long long ago, there is a sequence A with length n. All numbers in this sequence is no smaller than 1 and no bigger than n, and all numbers are different in this sequence.
Please calculate how many quad (a,b,c,d) satisfy:
1. 1a<b<c<dn
2. Aa<Ab
3. Ac<Ad
 

Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with a line contains an integer n.
The next line follows n integers A1,A2,,An .

[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <= Ai <= n
 

Output
For each case output one line contains a integer,the number of quad.
 

Sample Input
  
1 5 1 3 2 4 5
 

Sample Output
  
4


思路:树状数组求逆序数

/************************************************
* Author: Ac_sorry
* File:
* Create Date:
* Motto: One heart One life
* 优快云: http://blog.youkuaiyun.com/code_or_code
*************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define seed_ 131
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof a)
#define w(i) tree[i].w
#define ls(i) tree[i].ls
#define rs(i) tree[i].rs
using namespace std;
typedef long long LL;

const int N=50010;
int n;
int a[N],c[N];
LL rev_1[N],rev_2[N];


int lowbit(int x)
{
    return -x&x;
}

void add(int x)
{
    while(x<=n)
    {
        c[x]+=1;
        x+=lowbit(x);
    }
}

LL query(int x)
{
    LL ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}


int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        mem(c,0);
        mem(rev_1,0);
        mem(rev_2,0);
        scanf("%d",&n);
        LL ans=0;
        int x;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",a+i);
            add(a[i]);
            rev_1[i]=query(n)-query(a[i]);
        }
        mem(c,0);
        for(int i=n;i>=1;i--)
        {
            add(a[i]);
            rev_2[i]=rev_2[i+1]+query(a[i]-1);
        }
//        for(int i=1;i<=n;i++)
//        {
//            printf("%I64d--%I64d\n",rev_1[i],rev_2[i]);
//        }
        for(int i=2;i<=n-2;i++)
        {
            ans+=((LL)i-1-rev_1[i])*((LL)(n-i)*(n-i-1)/2-rev_2[i+1]);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值