HDU2491 Priest John's Busiest Day
题意:
有n个人要进行乒乓球比赛,每一个人都一个能力值。每一个人出现的次序就是他们住的位置
如今要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间
问这样的比赛一共能够进行多少次
思路:
用树状数组做,否则TLE,先从左到右扫一遍,计算每点左边大的个数和小的个数,
再从右到左扫一遍,计算每点右边大和小的个数,然后交叉相乘取和就能够了
代码例如以下:
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int n;
int p[N], c[N], li[N], la[N], ri[N], ra[N]; inline int Lowbit(int x){ return x&(-x); } void change(int u, int x)
{
while(u < N)
{
c[u] += x;
u += Lowbit(u);
}
} int get_sum(int x)
{
int ans = 0;
for(int i = x; i > 0; i -= Lowbit(i))
{
ans += c[i];
}
return ans;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(c, 0, sizeof(c));
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &p[i]);
int cnt = get_sum(p[i]);
li[i] = cnt; // i点左边比它小的
la[i] = i - cnt - 1; //i点左边比它大的
change(p[i], 1);
}
memset(c, 0, sizeof(c));
for(int i = n; i > 0; i--)
{
int cnt = get_sum(p[i]);
ri[i] = cnt; // i点右边比它小的
ra[i] = n - i - cnt; //i点右边比它大的
change(p[i], 1);
}
ll ans = 0;
for(int i = 1; i <= n; i++)
{
ans += li[i] * ra[i] + la[i] * ri[i];
}
printf("%I64d\n", ans);
}
return 0;
}
HDU2491 Priest John's Busiest Day的更多相关文章
- POJ 3684 Priest John's Busiest Day 2-SAT+输出路径
强连通算法推断是否满足2-sat,然后反向建图,拓扑排序+染色. 一种选择是从 起点開始,还有一种是终点-持续时间那个点 開始. 若2个婚礼的某2种时间线段相交,则有矛盾,建边. easy出错的地方就 ...
- POJ 3683 Priest John's Busiest Day (2-SAT+输出可行解)
题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...
- 图论(2-sat):Priest John's Busiest Day
Priest John's Busiest Day Description John is the only priest in his town. September 1st is the Jo ...
- POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)
POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...
- 【POJ3683】Priest John's Busiest Day
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- poj 3686 Priest John's Busiest Day
http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ...
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- Priest John's Busiest Day(POJ 3683)
原题如下: Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12162 ...
随机推荐
- Crontab无法自动执行,直接运行脚本却能执行
Crontab无法自动执行,直接运行脚本却能执行 http://bbs.chinaunix.net/thread-1926428-1-1.html crontab -e crontab内容为* * * ...
- Jmeter接口测试常见的乱码问题三种解决方法
使用Jmeter时经常遇到中文乱码问题,下面总结三种常用的解决方式. 1. 2.在Jmeter安装文件bin中找到jmeter.properties,打开jmeter.properties,搜索“IS ...
- 【 APACHE 】 Apache2.4.x版本虚拟主机配置
今天准备使用apache搭建一个目录浏览的服务,折腾了一下. apache2.4.x以后的版本: Require all granted 代替了apache2.4.x以前版本: Order Allow ...
- C++类中引用成员和常量成员的初始化(初始化列表)
如果一个类是这样定义的: Class A { public: A(int pram1, int pram2, int pram3); privite: int a; int &b; const ...
- 利用beamer做幻灯片插入EPS图片的方法
可以利用epstopdf宏包,在命令行下利用pdflatex带"-shell-escape"参数编译. 先插入epstopdf宏包:\usepackage{graphicx}\ ...
- Nginx-Primary script unknown的报错的解决方法
配置nginx时一直报错:file not found 错误日志: [error] 12691#0: *6 FastCGI sent in stderr: "Primary script u ...
- [centos6.5] 完全卸载httpd mysql php
rpm -qa|grep mysql # 列出所有mysql相关包 rpm -e 包名 # 逐一卸载,一个方便技巧是:卸载时可以不带版本,比如 # mysqlclient10-3.23.58-4.RH ...
- HDU 2568 前进(模拟,水)
轻松通过墓碑,进入古墓后,才发现里面别有洞天.突然,Yifenfei发现自己周围是黑压压的一群蝙蝠,个个扇动翅膀正准备一起向他发起进攻!形势十分危急!好在此时的yifenfei已经不是以前那个经常 ...
- CSS 从入门到放弃系列:CSS的引入方式
css的四种引入方式 内联方式(行间样式) <div style="width:100px;height: 100px; background-color: red"> ...
- 31、Django实战第31天:我的课程
1.编辑usercenter-mycourse.html继承usercenter-base.html 2.编辑users.views.py ... from operation.models impo ...