UVALive
来源:互联网 发布:js 检测div大小变化 编辑:程序博客网 时间:2024/06/10 09:43
这题不是一眼题,值得做。
思路:
假设第
那么现在考虑如何求得
注意:答案应该是longlong类型。
AC代码
//#define LOCAL#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define inf 0x3f3f3f3ftypedef long long LL;const int maxn = 20000+5;int cnt[100000+5], Rank[maxn];int left[maxn], right[maxn];int maxRank;int lowbit(int x) { return x&-x;}void init() { memset(cnt, 0, sizeof(cnt));}int getSum(int x) { int res = 0; while(x > 0) { res += cnt[x]; x -= lowbit(x); } return res;}void update(int x, int d) { while(x <= maxRank) { cnt[x] += d; x += lowbit(x); }}void solve(int start, int end, int u, int *a) { for(int i = start; i != end; i += u) { a[i] = getSum(Rank[i]-1); update(Rank[i], 1); }}int main() {#ifdef LOCAL freopen("data.in", "r", stdin); freopen("data.out", "w", stdout);#endif // LOCAL int T, n; scanf("%d", &T); while(T--) { init(); maxRank = -inf; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &Rank[i]); maxRank = max(maxRank, Rank[i]); } solve(0, n, 1, left); init(); solve(n-1, -1, -1, right); LL ans = 0; for(int i = 0; i < n; i++) { //枚举每一位裁判 ans += (LL)left[i] * (n-1-i-right[i]); ans += (LL)right[i] * (i-left[i]); } printf("%lld\n", ans); } return 0;}
如有不当之处欢迎指出!
阅读全文
0 0
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- 设计模式——装饰者模式
- 快速运动的物体穿过目标体的解决办法
- 第一篇博客
- poj 2796 Feel Good
- vscode 快捷键, 插件 资料指南 (持续更新)
- UVALive
- python_jieba分词的使用
- 二维数组
- HTML5 -video和audio的设置
- 存储过程 游标嵌套
- XZ_iOS之Runtime使用运行时获取类的属性列表
- 前段.微信小程序开发Ⅰ(结构、逻辑、工作流)
- 设计模式——工厂方法模式
- 【Java笔记】反射机制中用Class操作的一些演示