uvalive 4329(BIT)
来源:互联网 发布:java卸载不了怎么办 编辑:程序博客网 时间:2024/05/21 05:21
题意:一条街上有n个运动员,平时会一起切磋,要求有三个人,住在中间的才可以当裁判,n个人都有对应的技能值,裁判的技能值要比一个人大比一个人小。按住所给出n个人的技能值,问有多少种切磋方式。
题解:如果i当裁判,i左边的比他小的有ai个,右边比他小的有bi个,那么对于i有ai*(n - i - bi) + bi*(i - 1 - ai),然后代入了BIT二叉索引树的思想来解答。
#include <stdio.h>#include <string.h>const int M = 20005;const int N = 100005;int n, a[M], s[N], l[N], r[N];int lowbit(int x) {return x & (-x);}int sum(int x) {int res = 0;while (x > 0) {res += s[x];x -= lowbit(x);}return res;}void add(int x, int d) {while (x <= N) {s[x] += d;x += lowbit(x);}}int main() {int t;scanf("%d", &t);while (t--) {scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &a[i]);memset(s, 0, sizeof(s));for (int i = 1; i <= n; i++) {l[i] = sum(a[i]);//i左边比a[i]小的数字有几个add(a[i], 1);}memset(s, 0, sizeof(s));for (int i = n; i >= 1; i--) {r[i] = sum(a[i]);//i右边比a[i]小的数字有几个add(a[i], 1);}long long res = 0;for (int i = 1; i <= n; i++) {res += l[i] * (n - i - r[i]);res += (i - 1 - l[i]) * r[i];}printf("%lld\n", res);}return 0;}
0 0
- uvalive 4329(BIT)
- uvalive 2191(BIT)
- uvalive 5902(BIT)
- Bit String Reordering UVALive
- UVALive 6832Bit String Reordering
- Uvalive 6832 Bit String Reordering
- UVALive 6832Bit String Reordering
- [2012长春]Bit Magic UVALive
- UVALive 6832Bit String Reordering (模拟)
- UVALive - 6832 Bit String Reordering (找规律)
- UVALive 3565 Bit Compressor (搜索)
- uvalive 4329
- UVALive 3675 Sorted bit sequence(数位dp+二分)
- UVALive 4329 Ping pong
- UVALive 4329 Ping pong
- UVALive 4329 Ping pong
- UVALive - 4329 Ping pong
- UVALive 4329 Ping pong
- echarts函数初始化简单介绍
- 机器狗组装费用 南邮NOJ 1076 优先权队列
- php猴子选大王
- 完全删除MySQL (linux)
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- uvalive 4329(BIT)
- 什么是堆和栈,它们在哪儿?--堆栈
- Spring 定时器的配置
- 欢迎使用CSDN-markdown编辑器
- MySQL表连接图解
- 京东峰值系统设计
- -2147483648
- 关于图片转pdf,pdf提取文字的一些技巧
- 2015博客写作计划