【Description】

N (3 ≤ N ≤ 20000) ping pong players live along a west-east street(consider the street as a line segment).Each player has a unique skill rank. To improve their skill rank, they often compete with each other. Iftwo players want
to compete, they must choose a referee among other ping pong players and hold thegame in the referee’s house. For some reason, the contestants can’t choose a referee whose skill rank ishigher or lower than both of theirs. The contestants have to walk to the
referee’s house, and becausethey are lazy, they want to make their total walking distance no more than the distance between theirhouses. Of course all players live in different houses and the position of their houses are all different. Ifthe referee or any
of the two contestants is different, we call two games different. Now is the problem:how many different games can be held in this ping pong street?

【Input】

The first line of the input contains an integer T (1 ≤ T ≤ 20), indicating the number of test cases,followed by T lines each of which describes a test case.Every test case consists of N + 1 integers. The first integer is N, the
number of players. Then Ndistinct integers a1, a2 . . . aN follow, indicating the skill rank of each player, in the order of west to east(1 ≤ ai ≤ 100000, i = 1 . . . N).

【Output】

For each test case, output a single line contains an integer, the total number of different games.

【Sample Input】

1

3 1 2 3

【Sample Output】

1

【题解】

就是说要进行乒乓球比赛。

必须两个player和一个裁判。

然后裁判的能力值和住的地方都在两个players之间。

然后每个人住的房间在一条由西到东的笔直街道上。

每个人的能力值都不一样。

每个人都能当裁判。

设c[i]表示前i个人里面有几个人的能力值比第i个人小。

设d[i]表示i+1..n这些人里面有几个人的能力值比第i个人小。

i-1-ci 就是前i个人里面比第i个人的能力值大的人的个数

n-i-di就是i+1..n这些人里面比第i个人的能力值大的人的个数。

则答案就是

∑ ci*(n-i-di) + (i-ci-1)*di

因为ai不大最多为10W.

则用树状数组处理出ci和di即可。

然后ci是从前往后。di是从后往前。

LA不支持%I64d的输出

【代码】

#include <cstdio>
#include <cstring> const int MAXN = 101000; int n,a[MAXN],bmi[MAXN],ami[MAXN],c[MAXN],d[MAXN];
long long ans; void init()
{
memset(bmi, 0, sizeof(bmi));
memset(ami, 0, sizeof(ami));
ans = 0;
} int lowbit(int x)
{
return x & (-x);
} void input_data()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
{
int leijia = 0;
int temp = a[i]; //累加a[i]的前缀和
while (temp > 0)
{
leijia += bmi[temp];
temp -= lowbit(temp);
}
c[i] = leijia;
temp = a[i];
while (temp <= 100000)
{
bmi[temp]++; //只加1
temp += lowbit(temp);
}
}
for (int i = n; i >= 1; i--)//di与ci处理相同
{
int leijia = 0;
int temp = a[i];
while (temp > 0)
{
leijia += ami[temp];
temp -= lowbit(temp);
}
d[i] = leijia;
temp = a[i];
while (temp <= 100000)
{
ami[temp]++;
temp += lowbit(temp);
}
}
} void get_ans()
{
for (int i = 2; i <= n - 1; i++)
{
ans += (long long) c[i] * (n - i - d[i]);
ans += (long long ) (i - 1 - c[i]) * d[i];
}
} void output_ans()
{
printf("%lld\n", ans);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--)
{
init();
input_data();
get_ans();
output_ans();
}
return 0;
}

【33.20%】【LA 4320】【Ping pong】的更多相关文章

  1. LeetCode:搜索旋转排序数组【33】

    LeetCode:搜索旋转排序数组[33] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2]  ...

  2. 剑指Offer:二叉搜索树的后序遍历序列【33】

    剑指Offer:二叉搜索树的后序遍历序列[33] 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. ...

  3. JAVA 基础编程练习题20 【程序 20 求前 20 项之和】

    20 [程序 20 求前 20 项之和] 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和. 程序分析:请抓住分子与分母的变化规律. pac ...

  4. django学习-20.python3中的特殊方法【__str__】的作用

    目录结构 1.前言 2.[__str__]特殊方法的具体使用 2.1.当使用print打印一个类被实例化后生成的对象的时候,若类里有定义了[__str__]特殊方法,是打印出这样的数据:[__str_ ...

  5. C# 获取 mp3文件信息【包括:文件大小、歌曲长度、歌手、专辑】

    C# 获取 mp3文件信息[包括:文件大小.歌曲长度.歌手.专辑] 第一种方式:[代码已验证] // http://bbs.csdn.net/topics/390392612   string fil ...

  6. 006-优化web请求二-应用缓存、异步调用【Future、ListenableFuture、CompletableFuture】、ETag、WebSocket【SockJS、Stomp】

    四.应用缓存 使用spring应用缓存.使用方式:使用@EnableCache注解激活Spring的缓存功能,需要创建一个CacheManager来处理缓存.如使用一个内存缓存示例 package c ...

  7. uploadify,实际开发案例【选择完文件点击上传才上传】

    <script type="text/javascript"> )+Math.floor(Math.random()*)+']-'; //设置随机文件前缀. $k(fu ...

  8. 01 语言基础+高级:1-3 常用API第一部分_day08【String类、static、Arrays类、Math类】

    day08[String类.static.Arrays类.Math类] String类static关键字Arrays类Math类 教学目标能够使用String类的构造方法创建字符串对象能够明确Stri ...

  9. Java(133-151)【String类、static、Arrays类、Math类】

    1.字符串概述和特点 string在lang包里面,因此可以直接使用 字符串的内容不可变 2.字符串的构造方法和直接创建 三种构造方法 package cn.itcast.day08.demo01; ...

随机推荐

  1. Funui-Theme 资源的替换

    实现资源的替换,需要分为以下几个步骤 1.找到需要更改的模块 mediatek/packages/apps/FileManager 2.到主题模块下根据包名找到相应资源(以Grass为例) cd ve ...

  2. IO流学习笔记

    1.File类 文件和目录路径名的抽象表示形式. 4种构造方法 File(File parent, String child) File(File parent, String child) File ...

  3. THC=TERMINAL HANDLING CHARGE,碼頭操作費

    THC=TERMINAL HANDLING CHARGE,碼頭操作費

  4. BZOJ2565: 最长双回文串(Manacher)

    Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同).输入长度为n的串S,求S的最长双回文子串T, ...

  5. (转)Tomcat调优

    问题定位 对于Tomcat的处理耗时较长的问题主要有当时的并发量.session数.内存及内存的回收等几个方面造成的.出现问题之后就要进行分析了. 1.关于Tomcat的session数目 这个可以直 ...

  6. Stack switching mechanism in a computer system

    A method and mechanism for performing an unconditional stack switch in a processor. A processor incl ...

  7. 今天看到可以用sqlalchemy在python上访问Mysql

    from sqlalchemy import create_engine, MetaData, and_ 具体的还没有多看.

  8. silverlight依据json字符串动态创建实体类

    1.接收json字符串: //用JsonValue转换json字符串是为了之后获得json字符串的每行数据和每一列的列名 JsonValue jv = JsonValue.Parse(json);   ...

  9. HTML基础-第一讲

    转自:https://blog.csdn.net/likaier/article/details/326639?utm_source=blogxgwz9 HTML是网页主要的组成部分,基本上一个网页都 ...

  10. 设置Maven默认的JDK为1.7,解决Update Maven Project默认为1.5和Maven打包报错2个问题

    1.之前,一直遇到这个问题. Update Maven Project的时候,JDK变成了1.5的.    如果项目中有使用"@overdide",程序就会报错,需要手动修改JRE ...