LA 4329(树状数组)
题目描述:
N <tex2html_verbatim_mark>(3N
20000) <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>(1T
20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T <tex2html_verbatim_mark>lines each of which describes a test case.
Every test case consists of N + 1 <tex2html_verbatim_mark>integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N <tex2html_verbatim_mark>distinct integers a1, a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1ai
100000 <tex2html_verbatim_mark>, i = 1...N <tex2html_verbatim_mark>).
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 分析:对于每个a[i],算出从a[1]到a[i-1]比a[i]小的数有c[i]个,a[i+1]到a[n]比a[i]小的有d[i]个,则考虑当i为裁判时,可能的方案数为:c[i](n-i-d[i])+d[i](i-c[i]-1)。关键是算c[i]、d[i]。运用树状数组可以算出c[i],d[i]。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
const int maxn=;
const int maxv=;
int a[maxn],c[maxv],cc[maxn],d[maxn],n;
int lowbit(int x){
return x&-x;
}
int sum(int x){
int res=;
while(x>){
res+=c[x];
x-=lowbit(x);
}
return res;
}
void add(int x,int v){
while(x<=maxv){
c[x]+=v;
x+=lowbit(x);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n-;i++){
cc[i]=sum(a[i]-);
add(a[i],);
}
memset(c,,sizeof(c));
for(int i=n;i>=;i--){
d[i]=sum(a[i]-);
add(a[i],);
}
ll ans=;
for(int i=;i<=n-;i++)
ans+=cc[i]*(n-i-d[i])+(i-cc[i]-)*d[i];
printf("%lld\n",ans);
}
return ;
}
LA 4329(树状数组)的更多相关文章
- LA 4329 (树状数组) Ping pong
第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到10 ...
- poj Ping pong LA 4329 (树状数组统计数目)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 Accepted: 879 Descript ...
- 算法竞赛入门经典 LA 4329(树状数组)
题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...
- UVALive 4329 树状数组第二题
大白书上的题目,比较巧妙的是其分析,为了求某个i点做裁判的时候的情况数,只要知道左边有多少比它小的记为ansc,右边有多少比它小的记为ansd,则总种数,必定为 ansc*(右边总数-ansd)+an ...
- TTTTTTTTTTTTT LA 2191 树状数组 稍修改
题意:给出n个数字,操作有修改(S)和输出区间和(M). #include <iostream> #include <cstdio> #include <cstring& ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA 4329 ping-pong树状数组
题目链接: 刘汝佳,大白书,P197. 枚举裁判的位置,当裁判为i时,可以有多少种选法,如果已经知道在位置i之前有ci个数比ai小,那么在位置i之前就有i-1-ci个数比ai大. 在位置i之后有di个 ...
- 树状数组 LA 4329 亚洲赛北京赛区题
复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...
- LA 4329(树状数组)
算法竞赛入门经典 p197 题目大意: 一条大街上住着n个乒乓球爱好者.常常比赛切磋技术.每一个人都有一个不同的技能值a[i].每场比赛须要3个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在 ...
随机推荐
- Android Training精要(一)ActionBar上级菜单导航图标
Navigation Up(ActionBar中的上级菜单导航图标) 在android 4.0中,我们需要自己维护activity之间的父子关系. 导航图标ID为android.R.id.home @ ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- Process.StandardInput属性
获取用于写入应用程序输入的流. 命名空间:System.Diagnostics程序集:System(在 system.dll 中) 语法 C# C++ VB public StreamWr ...
- 【HDOJ】1059 Dividing
多重背包. #include <stdio.h> #include <string.h> #define mymax(a, b) (a>b) ? a:b ]; ]; vo ...
- Android开发学习笔记:浅谈WebView
本文转自:http://www.2cto.com/kf/201108/101518.html WebView(网络视图)能加载显示网页,可以将其视为一个浏览器.它使用了WebKit渲染引擎加载显示网页 ...
- devi into python 笔记(四)python的类
import 与 from ... import ...: #types只是一个包,FunctionType只是这个包里的一个函数.这里用它来演示 import types #如果要用Function ...
- Nido.Common.Utilities.MD5类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- MFC如何获取编辑框中输入的内容
1.GetDlgItemText() 2.先用 GetDlgItem(编辑框的ID)获取指向编辑框的指针.再用GetWindowText函数将获取内容保存至指定的字符数组里. 3.使用 GetDlgI ...
- Linux常用命令收集
rsync -avH --progress felix/ /home/magnum/work-environment/ 同步本地文件夹,异常中断后仍然可以续传 rsync -avH --progres ...
- Eclipse常用插件推荐
Eclipse Web Tools Platform(WTP) 地址:http://download.eclipse.org/webtools/ WTP十分强大,支持HTML, JavaScript, ...