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要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
随机推荐
- Ruby Rails学习中:关于测试的补充,MiniTest报告程序,Guard自动测试
一. 关于测试的补充 1.MiniTest报告程序 为了让 Rails 应用的测试适时显示红色和绿色,我建议你在测试辅助文件中加入以下内容: (1).打开文件:test/test_helper.rb ...
- Maven学习存档(1)——安装
一.安装 1.1 jdk的下载与安装 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 版本:maven2 ...
- 更改:把redis替换成kafka
之前的流程是:filebeat,redis,logstash,elasticsearch 现在的流程是:filebeat,Kafka(zookeeper),logstash,elasticsearch ...
- java自定义excel
文件下载 本文主要介绍spring boot环境下,利用Apache POI操作Excel,实现Excel文件的在线下载. 首先,我们在这里介绍一下关于ApachePOI中关于HSSF,XSSF和SX ...
- 【原创】Java基础之Nginx缓存
1 proxy_ignore_headers 1.1 Set-Cookie By default, nginx does not caches requests with Set-Cookie. 默认 ...
- HTML5中新增加的结构元素、网页元素和全局属性
HTML5新增的结构元素(新增的都是块元素,独占一行) 1) header 定义了文档的头部区域 <header> <h1>网站标题<h1> </header ...
- css基础(代码)
display: block; /*元素分为三大类,设置元素的显示方式} 行内 inlineli{ ...
- HTML之美化盒子
1. 美化盒子 1.1. 美化文本 1.1.1. 字体大小[font-size]: 字符框的高度,可继承,默认值medium,16px.基准字号:浏览器设置的默认字体大小,通 ...
- 不再显示广告案例(php操作cookie)
1,页面简单结构搭建 ad.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- 第二十六篇 jQuery 学习8 遍历-父亲兄弟子孙元素
jQuery 学习8 遍历-父亲兄弟子孙元素 jQuery遍历,可以理解为“移动”,使用“移动”还获取其他的元素. 什么意思呢?老师举一个例子: 班上30位同学,我是新来负责教这个班学生的老师 ...