Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversions in it. An inversion is defined as a pair of indices i < j such that Ai > Aj.

Now we have a new challenging problem. You are supposed to count the number of triple inversions in it. As you may guess, a triple inversion is defined as a triple of indices i < j < k such that Ai > Aj > Ak. For example, in the list {5, 1, 4, 3, 2} there are 4 triple inversions, namely (5,4,3), (5,4,2), (5,3,2) and (4,3,2). To simplify the problem, the list A is given as a permutation of integers from 1 to N.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N in [3, 105]. The second line contains a permutation of integers from 1 to N and each of the integer is separated by a single space.

Output Specification:

For each case, print in a line the number of triple inversions in the list.

Sample Input:

22
1 2 3 4 5 16 6 7 8 9 10 19 11 12 14 15 17 18 21 22 20 13

Sample Output:

8

题意:给定1~n的无序数列,求其中长度=3连续递减的字串个数,如样例:(16,14,13)。
思路:愚蠢!愚蠢!刚开始找的是三个数中的最前一个,然而判断连续递减就有点困难(这题时间限制为300ms)。
其实可以找中间的那个数,再向左查询比中间数大的数的个数,向右查询比中间数小的个数, 两侧相乘就是解了。
最大的问题是如何记录大小,因为给定的是1~n连续的数,甚至不需用离散化,通过遍历到某个数,找比它小或大的是否已经被标记了,再标记这个数。
快速求和操作显然要用到树状数组。
噢,还有这题注意结果使用long long 为此而WA。
 #include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#define LL long long
using namespace std; int n, c[], a[];
LL l[], r[];
void add(int x) //增加操作
{
while(x <= n)
{
c[x]++;
x += x & (-x);
}
} int get(int x) //获取和
{
int ans = ;
while(x)
{
ans +=c[x];
x -= x & (-x);
}
return ans;
}
int main()
{
LL ans;
while(cin >> n)
{
ans = ;
for(int i = ; i <= n; i++)
{
scanf("%d", a + i);
c[i] = ;
}
for(int i = ; i <= n; i++)
{
l[i] = i - - get(a[i]);
add(a[i]);
}
for(int i = ; i <= n; i++)
c[i] = ;
for(int i = ; i <= n; i++)
{
r[i] = a[i] - - get(a[i]);//为value - 左侧大于它的个数
add(a[i]);
} for(int i = ; i <= n; i++)
ans += l[i]*r[i]; printf("%lld\n", ans);
}
}

 

PAT 1009. Triple Inversions (35) 数状数组的更多相关文章

  1. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  2. poj 2481 Cows(数状数组 或 线段树)

    题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...

  3. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...

  5. HDU-3015 Disharmony Trees [数状数组]

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  6. wmz的数数(数状数组)

    wmz的数数(数状数组) 题目描述 \(wmz\)从小就显现出了过人的天赋,他出生的第三天就证明了哥德巴赫猜想,第五天就证明了质能方程,出生一星期之后,他觉得\(P\)是否等于\(NP\)这个问题比前 ...

  7. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  8. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

随机推荐

  1. NumPy常用函数总结

    转载:https://www.cnblogs.com/hd-chenwei/p/6832732.html NumPy库总包含两种基本的数据类型:矩阵和数组,矩阵的使用类似Matlab,本实例用得多的是 ...

  2. U盘安装OSX

    1.插入U盘,磁盘工具,格式化U盘为Mac OS X拓展 (日志式): 2.去网站搜索recovery disk assistant,此文件大约1.1M,直接打开使用它制作启动盘,进度条完毕就完成了. ...

  3. 有关rand(),srand()产生随机数学习总结

    看到夏雪冬日的有关rand()和srand()产生随机数的总结,挺好的,学习了,然后又有百度其他人的成果,系统总结一下.本文转自夏雪冬日:http://www.cnblogs.com/heyongga ...

  4. tftp 简要使用说明

    yum 安装:tftp    tftp-server (2)启动tftp   CentOS 6 service xinetd restart chkconfig tftp on     CentOS ...

  5. WebService(三)

    JAX-WS简单使用示例: 1.服务端 package com.rong.service; import javax.jws.WebMethod; import javax.jws.WebParam; ...

  6. 这套C#编码规范写不错

    自己总结的C#编码规范--1.命名约定篇:http://www.cnblogs.com/luzhihua55/p/CodingConventions1.html 自己总结的C#编码规范--2.命名选择 ...

  7. windows下的coreseek安装及PHP调用入门

    转载:http://zhan.renren.com/longmensoft?gid=3602888498043096197&checked=true 把我的运行环境简单说一下:windows ...

  8. Linux服务器开启tomcat的gc日志

    压力测试,为了能监控长期对gc的变化的情况,那么就需要在tomcat中进行配置相关的gc输入日志,以便后续来对gc中进行分析 工具 :linux+tomcat 1.进入到了tomcat的bin的目录下 ...

  9. jmeter同步定时器

    同步定时器是jmeter中一个比较重要的定时器,同步定时器,相当于一个储蓄池,累积一定的请求,当在规定的时间内达到一定的线程数量,这些线程会在同一个时间点一起并发,可以用来做大数据量的并发请求. 验证 ...

  10. [十三]SpringBoot 之 过滤器(Filter)和监听器(Listener)

    过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet 一样,不清楚的可以查看下上一篇文章 代码示例 package me.shijunjie.filter; import ...