计蒜客 Div2 Math of the Ninja
2017-08-14 13:04
232 查看
STL的find函数竟然不是二分查找!
binary_search竟然返回的是bool!
还是lower_bound/upper_bound好用。。
binary_search竟然返回的是bool!
还是lower_bound/upper_bound好用。。
#ifdef _DEBUG #pragma warning(disable : 4996) #endif #include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <functional> #include <sstream> #include <utility> #include <cstring> #include <cstdio> #include <cstdlib> #include <ctime> #include <cmath> #include <cctype> #define CLEAR(a, b) memset(a, b, sizeof(a)) #define CLOSE() ios::sync_with_stdio(false) #define IN() freopen("in.txt", "r", stdin) #define OUT() freopen("out.txt", "w", stdout) #define PF(a) printf("%d\n", a) #define SF(a) scanf("%d", &a) #define SFF(a, b) scanf("%d%d", &a, &b) #define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define LL long long #define maxn 100005 #define maxm 10005 #define MOD 2000000000 #define INF 10007 using namespace std; //-------------------------CHC------------------------------// //计蒜客 Div2 Math of the Ninja //树状数组 + 离散化 #define lowbit(x) x & -x int n; struct Node { int x, y; bool operator<(const Node &r) const { return x < r.x; } }node[maxn]; int a[maxn]; int bit[maxn]; void Update(int i) { while (i <= n) { bit[i]++; i += lowbit(i); } } int sum(int i) { 4000 int ret = 0; while (i) { ret += bit[i]; i -= lowbit(i); } return ret; } void Compress() { vector<int> v; FOR(i, 0, n) v.push_back(node[i].y); sort(v.begin(), v.end()); FOR(i, 0, n) a[i] = lower_bound(v.begin(), v.end(), node[i].y) - v.begin() + 1; } int main() { int T, kase = 1; SF(T); while (T--) { CLEAR(bit, 0); SF(n); FOR(i, 0, n) SFF(node[i].x, node[i].y); sort(node, node + n); Compress(); //FOR(i, 0, n) printf("a = %d", a[i]); LL ans = 0; FOR(i, 0, n) { int t = a[i]; ans += i - sum(t); Update(t); } printf("Case #%d: %lld\n", kase++, ans); } return 0; }equal_range 用法
// equal_range example #include <iostream> // std::cout #include <algorithm> // std::equal_range, std::sort #include <vector> // std::vector bool mygreater (int i,int j) { return (i>j); } int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds; // using default comparison: std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 bounds=std::equal_range (v.begin(), v.end(), 20); // ^ ^ // using "mygreater" as comp: std::sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10 bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^ std::cout << "bounds at positions " << (bounds.first - v.begin()); std::cout << " and " << (bounds.second - v.begin()) << '\n'; return 0; }归并排序求逆序数
#ifdef _DEBUG #pragma warning(disable : 4996) #endif #include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <functional> #include <sstream> #include <utility> #include <cstring> #include <cstdio> #include <cstdlib> #include <ctime> #include <cmath> #include <cctype> #define CLEAR(a, b) memset(a, b, sizeof(a)) #define CLOSE() ios::sync_with_stdio(false) #define IN() freopen("in.txt", "r", stdin) #define OUT() freopen("out.txt", "w", stdout) #define PF(a) printf("%d\n", a) #define SF(a) scanf("%d", &a) #define SFF(a, b) scanf("%d%d", &a, &b) #define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define LL long long #define maxn 100005 #define maxm 10005 #define MOD 2000000000 #define INF 10007 using namespace std; //-------------------------CHC------------------------------// struct Node { int x, y; bool operator<(const Node &r) const { return x < r.x; } }node[maxn]; int a[maxn], t[maxn]; LL cnt; void Merge(int l, int mid, int r) { int i = l, j = mid, k = 0; while (i < mid && j < r) { if (a[i] <= a[j]) t[k++] = a[i++]; else t[k++] = a[j++], cnt += mid - i; } while (i < mid) t[k++] = a[i++]; while (j < r) t[k++] = a[j++]; FOR(i, 0, k) a[l + i] = t[i]; } void MergeSort(int l, int r) { if (r - l > 1) { int mid = (r + l) >> 1; MergeSort(l, mid); MergeSort(mid, r); Merge(l, mid, r); } } int main() { int T, kase = 1; SF(T); while (T--) { int n; SF(n); FOR(i, 0, n) SFF(node[i].x, node[i].y); sort(node, node + n); FOR(i, 0, n) a[i] = node[i].y; cnt = 0; MergeSort(0, n); printf("Case #%d: %lld\n", kase++, cnt); } return 0; }
相关文章推荐
- 翻译《Secrets of the JavaScript Ninja》—— 第一章 介绍
- Secret of the JavaScript Ninja 学习笔记 - 4
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array
- 《Secrets of the JavaScript Ninja》:JavaScript 之运行时代码
- Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man
- Codeforces Round #251(Div. 2) 439C. Devu and Partitioning of the Array 构造
- 点击不在这个区域部分,div消失,jquery代码 - hiding an element on click of anything else on the page
- secret of the javascript ninja笔记
- Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)
- Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game
- Codeforces Round #333 (Div. 2) E Kleofáš and the n-thlon
- Codeforces Round #428 (Div. 2) Game of the Rows (思维)
- 计蒜客_King of the Waves_dfs
- Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)
- Codeforces Round #342 (Div. 2)--B. War of the Corporations
- Codeforces Round #258 (Div. 2/C)/Codeforces451C_Predict Outcome of the Game(枚举)
- Codeforces Round #342 (Div. 2)-B. War of the Corporations
- Allow user to scroll and maintain position with "Scroll To Bottom of the Div" example
- Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) E. The Supersonic Rocket
- Codeforces Round #342 (Div. 2)-B. War of the Corporations