POJ 3928 Ping pong(树状数组+两次)
题意:每个人都有一个独特的排名(数字大小)与独特的位置(从前往后一条线上),求满足排名在两者之间并且位置也在两者之间的三元组的个数
思路:单去枚举哪些数字在两者之间只能用O(n^3)时间太高,但是可以转变思想。我们可以转化为对于每个数字a,求出后面比当前数a大的每个数b,再求出数b后面比当前数b大的数字c的个数,接着对于每个a对应的每个b中c的个数之和就是一半结果,当然还有比他小的是另一半求法类似。其实就是树状数组求逆序数第一次求出每个b,接着再用b求出c,这样求两次就好
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
struct node
{
int val,pos;
}num[Max];
int n;
ll bit[Max];
int lowbit(int x)
{
return x&(-x);
}
void Add(int x,int y)
{
while(x<=n)
{
bit[x]+=(ll)y;
x+=lowbit(x);
}
return;
}
ll Sum(int x)
{
ll sum=0ll;
while(x>)
{
sum+=bit[x];
x-=lowbit(x);
}
return sum;
}
ll Manx(int n,int *pop,int *ivsn,int *ivsn2,int hh)
{
ll sum=0ll,manx=0ll;
memset(bit,0ll,sizeof(bit));
for(int i=n;i>;--i)
{
if(hh)
ivsn[i]=sum-Sum(pop[i]);
else
ivsn[i]=Sum(pop[i]);
Add(pop[i],ivsn2[i]);
sum+=ivsn2[i];
manx+=ivsn[i];
}
return manx;
}
int pop[Max],ivsn[Max],ivsn2[Max];
bool cmp(struct node p1,struct node p2)
{
return p1.val<p2.val;
}
int main()
{
int t;
ll ans;
scanf("%d",&t);
while(t--)
{
ans=0ll;
scanf("%d",&n);
for(int i=;i<=n;++i)
{
scanf("%d",&num[i].val);
num[i].pos=i;
ivsn2[i]=;
}
sort(num+,num+n+,cmp);
for(int i=;i<=n;i++)
pop[num[i].pos]=i;//离散化
Manx(n,pop,ivsn,ivsn2,);//找出pop数组每个数后面比当前大的每个位置的ivsn2数组值的总和
ans+=Manx(n,pop,ivsn2,ivsn,);
for(int i=;i<=n;i++)
ivsn2[i]=;
Manx(n,pop,ivsn,ivsn2,);//找出pop数组每个数后面比当前小的每个位置的ivsn2数组值的总和
ans+=Manx(n,pop,ivsn2,ivsn,);
printf("%I64d\n",ans);
}
return ;
}
POJ 3928 Ping pong(树状数组+两次)的更多相关文章
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
- 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个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...
- LA4329 Ping pong 树状数组
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- POJ 3928 Ping pong
题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...
随机推荐
- js 正则匹配 域名【host】
如果直接在js中是可以直接取到hostname的,以下方式是通过正则匹配: var url = "http://www.cnblogs.com/cench" var reg = / ...
- oracle中把函数的执行权限赋个某个用户
赋权:grant execute on function1 to ucr_dtb1;收回执行权限:revoke execute on function1 from ucr_dtb1; 在ucr_dtb ...
- 详细解读:远程线程注入DLL到PC版微信
一.远程线程注入的原理 1.其基础是在 Windows 系统中,每个 .exe 文件在双击打开时都会加载 kernel32.dll 这个系统模块,该模块中有一个 LoadLibrary() 函数,可以 ...
- [译]GLUT教程 - 每秒帧数
Lighthouse3d.com >> GLUT Tutorial >> Extras >> Frames per Second 你的程序实际上跑得多快? 有时我们 ...
- mybatis 单一参数时的动态语句
public void getBookList(String publisher,String author){ Map<String,Object> maps = new HashMap ...
- iOS引用当前显示的UIAlertView
UIAlertView在iOS里和一般的UIView不一样,有时候使用起来会有一些不便.特别要引用当前显示的UIAlertView的时候,就存在一些难度. 在iOS7以前,可以下面的代码可以解决这个问 ...
- Visual Studio的 Apache Cordova 插件CTP3.0发布!
北京时间12号晚23点开始的Connect()活动上,微软发布了一系列激动人心的消息! .NET开源了!以后.NET将可在Linux和Mac OS平台上运行! VS免费了!!如果你是学生,个人开发者, ...
- simple_pool对象池——优化<二>
本文章由cartzhang编写.转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/55051570 作者:car ...
- spring boot集成activemq
spring boot集成activemq 转自:https://blog.csdn.net/maiyikai/article/details/77199300
- Android Studio gradle 文件中 ${supportLibVersion} 用法
一般我们在项目中的gradle会添加如下库文件 dependencies { compile 'com.android.support:appcompat-v7:23.1.0' compile 'co ...