【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. js09--函数 call apply

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. Menu-actionBarMenu字体颜色修改

    经常会遇到对menu字体颜色进行修改的情况,今天就遇到了一个.就是在action上有一个menu是黑色的,想要改成白色.方法如下 <style name="Email.Theme&qu ...

  3. 洛谷P3273 [SCOI2011]棘手的操作

    题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作:U x y: 加一条边,连接第x个节点和第y个节点A1 x v: 将第x个节点的权 ...

  4. Objective-C基础笔记(4)Category

    OC中提供了一种与众不同的方式--Category,可以动态的为已经存在的类添加新的行为(方法),这样可以保证类的原始设计规模较小,功能增加时再逐步扩展. 在使用Category对类进行扩展时,不需要 ...

  5. C#截取中英文混合字符串分行显示

    private int GetStrByteLength(string str) { return System.Text.Encoding.Default.GetByteCount(str); } ...

  6. SJTU 3001. 二哥的幸运

    Description 二哥是一个挺二的人,在二哥的世界观中,有些数字代表着幸运,假设在某一天二哥可以看到一个幸运数字,那么他将很高兴.当然,二哥对于幸运的定义也是不同凡响,假设一个数字仅仅包括4或者 ...

  7. Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止)

    Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止) ICS 即Internet Connection Sharing,internet连接共享,可以使局域网上其 ...

  8. ThinkPHP5.0---删除数据

    删除特定记录 public function delete() { // 获取要删除的对象:关键字为16 $Teacher = Teacher::); // 删除对象 $Teacher->del ...

  9. 【AtCoder Regular Contest 082 A】Together

    [链接]点击打开链接 [题意] 给你n个数字,每个位置上的数字可以+1,不变,或-1,每个位置只能操作一次. 操作完之后,让你选一个数字x,然后统计a[i]==x的个数count. 问你count的最 ...

  10. windows下配置mycat与常见问题解决

    mycat官网:http://www.mycat.org.cn/ wiki:https://github.com/MyCATApache/Mycat-Server/wiki MyCat使用Mysql的 ...