解题思路:
离散化 + 树状数组
参考代码:
#include<bits/stdc++.h> using namespace std; const int SIZE = 1024; struct datas { int num, dis; } Num[SIZE]; int BIT[SIZE], total; bool cmp(datas A, datas B) { return A.num < B.num; } int query(int L) { int res = 0; while (L) { res += BIT[L]; L -= L & (-L); } return res; } void updata(int pos, int V) { while (pos <= total) { BIT[pos] += V; pos += pos & (-pos); } } int main() { scanf("%d", &total); for (int i = 1; i <= total; i++) { scanf("%d", &Num[i].num); Num[i].dis = i; } sort(Num + 1, Num + total + 1, cmp); for (int pos = 1; pos <= total; pos++) updata(pos, 1); int ans = 0; for (int i = 1; i <= total; i++) { ans += query(Num[i].dis - 1); updata(Num[i].dis, -1); } printf("%d", ans); }
0.0分
0 人评分
C语言训练-求PI* (C语言代码)浏览:883 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码) 如果a<0,那就不是这个代码了,哈哈哈哈浏览:1227 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:518 |
2^k进制数 (C语言描述,蓝桥杯)浏览:1420 |
1048题解(读入回车问题)浏览:554 |
核桃的数量 (C语言代码)浏览:870 |
简单的a+b (C语言代码)浏览:506 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:440 |
1202题解浏览:610 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:1189 |