【33.20%】【LA 4320】【Ping pong】
【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】的更多相关文章
- LeetCode:搜索旋转排序数组【33】
LeetCode:搜索旋转排序数组[33] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ...
- 剑指Offer:二叉搜索树的后序遍历序列【33】
剑指Offer:二叉搜索树的后序遍历序列[33] 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. ...
- JAVA 基础编程练习题20 【程序 20 求前 20 项之和】
20 [程序 20 求前 20 项之和] 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和. 程序分析:请抓住分子与分母的变化规律. pac ...
- django学习-20.python3中的特殊方法【__str__】的作用
目录结构 1.前言 2.[__str__]特殊方法的具体使用 2.1.当使用print打印一个类被实例化后生成的对象的时候,若类里有定义了[__str__]特殊方法,是打印出这样的数据:[__str_ ...
- C# 获取 mp3文件信息【包括:文件大小、歌曲长度、歌手、专辑】
C# 获取 mp3文件信息[包括:文件大小.歌曲长度.歌手.专辑] 第一种方式:[代码已验证] // http://bbs.csdn.net/topics/390392612 string fil ...
- 006-优化web请求二-应用缓存、异步调用【Future、ListenableFuture、CompletableFuture】、ETag、WebSocket【SockJS、Stomp】
四.应用缓存 使用spring应用缓存.使用方式:使用@EnableCache注解激活Spring的缓存功能,需要创建一个CacheManager来处理缓存.如使用一个内存缓存示例 package c ...
- uploadify,实际开发案例【选择完文件点击上传才上传】
<script type="text/javascript"> )+Math.floor(Math.random()*)+']-'; //设置随机文件前缀. $k(fu ...
- 01 语言基础+高级:1-3 常用API第一部分_day08【String类、static、Arrays类、Math类】
day08[String类.static.Arrays类.Math类] String类static关键字Arrays类Math类 教学目标能够使用String类的构造方法创建字符串对象能够明确Stri ...
- Java(133-151)【String类、static、Arrays类、Math类】
1.字符串概述和特点 string在lang包里面,因此可以直接使用 字符串的内容不可变 2.字符串的构造方法和直接创建 三种构造方法 package cn.itcast.day08.demo01; ...
随机推荐
- jquery constructor
function F(){ this.a = "aaa"; alert(111); } F.prototype = { constructor:F, } var f = new F ...
- 点击事件-click,longclick
今天在修改一个问题的时候,遇到了click,longclick事件触发情况.记录下来. 代码 tView.setOnLongClickListener(new OnLongClickListener( ...
- SQL2012的新分页方法
SELECT BusinessEntityID , FirstName , LastName FROM Person.Person ORDER BY BusinessEntityID OFFSET ( ...
- IPod在Linux下的实战
刚收到一个朋友送的Ipod,经过研究今天我为大家分享一点在Linux系统下使用的经验.Apple的iPod它炫目时尚,超薄还可以俘储大量歌曲,这使得iPod十分流行.但流行的同时也带来了一些问题, 在 ...
- 洛谷——V1772 巧妙填数
描述 将1,2,\cdots,91,2,⋯,9共99个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:31:2:3的比例. 试求出所有满足条件的三个三位数.例如:三个三位数192,384, ...
- 90.#define高级用法
define把参数变成字符串 #define f(x) printf("%s",#x); define连接两个字符串 #define a(x) a##x define把参数变成字符 ...
- solr索引创建流程
solr索引创建流程: 分词组件Tokenizer 分词组件(Tokenizer)会做以下几件事情(这个过程称为:Tokenize),处理得到的结果是词汇单元(Token). 1.将文档分成一个一个单 ...
- 第二遍回顾--①前端flex布局
1.flex: 弯曲,收缩 2.概念 2条主轴,main axis,cross axis; 每个单元为flex item,主轴空间main size,交叉轴空间cross size; 3.容器 .co ...
- js原生代码实现鼠标拖拽(超简单)
首先先来看这一张图 在这种图中,盒子的大小为512px,并且margin-left:-250px margin-top:140px;并通过一些样式让其在中部显示 这些样式都不是重要的,这里加个marg ...
- Flask项目之手机端租房网站的实战开发(八)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...