POJ 3928 & HDU 2492 Ping pong(树阵评价倒数)
主题链接:
PKU:http://poj.org/problem?id=3928
HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492
Description
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
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. (1 <= ai <= 100000, i = 1 ... N).
Output
Sample Input
1
3 1 2 3
Sample Output
1
Source
代码例如以下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100017;
int n;
int a[maxn], c[maxn];
int leftMax[maxn], leftMin[maxn];
int rightMax[maxn], rightMin[maxn];
typedef __int64 LL; int Lowbit(int x) //2^k
{
return x&(-x);
} void update(int i, int x)//i点增量为x
{
while(i <= maxn)//注意此处
{
c[i] += x;
i += Lowbit(i);
}
}
int sum(int x)//区间求和 [1,x]
{
int sum=0;
while(x>0)
{
sum+=c[x];
x-=Lowbit(x);
}
return sum;
} int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
}
memset(c,0,sizeof(c));
for(int i = 1; i <= n; i++)
{
leftMin[i] = sum(a[i]);//计算左边小的个数
leftMax[i] = i-leftMin[i]-1;//计算左边大的个数
update(a[i],1);
}
memset(c,0,sizeof(c));//再次清零
for(int i = n,j = 1; i >= 1; i--,j++)
{
rightMin[i] = sum(a[i]);
rightMax[i] = j-rightMin[i]-1;
update(a[i],1);
}
LL ans = 0;
for(int i = 1; i <= n; i++)
{
ans+=leftMax[i]*rightMin[i] + leftMin[i]*rightMax[i];//交叉相乘取和
}
printf("%I64d\n",ans);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ 3928 & HDU 2492 Ping pong(树阵评价倒数)的更多相关文章
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】
Ping pong Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)
Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...
- HDU 2492 Ping pong (数状数组)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- UVA 1428 - Ping pong(树状数组)
UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
随机推荐
- D3D 扎带 小样本
D3D 符合基本程序 #pragma once #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.li ...
- atitit.提升稳定性---hibernate 添加重试retry 机制解决数据库连接关闭
atitit.提升稳定性---hibernate 添加重试retry 机制解决数据库连接关闭 1. 流程总结 retry(5times).invoke(xxx).test().rest().$() t ...
- JavaEE(14) - JPA关联映射
1. 单向N-1关联映射 2. 单向1-1关联映射 3. 单向1-N关联映射 4. 单向N-N关联映射 5. 双向1-1关联映射 6. 双向1-N关联映射 7. 双向N-N关联映射 1. 单向N-1关 ...
- RAC下一个Fatal NI connect error 12170.错误处理
原创作品,离 "深蓝blog" 博客,欢迎转载,请务必注明转载的来源.否则有权追究版权责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/a ...
- Repair Cisco vpnclient on windows10
Repair Cisco vpnclient on windows10 http://linux48.com/archives/435/ http://bbs.pcbeta.com/viewthrea ...
- 易Android登录Demo
上一页介绍Android项目简单的页面跳转实例,算是对开发环境的熟悉,这一篇将在此基础上增加一些简单的逻辑,实现登录的效果. 登录之前: 登录成功: watermark/2/text/aHR0cDov ...
- Javascript学习6 - 类、对象、继承
原文:Javascript学习6 - 类.对象.继承 Javasciprt并不像C++一样支持真正的类,也不是用class关键字来定义类.Javascript定义类也是使用function关键字来完成 ...
- Winform 实现像菜单一样弹出层
原文:Winform 实现像菜单一样弹出层 在实际工作中,如果能像菜单一样弹出自定义内容,会方便很多,比如查询时,比如下拉列表显示多列信息时,比如在填写某个信息需要查看一些信息树时.这个时候自定义弹出 ...
- 网络资源(6) - EJB视频
2014_08_24 http://v.youku.com/v_show/id_XMjE0NjE3MDA0.html?f=5227828 01_EJB3.0_下载安装与运行jboss -------- ...
- 馋-c语言的规则
在记者采访过程,有着c的认识的情况,有时会被问到有关字符搭配以及运算先后顺序的问题,比方a+++++b的值.++i+++i+++i+i的值等类似的,这都属于c的符号方面的问题.那么如何才干轻而易举的去 ...