CodeForces 830B - Cards Sorting
2017-07-14 20:53
387 查看
将每个数字的位置存进该数字的vector中
原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案
树状数组维护每个数字现在的位置和原位置之差
原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案
树状数组维护每个数字现在的位置和原位置之差
#include <bits/stdc++.h> using namespace std; #define LL long long const int N = 100005; int n; int a , b ; vector<int> v ; int c ; void modify(int x, int num) { while (x <= 100000) c[x] += num, x += x&-x; } int sum(int x) { int s = 0; while (x) s += c[x], x -= x&-x; return s; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b+1, b+1+n); for (int i = 1; i <= n; i++) v[a[i]].push_back(i); LL ans = 0; int now = 0; for (int i = 1; i <= n;) { int p = lower_bound(v[b[i]].begin(), v[b[i]].end(), now) - v[b[i]].begin(); if (p == 0) { int pos = v[b[i]][v[b[i]].size()-1]; ans += pos - sum(pos) - (now - sum(now)); now = pos; } else { int pos = v[b[i]][p-1]; ans += n - sum(n) - (now-sum(now)) + pos - sum(pos); now = pos; } for (int j = 0; j < v[b[i]].size(); j++) modify(v[b[i]][j], 1); i += v[b[i]].size(); } cout << ans << endl; }
相关文章推荐
- Codeforces 830B - Cards Sorting 树状数组
- AC日记——Cards Sorting codeforces 830B
- codeforces 830 B Cards Sorting
- Codeforces-830B:Cards Sorting(树状数组+线段树)
- CodeForces 706B Interesting drink (二分查找)
- Codeforces 779D String Game 二分
- codeforces 839d Winter is here
- CodeForces 471C - MUH and House of Cards(推导)
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
- CodeForces 706C Hard problem (水DP)
- codeforces 777C Alyona and Spreadsheet(预处理+思维)
- Codeforces 839E Mother of Dragons(极大团)
- codeforces 148 D. Bag of mice 概率dp
- Codeforces 609C Load Balancing 【水题】
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
- Codeforces 779B-Weird Rounding
- 【CodeForces】616C - The Labyrinth
- codeforces-#472B. Design Tutorial: Learn from Life(贪心)
- Codeforces Good Bye 2015 ABCDE
- Codeforces 706c dp