CodeForces 830B - Cards Sorting
将每个数字的位置存进该数字的vector中
原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案
树状数组维护每个数字现在的位置和原位置之差
- #include <bits/stdc++.h>
- using namespace std;
- #define LL long long
- const int N = ;
- int n;
- int a[N], b[N];
- vector<int> v[N];
- int c[N];
- void modify(int x, int num)
- {
- while (x <= ) c[x] += num, x += x&-x;
- }
- int sum(int x)
- {
- int s = ;
- while (x) s += c[x], x -= x&-x;
- return s;
- }
- int main()
- {
- scanf("%d", &n);
- for (int i = ; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
- sort(b+, b++n);
- for (int i = ; i <= n; i++)
- v[a[i]].push_back(i);
- LL ans = ;
- int now = ;
- for (int i = ; i <= n;)
- {
- int p = lower_bound(v[b[i]].begin(), v[b[i]].end(), now) - v[b[i]].begin();
- if (p == )
- {
- int pos = v[b[i]][v[b[i]].size()-];
- ans += pos - sum(pos) - (now - sum(now));
- now = pos;
- }
- else
- {
- int pos = v[b[i]][p-];
- ans += n - sum(n) - (now-sum(now)) + pos - sum(pos);
- now = pos;
- }
- for (int j = ; j < v[b[i]].size(); j++) modify(v[b[i]][j], );
- i += v[b[i]].size();
- }
- cout << ans << endl;
- }
CodeForces 830B - Cards Sorting的更多相关文章
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- codeforces 830 B Cards Sorting
B. Cards Sorting http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...
- 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 ...
- 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 ...
- codeforces 830 B. Cards Sorting(线段树)
题目链接:http://codeforces.com/contest/830/problem/B 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...
- Codeforces Round #424 Div2 E. Cards Sorting
我只能说真的看不懂题解的做法 我的做法就是线段树维护,毕竟每个数的顺序不变嘛 那么单点维护 区间剩余卡片和最小值 每次知道最小值之后,怎么知道需要修改的位置呢 直接从每种数维护的set找到现在需要修改 ...
- Codeforces Round #424 E. Cards Sorting
题目大意:给你一堆n张牌(数字可以相同),你只能从上面取牌,如果是当前牌堆里面最小的值则拿走, 否则放到底部,问你一共要操作多少次. 思路:讲不清楚,具体看代码.. #include<bits/ ...
- 【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting
Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
随机推荐
- The import javax.websocket cannot be resolved的解决问题
在eclipse中导入项目的时候出现了这个问题,废了我半天劲,才搞明白,把问题记录下来,方便大家以后遇到这问题好处理.提供参考. 出现的问题截图: 因为我用的是tomcat8, 大体步骤:项目上点右键 ...
- spring boot 使用elasticsearch
在文章开始之前我们先来介绍一下elasticsearch 是一个分布式的 RESTful 风格的搜索和数据分析引擎. 查询 : Elasticsearch 允许执行和合并多种类型的搜索 — 结构化.非 ...
- SqlServer中获取所有数据库,所有表,所有字段
原文:SqlServer中获取所有数据库,所有表,所有字段 一.获取所有数据库 select * from master.dbo.SysDatabases 二.获取某个库中所有表 SELECT * F ...
- Resistors in Parallel(找规律+大数)
题意:https://codeforces.com/group/ikIh7rsWAl/contest/254825/problem/E 给你一个n,计算n / Sigma(1~n)的d(是n的只出现一 ...
- django时区与时间差的问题
时区的正确配置方式: # 这里还可以配置成中文 一般用不到 LANGUAGE_CODE = 'en-us' # TIME_ZONE = 'UTC' TIME_ZONE = 'Asia/Shanghai ...
- DL4J中文文档/Keras模型导入/函数模型
导入Keras函数模型 假设使用Keras的函数API开始定义一个简单的MLP: from keras.models import Model from keras.layers import Den ...
- mybatis插入数据返回主键
原来之前一直用错了... keyProperty是表示将返回的主键设置为该方法参数的对应属性中去,而不是用返回值的形式的去获取.
- Active Learning 主动学习
Active Learning 主动学习 2015年09月30日 14:49:29 qrlhl 阅读数 21374 文章标签: 算法机器学习 更多 分类专栏: 机器学习 版权声明:本文为博主原创文 ...
- luogu题解 P3709 【大爷的字符串题】
题目链接: https://www.luogu.org/problemnew/show/P3709 思路: 首先我是没读懂题目的,浏览了讨论区的dalao发现才知道就是求区间众数的出现次数. 然后肯定 ...
- postman传数组参数,二维数组,多维数组
一维数组: 传递: 接收: 二维数组: 传递: 接收: 依此类推,