【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; ...
随机推荐
- MYSQLMANAGER实例管理器总结
好久没有写文章了,今天来看看MYSQL的实例管理器(MYSQLMANAGER).一.简单介绍:1.MySQL实例管理器(IM)是通过TCP/IP端口运行的后台程序,用来监视和管理MySQL数据库服务器 ...
- linux下多进程的文件拷贝与进程相关的一些基础知识
之前实现了用文件IO的方式能够实现文件的拷贝,那么对于进程而言,我们是否也能够实现呢? 答案是肯定的. 进程资源: 首先我们先回想一下,进程的执行须要哪些资源呢?其资源包含CPU资源,内存资源,当然还 ...
- sql server中新增一条数据后返回该数据的ID
开发中遇到的问题:在新增一条数据后往往不需要返回该数据的ID,但是有的时候可能需要返回该数据的ID以便后面的编程使用. 在这里介绍两种方法: 其一:使用存储过程: create procedure a ...
- windows CE项目开发
软件列表 1.Windows mobile 设备中心 2.Microsoft visual Studio 2008 3.串口调试工具(sscom42.exe) 4.Wince 6.0模拟器 5.vir ...
- c++ 常识
1) 功能:格式化字符串输出 说明:format指定输出格式,后面跟要输出的变量 目前printf支持以下格式: %c 单个字符 ...
- mycat 之datanode datahost writehost readhost 区别(转)
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> &l ...
- HDU 2577 How to Type (线性dp)
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- asp.net--TextBox属性全研究
. .aspx代码例如以下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="T ...
- jquery 获取上一个兄弟元素和下一个兄弟元素
jQuery.prev(),返回上一个兄弟节点,不是所有的兄弟节点 jQuery.prevAll(),返回所有之前的兄弟节点 jQuery.next(),返回下一个兄弟节点,不是所有的兄弟节点 jQu ...
- window.location无法跳转页面的问题
最近在使用 window的location时碰到一个无法跳转页面的问题, 后来在location语句后加了一条这样的语句:window.event.returnValue = false;然后竟然可以 ...