【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong
题目:
| Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
N(3N
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. 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(1T
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 N distinct integers a1, a2...aN follow, indicating the skill rank of each player, in the order of west to east ( 1ai
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 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 思路:
求一共能组织多少种比赛。如果裁判位于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的更多相关文章
- UVALive 4329 Ping pong
Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Fo ...
- UVALive 4329 Ping pong(树状数组)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- UVALive 4329 Ping pong (BIT)
枚举中间的人,只要知道在这个人前面的技能值比他小的人数和后面技能值比他小的人数就能计算方案数了,技能值大的可有小的推出. 因此可以利用树状数组,从左到右往树上插点,每个点询问sum(a[i]-1)就是 ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- 【暑假】[实用数据结构]UVAlive 3026 Period
UVAlive 3026 Period 题目: Period Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
UVAlive X-Plosives 思路: “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物” 如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到K ...
随机推荐
- 汇编之FS段寄存器
FS寄存器指向当前活动线程的TEB结构(线程结构) 偏移 说明 000 指向SEH链指针 004 线程堆栈顶部 008 线程堆栈底部 00C SubSystemTib 010 FiberD ...
- AxureRP制作Tab标签
1.添加动态Panel 2.双击进入编辑动态Panel 3.点击一个面板状态,编辑全部状态 4.选择虚线框,画出两个矩形图,一大一小:
- hdu 3646
DP 状态转移方程还是比较容易想到 关键问题是当前要攻击的怪兽的血量 dp[i][j] = max(dp[i-1][j]+第i只鸟不使用double可杀死的怪兽数, dp[i-1][j-1]+第i ...
- uva 11039
水题 排序 判符号 #include <cstdio> #include <cstring> #include <algorithm> using namespa ...
- hdu 4403
水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...
- Firefly Http通信简单介绍
原地址:http://www.9miao.com/question-15-54042.html 首先创建firefly工程,firefly-admin.py createproject httptes ...
- Apache Flume 简介
转自:http://blog.163.com/guaiguai_family/blog/static/20078414520138100562883/ Flume 是 Cloudera 公司开源出来的 ...
- 玩转redis
http://www.cnblogs.com/huangxincheng/p/5002794.html
- Linux中的栈:用户态栈/内核栈/中断栈
http://blog.chinaunix.net/uid-14528823-id-4136760.html Linux中有多种栈,很容易弄晕,简单说明一下: 1.用户态栈:在进程用户态地址空间底部, ...
- 基于http.sys来开发的,真的是非常稳定
真正的WEB服务器是不会用Indy写的.因为它是基于每连接每线程的. 其实真正的服务器需要下很多功夫,无法快速开发的.比如说,字符串处理.玩服务器基本上就是玩内存.举个例子: var str:Ansi ...