【暑假】[实用数据结构]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 ...
随机推荐
- PAT-乙级-1029. 旧键盘(20)
1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...
- uva 10105
数学 杨辉三角 多项式系数 #include <cstdio> int f[13] = {1}; void init() { for (int i = 1; i < 13; i+ ...
- 【Visual C++】一些开发心得与调试技巧
自己平时收集的一些技巧与心得,这里分享出来,普及一下知识. 1.如何在Release状态下进行调试 Project->Setting=>ProjectSetting对话框,选择Releas ...
- [itint5]Excel数转换
http://www.itint5.com/oj/#23 这里就是26进制的转换,但是要注意没有0,A就是1,Z就是26.所以要想象成从0开始,才能用原来的方法计算. //将十进制数转换为excel数 ...
- D3DXCOLOR 和 D3DCOLOR 和 D3DCOLORVALUE
D3DCOLOR 是一个DWORD 型.第一个byte表示Alpha值,后面三个byte依次是r(红)g(绿)b(蓝)值.32位. 下面是一些关于D3DCOLOR 的宏: D3DCOLOR_ARGB( ...
- HDU1312——Red and Black(DFS)
Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...
- Android隐藏输入法键盘(hideSoftInputFromInputMethod没有效果)
在个别时候,需要强制隐藏Android输入法键盘,如当前键盘正在显示,这个时候点击了侧滑面板,就要强制隐藏输入法键盘.网上常见的方法有: 1. InputMethodManager imm = (In ...
- ripple
ripple模拟器非常好用,chrome上的插件
- 对于eclipse新建maven工程需要注意的地方。
新建项目的时候,如果想配置默认的maven的jre为1.6或者别的. http://hi.baidu.com/hi_hi/item/765ec8bbc49880d384dd79d1 1.cmd命令建立 ...
- jquery 分页控件(一)
以前一直都是用别人的分页控件,虽然用得很爽,但总觉的还是自己写个小插件比较好,这个插件效果.代码等都有参照别人完成的控件.即便功能并不是那么完善,扩展性也不好,bug或许还很多.个人觉得,适合自己用就 ...