sgu 180 - Inversions (离散化+树状数组)

本文介绍了一种通过离散化处理相同元素的逆序对计数算法,并给出了一段C++实现代码。该算法利用树状数组进行优化,解决给定整数数组中逆序对的数量计算问题。
 - Inversions
Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

180. Inversions
time limit per test: 0.25 sec. 
memory limit per test: 4096 KB
input: standard 
output: standard



There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].

Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.

Output
Write amount of such pairs.

Sample test(s)

Input
 
 

2 3 1 5 4
 
 

Output
 
 
3
 
 
一直wa 2
后来发现是没处理相同元素(我好傻逼啊。。。。)
离散化的时候,很重要的一项,当然是相同的元素,离散化的之后也要变成相同的。。。
上道题过了纯粹是数据水。。。
 
  1 /*************************************************************************
  2     > File Name: code/sgu/180.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  5     > Created Time: 2015年08月06日 星期四 16时40分53秒
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #define y0 abc111qqz
 21 #define y1 hust111qqz
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define tm crazy111qqz
 25 #define lr dying111qqz
 26 using namespace std;
 27 #define REP(i, n) for (int i=0;i<int(n);++i)  
 28 typedef long long LL;
 29 typedef unsigned long long ULL;
 30 const int inf = 0x7fffffff;
 31 const int N=7E4+7;
 32 struct Q
 33 {
 34     int val;
 35     int id;
 36 }q[N];
 37 int c[N];
 38 int ref[N];
 39 int n;
 40 bool cmp(Q a,Q b)
 41 {
 42     if (a.val<b.val)
 43     return true;
 44     return false;
 45 }
 46 
 47 int lowbit( int x)
 48 {
 49     return x&(-x);
 50 }
 51 void update( int x,int delta)
 52 {
 53     for ( int i = x; i < N ; i=i+lowbit(i) )
 54     {
 55     c[i] = c[i] + delta;
 56     }
 57 }
 58 int Sum( int x)
 59 {
 60     int res  =0;
 61     for ( int i = x; i >= 1 ; i = i-lowbit(i))
 62     {
 63     res = res + c[i];
 64     }
 65     return res;
 66 }
 67 int main()
 68 {
 69     while (scanf("%d",&n)!=EOF)
 70     {
 71 
 72       memset(c,0,sizeof(c));
 73       for ( int i = 1; i <= n ; i++ )
 74       {
 75         scanf("%d",&q[i].val);
 76     q[i].id  = i ;
 77     }
 78     sort(q+1,q+n+1,cmp);
 79     for ( int i = 1; i <= n ; i++ )
 80     {
 81     if (q[i].val!=q[i-1].val)
 82     {
 83         ref[q[i].id]=i;
 84     }
 85     else
 86     {
 87         ref[q[i].id]=ref[q[i-1].id];
 88     }
 89     }
 90 
 91   //  for ( int i = 1 ;i <= n ; i ++) cout<<ref[i]<<endl;
 92     LL  ans = 0;
 93     for ( int i = 1 ; i <= n ; i++ )
 94     {
 95     update(ref[i],1);
 96     ans = ans + i-Sum(ref[i]);
 97     }
 98     cout<<ans<<endl;
 99     }
100   
101     return 0;
102 }

 

 

转载于:https://www.cnblogs.com/111qqz/p/4708482.html

MATLAB代码实现了一个基于多种智能优化算法优化RBF神经网络的回归预测模型,其核心是通过智能优化算法自动寻找最优的RBF扩展参数(spread),以提升预测精度。 1.主要功能 多算法优化RBF网络:使用多种智能优化算法优化RBF神经网络的核心参数spread。 回归预测:对输入特征进行回归预测,适用于连续值输出问题。 性能对比:对比不同优化算法在训练集和测试集上的预测性能,绘制适应度曲线、预测对比图、误差指标柱状图等。 2.算法步骤 数据准备:导入数据,随机打乱,划分训练集和测试集(默认7:3)。 数据归一化:使用mapminmax将输入和输出归一化到[0,1]区间。 标准RBF建模:使用固定spread=100建立基准RBF模型。 智能优化循环: 调用优化算法(从指定文件夹中读取算法文件)优化spread参数。 使用优化后的spread重新训练RBF网络。 评估预测结果,保存性能指标。 结果可视化: 绘制适应度曲线、训练集/测试集预测对比图。 绘制误差指标(MAE、RMSE、MAPE、MBE)柱状图。 十种智能优化算法分别是: GWO:灰狼算法 HBA:蜜獾算法 IAO:改进天鹰优化算法,改进①:Tent混沌映射种群初始化,改进②:自适应权重 MFO:飞蛾扑火算法 MPA:海洋捕食者算法 NGO:北方苍鹰算法 OOA:鱼鹰优化算法 RTH:红尾鹰算法 WOA:鲸鱼算法 ZOA:斑马算法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值