【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. 嵌入式 Linux应用程序如何读取(修改)芯片寄存器的值

    这一问题来自项目中一个实际的需求:我需要在Linux启动之后,确认我指定的芯片寄存器是否与我在uboot的配置一致. 举个例子:寄存器地址:0x20000010负责对DDR2的时序配置,该寄存器是在u ...

  2. CSS笔记 - SVG Polyline 图片绘制动画边框

    <style> div{ width: 420px; height: 200px; background: url('./img/timg.jpg') no-repeat; } polyl ...

  3. BZOJ5020: [THUWC 2017]在美妙的数学王国中畅游(LCT,泰勒展开,二项式定理)

    Description 数字和数学规律主宰着这个世界.   机器的运转,   生命的消长,   宇宙的进程,   这些神秘而又美妙的过程无不可以用数学的语言展现出来.   这印证了一句古老的名言:   ...

  4. 五、Docker+Sqlserver

    原文:五.Docker+Sqlserver sqlserver现在支持linux了.所以可以运行在docker内 一.下载镜像: docker pull microsoft/mssql-server- ...

  5. 关于搭建Session服务器(转载)

    Session服务器配置指南与使用经验 一.摘要 所有Web程序都会使用Session保存数据. 使用独立的Session服务器可以解决负载均衡场景中的Session共享问题.本文介绍.NET平台下建 ...

  6. 1.2 Use Cases中 Commit Log官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Commit Log 提交日志 Kafka can serve as a kind ...

  7. Emgucv 图像操作笔记

    这里记下一些学习过程中的心得和技巧.我用VS2008,C#的平台进行编写. 1.将图片载入PictureBox的方法: Image<Bgr, byte> img = new Image&l ...

  8. Android中Application类的详解:

    Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...

  9. JS里的map与forEach遍历

    map 返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值. var numbers = [3,2,6,3] function func(num){ return num * documen ...

  10. python3 求斐波那契数列(Fibonacci sequence)

    输出斐波那契数列的前多少个数. 利用函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan # ----斐波那契数列( ...