codeforces 830 B. Cards Sorting(线段树)
题目链接:http://codeforces.com/contest/830/problem/B
题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有几个数优先往后面找如果后面没了再轮到前面找。可以开始将每个数的下表定为1然后拿掉之后就为0然后两值之间有几个数就用区间求和来求。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 1e5 + ;
typedef long long ll;
int a[M] , b[M];
struct TGP {
int Min , pos;
TGP() {}
TGP(int Min , int pos):Min(Min), pos(pos) {}
};
struct TnT {
int l , r , sum;
TGP gp;
}T[M << ];
void push_up(int i) {
T[i].sum = T[i << ].sum + T[(i << ) | ].sum;
if(T[i << ].gp.Min <= T[(i << ) | ].gp.Min) {
T[i].gp = T[i << ].gp;
}
else T[i].gp = T[(i << ) | ].gp;
}
void build(int l , int r , int i) {
int mid = (l + r) >> ;
T[i].l = l , T[i].r = r;
if(l == r) {
T[i].gp.Min = a[l];
T[i].gp.pos = l;
T[i].sum = ;
return ;
}
build(l , mid , i << );
build(mid + , r , (i << ) | );
push_up(i);
}
void update(int pos , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == T[i].r && T[i].l == pos) {
T[i].gp.Min = inf;
T[i].sum = ;
return ;
}
if(pos > mid) update(pos , (i << ) | );
else update(pos , i << );
push_up(i);
}
TGP query(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
return T[i].gp;
}
if(mid < l) return query(l , r , (i << ) | );
else if(mid >= r) return query(l , r , i << );
else {
TGP a1 = query(l , mid , i << ) , a2 = query(mid + , r , (i << ) | );
if(a1.Min > a2.Min) return a2;
else return a1;
}
}
int getsum(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
return T[i].sum;
}
if(mid < l) return getsum(l , r , (i << ) | );
else if(mid >= r) return getsum(l , r , i << );
else return getsum(l , mid , i << ) + getsum(mid + , r , (i << ) | );
}
int main() {
int n;
scanf("%d" , &n);
for(int i = ; i <= n ; i++) scanf("%d" , &a[i]) , b[i] = a[i];
sort(b + , b + n + );
build( , n , );
ll ans = ;
int now = ;
for(int i = ; i <= n ; i++) {
TGP gg = query(now , n , );
//cout << gg.Min << ' ' << gg.pos << endl;
if(gg.Min == b[i]) {
ans += getsum(now , gg.pos , );
//cout << "ans: " << ans << endl;
now = gg.pos + ;
if(now > n) now = ;
update(gg.pos , );
}
else {
TGP gb = query( , now , );
//cout << gb.Min << ' ' << gb.pos << endl;
if(gb.Min == b[i]) {
ans += getsum(now , n , );
ans += getsum( , gb.pos , );
now = gb.pos + ;
if(now > n) now = ;
update(gb.pos , );
}
}
}
printf("%lld\n" , ans);
return ;
}
codeforces 830 B. Cards Sorting(线段树)的更多相关文章
- codeforces 830 B Cards Sorting
B. Cards Sorting http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- 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 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
随机推荐
- 【JDK】JDK源码分析-Vector
概述 上文「JDK源码分析-ArrayList」主要分析了 ArrayList 的实现原理.本文分析 List 接口的另一个实现类:Vector. Vector 的内部实现与 ArrayList 类似 ...
- 【JDK】JDK源码分析-ArrayList
概述 ArrayList 是 List 接口的一个实现类,也是 Java 中最常用的容器实现类之一,可以把它理解为「可变数组」. 我们知道,Java 中的数组初始化时需要指定长度,而且指定后不能改变. ...
- 【Python-Django定义用户模型类】Python-Django定义用户模型类详解!!!
定义用户模型类 1. Django默认用户认证系统 Django自带用户认证系统 它处理用户账号.组.权限以及基于cookie的用户会话. Django认证系统位置 django.contrib.au ...
- web渗透---第二天
协议常识 HTTP协议 百度百科的解释:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议. 所有的WWW文件都必须遵守这个标准. ...
- spark shuffle写操作三部曲之BypassMergeSortShuffleWriter
前言 再上一篇文章 spark shuffle的写操作之准备工作 中,主要介绍了 spark shuffle的准备工作,本篇文章主要介绍spark shuffle使用BypassMergeSortSh ...
- Vue监听键盘回车事件
在写页面时遇见了登录页需要加一个键盘回车事件. vue 的 v-on中有这样的修饰符 <input v-on:keyup.enter="submit"> 即<in ...
- Unity经典游戏教程之:冒险岛
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- 转载:MyBatis mapper.xml中使用静态常量或者静态方法
转自:https://my.oschina.net/wtslh/blog/682704 今天偶然之间刷到了这样一篇博客,有点意外 mybatis 还可以这样使用ONGL常量的方式,该方式针对 xml的 ...
- Fork 多进程 模拟并行访问web service获取响应时间差
#include <ros/ros.h> #include <iostream> #include <string> #include <cstring> ...
- PIP键盘设置实时时钟--智能模块
大家好,许久没来发帖,今天带来点干货.希望大家多多讨论,相互学习. 使用 TOPWAY Smart LCD (HMT050CC-C) PIP键盘设置实时时钟 第一步 建立工程 第二步 建立2 ...