将每个数字的位置存进该数字的vector中

原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案

树状数组维护每个数字现在的位置和原位置之差

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define LL long long
  4. const int N = ;
  5. int n;
  6. int a[N], b[N];
  7. vector<int> v[N];
  8. int c[N];
  9. void modify(int x, int num)
  10. {
  11. while (x <= ) c[x] += num, x += x&-x;
  12. }
  13. int sum(int x)
  14. {
  15. int s = ;
  16. while (x) s += c[x], x -= x&-x;
  17. return s;
  18. }
  19. int main()
  20. {
  21. scanf("%d", &n);
  22. for (int i = ; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
  23. sort(b+, b++n);
  24. for (int i = ; i <= n; i++)
  25. v[a[i]].push_back(i);
  26. LL ans = ;
  27. int now = ;
  28. for (int i = ; i <= n;)
  29. {
  30. int p = lower_bound(v[b[i]].begin(), v[b[i]].end(), now) - v[b[i]].begin();
  31. if (p == )
  32. {
  33. int pos = v[b[i]][v[b[i]].size()-];
  34. ans += pos - sum(pos) - (now - sum(now));
  35. now = pos;
  36. }
  37. else
  38. {
  39. int pos = v[b[i]][p-];
  40. ans += n - sum(n) - (now-sum(now)) + pos - sum(pos);
  41. now = pos;
  42. }
  43. for (int j = ; j < v[b[i]].size(); j++) modify(v[b[i]][j], );
  44. i += v[b[i]].size();
  45. }
  46. cout << ans << endl;
  47. }

CodeForces 830B - Cards Sorting的更多相关文章

  1. Codeforces 830B - Cards Sorting 树状数组

    B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  3. codeforces 830 B Cards Sorting

    B. Cards Sorting  http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组

    E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

    Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. codeforces 830 B. Cards Sorting(线段树)

    题目链接:http://codeforces.com/contest/830/problem/B 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...

  7. Codeforces Round #424 Div2 E. Cards Sorting

    我只能说真的看不懂题解的做法 我的做法就是线段树维护,毕竟每个数的顺序不变嘛 那么单点维护 区间剩余卡片和最小值 每次知道最小值之后,怎么知道需要修改的位置呢 直接从每种数维护的set找到现在需要修改 ...

  8. Codeforces Round #424 E. Cards Sorting

    题目大意:给你一堆n张牌(数字可以相同),你只能从上面取牌,如果是当前牌堆里面最小的值则拿走, 否则放到底部,问你一共要操作多少次. 思路:讲不清楚,具体看代码.. #include<bits/ ...

  9. 【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting

    Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...

随机推荐

  1. The import javax.websocket cannot be resolved的解决问题

    在eclipse中导入项目的时候出现了这个问题,废了我半天劲,才搞明白,把问题记录下来,方便大家以后遇到这问题好处理.提供参考. 出现的问题截图: 因为我用的是tomcat8, 大体步骤:项目上点右键 ...

  2. spring boot 使用elasticsearch

    在文章开始之前我们先来介绍一下elasticsearch 是一个分布式的 RESTful 风格的搜索和数据分析引擎. 查询 : Elasticsearch 允许执行和合并多种类型的搜索 — 结构化.非 ...

  3. SqlServer中获取所有数据库,所有表,所有字段

    原文:SqlServer中获取所有数据库,所有表,所有字段 一.获取所有数据库 select * from master.dbo.SysDatabases 二.获取某个库中所有表 SELECT * F ...

  4. Resistors in Parallel(找规律+大数)

    题意:https://codeforces.com/group/ikIh7rsWAl/contest/254825/problem/E 给你一个n,计算n / Sigma(1~n)的d(是n的只出现一 ...

  5. django时区与时间差的问题

    时区的正确配置方式: # 这里还可以配置成中文 一般用不到 LANGUAGE_CODE = 'en-us' # TIME_ZONE = 'UTC' TIME_ZONE = 'Asia/Shanghai ...

  6. DL4J中文文档/Keras模型导入/函数模型

    导入Keras函数模型 假设使用Keras的函数API开始定义一个简单的MLP: from keras.models import Model from keras.layers import Den ...

  7. mybatis插入数据返回主键

    原来之前一直用错了... keyProperty是表示将返回的主键设置为该方法参数的对应属性中去,而不是用返回值的形式的去获取.

  8. Active Learning 主动学习

    Active Learning 主动学习 2015年09月30日 14:49:29 qrlhl 阅读数 21374 文章标签: 算法机器学习 更多 分类专栏: 机器学习   版权声明:本文为博主原创文 ...

  9. luogu题解 P3709 【大爷的字符串题】

    题目链接: https://www.luogu.org/problemnew/show/P3709 思路: 首先我是没读懂题目的,浏览了讨论区的dalao发现才知道就是求区间众数的出现次数. 然后肯定 ...

  10. postman传数组参数,二维数组,多维数组

    一维数组: 传递: 接收: 二维数组: 传递: 接收: 依此类推,