uvalive 4329
来源:互联网 发布:mac无法读取移动硬盘 编辑:程序博客网 时间:2024/05/10 15:35
Description
N(3N20000) 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. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee's house. For some reason, the contestants can't choose a referee whose skill rank is higher or lower than both of theirs. The contestants have to walk to the referee's house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the 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(1T20) , 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 isN , the number of players. Then N distinct integers a1,a2...aN follow, indicating the skill rank of each player, in the order of west to east (1ai100000 , i = 1...N ).
Output
For each test case, output a single line contains an integer, the total number of different games.
Sample Input
13 1 2 3
Sample Output
1
题意:有n个运动员,有一定顺序,每个人都有自己的排名,三个人位置升序的人组成一场比赛,中间的那个人作为裁判,并且裁判的排名要在其他两位选手之间,问最多能举行多少场比赛(有一个人不一样便视为不同的比赛)
思路:如果枚举运动员的话,时间复杂度较高,不可取,我们这里枚举每一个人作为裁判,运用树状数组计算出裁判两边排名高与和低与裁判排名的人数,这是树状数组的一个常见的使用情况,计算逆序数。
#include <cstring>#include <cstdio>using namespace std;int a[100006];int lmin[100006],rmin[100006];int c[100006];int low(int x){return x&(-x);}int add(int pos){int tm=pos;while(tm<100006){c[tm]+=1;tm+=low(tm);}return 1;}int sum(int pos){int su=0,tmp=pos;while(tmp>0){su+=c[tmp];tmp-=low(tmp);}return su;}int main(){//freopen("in.txt","r",stdin);int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);memset(c,0,sizeof(c));memset(lmin,0,sizeof(lmin));memset(rmin,0,sizeof(rmin));memset(a,0,sizeof(a));for(int i=1;i<=n;i++){scanf("%d",&a[i]);lmin[i]+=sum(a[i]); add(a[i]);}memset(c,0,sizeof(c));for(int i=n;i>0;i--){rmin[i]+=sum(a[i]);add(a[i]);}long long ans=0;for(int i=2;i<n;i++){ans+=(long long)lmin[i]*(n-i-rmin[i]); //注意这里有两种情况ans+=(long long)rmin[i]*(i-1-lmin[i]);}printf("%lld\n",ans);}}
- uvalive 4329
- UVALive 4329 Ping pong
- UVALive 4329 Ping pong
- UVALive 4329 Ping pong
- UVALive - 4329 Ping pong
- UVALive 4329 Ping pong
- uvalive 4329(BIT)
- UVALive-4329-Ping-pong
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- 小白学开发(iOS)OC_ 字符串的获取 (2015-08-11)
- 设置CListCtrl单元格的颜色
- 01-复杂度2. Maximum Subsequence Sum (25)
- 我的学习笔记1:加密、解密,以及OpenSSL创建私有CA
- 算法学习之排序算法(三)(选择排序法)
- uvalive 4329
- 迅雷赚钱宝 红水晶矿机 WS1408 专用特制盒子 超级项目
- 【工业串口和网络软件通讯平台(SuperIO)教程】六.二次开发导出数据驱动
- 可靠联机的 TCP 协议
- 黑马程序员———Java概述
- Java虚拟机学习 - 体系结构 内存模型
- RSync实现文件备份同步
- 由memcpy strcpy memmove看内存重叠
- hdu2602-Bone Collector (背包)