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要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
随机推荐
- (5.13)mysql高可用系列——1主3从复制(SSL)
目录: [0]需求 目前使用Mysql数据库,100GB+数据量,需要实现1主3从环境. 需要实现SSL安全复制,同时需要测试异常宕机切换演练 [1]实验环境 数据库架构:主从复制,基于主库搭建3个从 ...
- WijmoJS V2019.0 Update2发布:再度增强 React 和 Vue 框架的组件功能
前端开发工具包 WijmoJS 在2019年的第二个主要版本 V2019.0 Update2 已经发布,本次发布涵盖了React 和 Vue 框架下 WijmoJS 前端组件的功能增强,并加入更为易用 ...
- Oracle创建表空间、创建用户,给用户分配表空间以及可操作权限
创建表空间一共可分为四个步骤 具体脚本如下: 第1步:创建临时表空间 create temporary tablespace yd_temp tempfile 'D:\oracledata ...
- 我的第一个Java博客
1.2019 11.23 Alone in Beijing;
- 【模板】dijkstra与floyd
(我永远喜欢floyd) 温馨提示:与SPFA一起食用效果更佳 传送门:https://www.cnblogs.com/Daz-Os0619/p/11388157.html Floyd 大概思路: 对 ...
- 【原创】大叔经验分享(57)hue启动coordinator时报错
hue启动coordinator时报错,页面返回undefinied错误框: 后台日志报错: runcpserver.log [13/May/2019 04:34:55 -0700] middlewa ...
- webpack的postcss的基本应用
PostCss是什么? PostCSS在webpack中的基本应用 一.PostCss是什么? 如果有深入学习PostCss需求的话可以参考大漠的资料:https://www.w3cplus.com/ ...
- git commit报错解决,绕过代码检查
上一个项目用的svn,新项目用了git,很开心,终于学习了git了,本以为把git都学会了,但是还是遇到了一个不在自己学习的知识点范围内的问题,最后是同事帮忙解决的. 问题:第一次代码commit的时 ...
- 4.Struts2-OGNL
/*ognl 是 strut2 特有的表达式,使用 ognl,struts2 就无需将对象手动放值进request等范围,页面(从值栈中)直接传值*/ OGNL <?xml version=&q ...
- SmartBinding与kbmMW#3
前言 在SmartBinding #2中,我介绍了新的自动绑定功能,支持在Form设计器中直接定义绑定.不仅如此,kbmMW SmartBind还有更多很酷的功能,即将发布的kbmMW中的SmartB ...