【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]++; //只加1temp += 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;
}

转载于:https://www.cnblogs.com/AWCXV/p/7632256.html

【33.20%】【LA 4320】【Ping pong】相关推荐

  1. 【33】解读TPU:设计和拆解一块ASIC芯片

    [计算机组成原理]学习笔记--总目录 [33]解读TPU:设计和拆解一块ASIC芯片 引言 一.TPU V1 想要解决什么问题? 二.深入理解 TPU V1 1)快速上线和向前兼容,一个 FPU 的设 ...

  2. 【Deepin 20系统】Linux系统安装RTX 2070的NVIDIA卡驱动

    目录 1 查看显卡型号 2 下载驱动 3 安装编译环境 4 Bios关闭Security boot 5 禁用NVIDIA开源驱动nouveau 6 安装NVIDIA闭源驱动 6.1 卸载已经安装的驱动 ...

  3. 【Deepin 20系统】华硕主板Z390设置BIOS后做Deepin 20系统

    这里写目录标题 1 做系统盘 2 设置BIOS 3 分区 4 开始Deepin系统的世界 5 Deepin系列问题 1 做系统盘 准备大于8G的U盘 进入官网Deppin 官网点击下载镜像文件,可以把 ...

  4. UOJ Round #20 T1 A. 【UR #20】跳蚤电话(组合数+树形DP)

    UOJ Round #20 T1 A. [UR #20]跳蚤电话 题目大意 给出一棵树,求建出该树的不同操作方案数.建树方式如下:初始 S S S集合只有 1 1 1,操作 1 1 1为取已连的边 x ...

  5. 【世界名胜大观:最著名的50个景点】

    [世界名胜大观:最著名的50个景点] 第1位-美国大峡谷-The Grand Canyon 美国大峡谷是一个举世闻名的自然奇观,位于西部亚利桑那州西北部的凯巴布高原上,总面积2724.7平方公里.由于 ...

  6. 【网友评出的得分最高的100部电影】你有多少部没看过?留着找时间看咯!!

    [网友评出的得分最高的100部电影]你有多少部没看过?留着找时间看咯!! 时光网评出的得分最高100部电影! 9.3 1 肖申克的救赎 The Shawshank Redemption 9.2 2 盗 ...

  7. 【网友评出的得分最高的100部电影】你有多少部没看过?留着找时间看咯!!...

    [网友评出的得分最高的100部电影]你有多少部没看过?留着找时间看咯!! 时光网评出的得分最高100部电影! 9.3 1 肖申克的救赎 The Shawshank Redemption 9.2 2 盗 ...

  8. oracle设置缓存大小设置多少,【数据库类※从V$DB_CACHE_ADVICE中设置数据缓冲大小※】...

    [数据库类※从V$DB_CACHE_ADVICE中设置数据缓冲大小※]     Oracle 9i引入了一个新的途径来预测数据缓冲cache中附加数据缓存的所带来的好处的多少.V$DB_CACHE_A ...

  9. 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(8) 权限管理,自定义权限,扩展权限...

    索引 [无私分享:从入门到精通ASP.NET MVC]从0开始,一起搭框架.做项目 目录索引 简述 今天我们来做权限的管理,这篇比较多 希望新手朋友慢慢消化 项目准备 我们用的工具是:VS 2013 ...

  10. 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(5.5) 登录功能的实现,完善登录功能...

    索引 [无私分享:从入门到精通ASP.NET MVC]从0开始,一起搭框架.做项目 目录索引 简述 今天我们来完善我们的登录功能 项目准备 我们用的工具是:VS 2013 + SqlServer 20 ...

最新文章

  1. JDBC-Statement 对象
  2. sliverlight--无法启动调试。
  3. 微信小程序适配iphonex
  4. tomcat配置及其中部分目录作用虚拟路径虚拟主机
  5. 【Linux】安装前的准备-----安装纯净版的虚拟机的步骤
  6. PHP之wampserver修改根目录与默认页面
  7. 一起谈.NET技术,ASP.NET MVC 通过 FileResult 向浏览器发送文件
  8. 经验 | 我的研究生这三年
  9. 基于CloudStack+KVM的企业私有云的实现
  10. Java基础IO流(案例源码)【六】
  11. node.js--尝试做一个crub
  12. c语言实现审查元素,如何删除qq空间说说?一键自动删除QQ空间说说审查元素代码分享(超简单)...
  13. 小牛叔讲Python第6章:for循环-能计数的循环1234567
  14. 人工智能和计算机程序有什么区别,人工智能和机器学习之间有什么区别?
  15. 【CV学习笔记】图像预处理warpaffine
  16. Linux打印口/LPT口出厂测试工具与使用说明
  17. python程序运行时间计时
  18. 自媒体短视频搬运如何伪原创上热门!老司机教你伪原创短视频的做法
  19. IP地址、手机归属和身份证查询接口
  20. 余数和除数以及被除数的关系

热门文章

  1. torchvision.transforms
  2. go bson转换成json_如何快速提升 Go 程序性能?
  3. 【数据库课程设计】金融数据库设计与实现
  4. 使用hive计算每一年的最大气温的日期+温度
  5. axios 请求接口获取文件流数据导出数据到excel,解决乱码
  6. “最快3个月复制支付宝的核心能力” 解密蚂蚁金服bPaaS
  7. WebSocket协议:5分钟从入门到精通
  8. Linux学习134 Unit 4
  9. CDlinux制作U盘启动盘,打造自己的口袋系统
  10. linux目录跳转快捷方式——z武器