UVAlive 4329 Ping pong

题目:

Ping pong
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 is N , 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

1
3 1 2 3

Sample Output

1

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

思路:
求一共能组织多少种比赛。如果裁判位于i,那么这种情况可以组织c[i]*(n-i-d[i]) + d[i]*(i-1-c[i])种比赛,其中c[i]为i左边比a[i]小的数目而d[i]为i右边比a[i]小的数目。问题转化为求c[i]与d[i],引入数组x,x[j]表示到目前为止是否有a[i]=j有则为1,那么c[i]就是x[i]的前缀和。d同理。ans需要枚举裁判的位置才能得到,因此需要不断修改x(add目前考虑位置的x),是动态前缀和的问题。代码由FenwickTree实现。 代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = + ,INF=<<; inline int lowbit(int u) { return u&-u; } struct FenwickTree { //动态范围求和问题
int n;
vector<int> C; //当数组用 void resize(int n){ this->n=n;C.resize(n); } //重新调节大小
void clear(){ fill(C.begin(),C.end(),); } //清零C int sum(int u){
int ret=;
while(u>){
ret += C[u]; u -= lowbit(u);
}
return ret;
}
void add(int u,int d){
while(u <= n){
C[u] += d; u += lowbit(u);
}
}
}; FenwickTree f;
int c[maxn],d[maxn],a[maxn];
int n; int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d",&n);
int maxa=-INF;
FOR(i,,n+) {
scanf("%d",&a[i]);maxa=max(maxa,a[i]);
}
f.resize(maxa); //将f.n载为maxa
f.clear(); //clear1
FOR(i,,n+) {
f.add(a[i],); //x[a[i]]=1
c[i]=f.sum(a[i]-);
//sum(x,1,a[i]-1)
}
f.clear(); //clear2
for(int i=n;i;i--){ //注意是--
f.add(a[i],);
d[i]=f.sum(a[i]-);
} long long ans=;
FOR(i,,n+)
ans += (long long)c[i]*(n-i-d[i]) + (long long)(i-c[i]-)*d[i];
printf("%lld\n",ans);
}
return ;
}
												

【暑假】[实用数据结构]UVAlive 4329 Ping pong的更多相关文章

  1. UVALive 4329 Ping pong

                                      Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Fo ...

  2. UVALive 4329 Ping pong(树状数组)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...

  3. UVALive - 4329 Ping pong 树状数组

    这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...

  4. UVALive 4329 Ping pong (BIT)

    枚举中间的人,只要知道在这个人前面的技能值比他小的人数和后面技能值比他小的人数就能计算方案数了,技能值大的可有小的推出. 因此可以利用树状数组,从左到右往树上插点,每个点询问sum(a[i]-1)就是 ...

  5. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  6. 【暑假】[实用数据结构]UVAlive 3026 Period

    UVAlive 3026 Period 题目: Period   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  7. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  8. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  9. 【暑假】[实用数据结构]UVAlive 3644 X-Plosives

    UVAlive X-Plosives 思路:    “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物”  如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到K ...

随机推荐

  1. myeclipse报jar包missing

    一.问题描述 从版本库中check out项目后,发现项目有“感叹号”,且pom.xml文件有红色的“差号”.如下图: 在error window里可以看到missing jar包的提示,如下: 打开 ...

  2. asp 文件上传(ASPUpload组件上传)

    要实现该功能,就要利用一些特制的文件上传组件.文件上传组件网页非常多,这里介绍国际上非常有名的ASPUpload组件 1 下载和安装ASPUpload   要实现该功能,就要利用一些特制的文件上传组件 ...

  3. leetcode6 Reverse Words in a String 单词取反

    Reverse Words in a String  单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...

  4. HDU4631+Set+最近点对

    题意:一个空平面,每次增加一个点, 其坐标根据上一个点算出:(x[i-1] * Ax + Bx ) mod Cx,(y[i-1] * Ay + By ) mod Cy 求出现有点集中的最近点对的距离的 ...

  5. linux crontab任务调度的使用

    (不推荐)可以直接将要调度的任务写入crontab任务表中 (推   荐)可以先将要完成的任务写入一个shell文件,如myTask.sh(还可能需要将该文件改为可执行的:chmod 744 myTa ...

  6. linux网络环境配置

    第一种方法: (red hat) (1)用root身份登录,运行setup命令进入到text mode setup utility 对网络进行配置,这里可以进行ip,子网掩码,默认网关,dns的设置. ...

  7. 在windows下使用git需要反复输入用户名和密码的问题

    节选自我还在写的git文档中的一部分,用md写的,博客园竟然还不支持markdown,完全没有格式啊,懒得弄了,不过解决方法是没有问题的 在win下使用git,如果没有任何设置,一定会反复输入用户名和 ...

  8. P94、面试题12:打印1到最大的n位数

    题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则打印出1,2,3一直到最大的3位数999. 思路:先把字符串中的每一个数字都初始化为‘0’,然后每一次为字符串表示的数字加1,再打印 ...

  9. 蓝牙(2)用BluetoothAdapter搜索蓝牙设备示例

    注意在搜索之前要先打开蓝牙设备 package com.e.search.bluetooth.device; import java.util.Set; import android.app.Acti ...

  10. WinAPI——钩子函数大全

    SetWindowsHookEx 函数功能:该函数将一个应用程序定义的挂钩处理过程安装到挂钩链中去,您可以通过安装挂钩处理过程来对系统的某些类型事件进行监控,这些事件与某个特定的线程或系统中的所有事件 ...