#424 Div2 E
#424 Div2 E
题意
给出一个 n 个数的数列,从前往后取数,如果第一个数是当前数列的最小值,则取出,否则将它放到数列尾端,问使数列为空需要多少步操作。
分析
用数据结构去模拟。
线段树维护区间最小值及取得最小值的位置。树状数组维护仍存在的数的个数( 1 表示未取,0 表示已取)。
首先寻找全局最小值,那么答案加上它前面的存在的数的个数,然后删掉这个值,(在线段树中将这个值置为无穷大等价于删除掉它)。
设这个前面删掉的最小值位置为 idx,那么再从 idx+1 往后找,是否存在全局最小值。
如果存在下标为 pos ,那么答案加上区间 (idx, pos] 仍然存在的数的个数(使用树状数组计算),再把 idx 置为 pos。
如果不存在,答案加上 (idx, n] 仍然存在的数的个数,退出当前循环,从头开始找(即把 idx 置为 0)。
code
#include<bits/stdc++.h>
#define lson l, m, rt * 2
#define rson m + 1, r, rt * 2 + 1
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
const int INF = 2e9 + 1;
int a[MAXN];
struct BIT {
int f[MAXN];
void add(int p, int c) {
while(p <= MAXN) {
f[p] += c;
p += (p & -p);
}
}
int query(int p) {
int s = 0;
while(p) {
s += f[p];
p -= (p & -p);
}
return s;
}
} bit;
struct ST {
struct node {
int id, val;
node() {}
node(int id_, int val_):id(id_), val(val_) {}
} ary[MAXN << 2];
node pushUp(node& nd, int rt) {
if(ary[2 * rt].val <= ary[2 * rt + 1].val) {
nd.id = ary[2 * rt].id;
nd.val = ary[2 * rt].val;
} else {
nd.id = ary[2 * rt + 1].id;
nd.val = ary[2 * rt + 1].val;
}
}
void build(int l, int r, int rt) {
if(l == r) {
ary[rt].id = l;
ary[rt].val = a[l];
return;
}
int m = (l + r) / 2;
build(lson);
build(rson);
pushUp(ary[rt], rt);
}
void query(int L, int R, int& pos, int& res, int l, int r, int rt) {
if(L <= l && R >= r) {
if(ary[rt].val < res) {
res = ary[rt].val;
pos = ary[rt].id;
}
return;
}
int m = (l + r) / 2;
if(L <= m) query(L, R, pos, res, lson);
if(R > m) query(L, R, pos, res, rson);
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
ary[rt].val = val;
return;
}
int m = (l + r) / 2;
if(m < p) update(p, val, rson);
else update(p, val, lson);
pushUp(ary[rt], rt);
}
} st;
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
bit.add(i, 1);
}
st.build(1, n, 1);
ll ans = 0;
for(int T = 0; T < n;) {
for(int idx = 0; idx < n && T < n;) {
int minV = INF, minv = INF, posV, posv;
st.query(1, n, posV, minV, 1, n, 1);
st.query(idx + 1, n, posv, minv, 1, n, 1);
if(minv != minV) {
ans += bit.query(n) - bit.query(idx);
break;
}
ans += bit.query(posv) - bit.query(idx);
st.update(posv, INF, 1, n, 1);
bit.add(posv, -1);
T++;
idx = posv;
}
}
cout << ans << endl;
return 0;
}
#424 Div2 E的更多相关文章
- #424 Div2 C
#424 Div2 C 题意 给出 k 个分数,初始分数未知,按顺序把这 k 个分数加到初始分数上,已知 n 个加入分数后的结果(无序),问初始分数有多少种可能. 分析 也就是说这 n 个结果,它们之 ...
- Codeforces Round #424 Div2 E. Cards Sorting
我只能说真的看不懂题解的做法 我的做法就是线段树维护,毕竟每个数的顺序不变嘛 那么单点维护 区间剩余卡片和最小值 每次知道最小值之后,怎么知道需要修改的位置呢 直接从每种数维护的set找到现在需要修改 ...
- codeforces round #424 div2
A 暴力查询,分三段查就可以了 #include<bits/stdc++.h> using namespace std; ; int n, pos; int a[N]; int main( ...
- #424 Div2 Problem C Jury Marks (二分 && 暴力 && std::unique && 思维)
题目链接 :http://codeforces.com/contest/831/problem/C 题意 :选手有一个初始积分,接下来有k个裁判为他加分或减分(时间顺序给出),然后告诉你n(1< ...
- bc#54 div2
用小号做的div2 A:竟然看错了排序顺序...白白WA了两发 注意读入一整行(包括空格):getline(cin,st) [gets也是资瓷的 #include<iostream> us ...
- $('div a') 与$('div>a'),.div+.div2与.div~.div2
$('div a'):div标签下所有层次a元素的jquery对象 $('div>a'):div标签下子元素层次a元素的jquery对象 <body> <div class=' ...
- SRM 657 DIV2
-------一直想打SRM,但是感觉Topcoder用起来太麻烦了.题目还是英文,不过没什么事干还是来打一打好了.但是刚注册的号只能打DIV2,反正我这么弱也只适合DIV2了.. T1: 题目大意: ...
- CodeForces Round 192 Div2
This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...
- Codeforce Round #211 Div2
真的是b到不行啊! 尼玛C题一个这么简单的题目没出 aabbccddee 正确的是aabccdee 我的是 aabcdee 硬是TM的不够用,想半天还以为自己的是对的... A:题... B:题. ...
随机推荐
- shell脚本获取网页快照并生成缩略图
获取网页快照并生成缩略图可分两步进行: 1.获取网页快照 2.生成缩略图 获取网页快照 这里我们用 phantomjs 来实现.关于 phantomjs 的详细用法可参考官方网站. 1.安装 我的环境 ...
- 洛谷P1003铺地毯(提高组)
题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n. 现在将这些地毯按照编号从小到大的顺序平行于 ...
- rownum浅谈(二)
上篇说到rownum和order by及索引列的关系,明白了通过构建一个子查询把查询结果固定住再取数就可以了 .还是取最近10条创建的用户: select * from (select u.* fro ...
- 【bzoj3675】[Apio2014]序列分割 斜率优化dp
原文地址:http://www.cnblogs.com/GXZlegend/p/6835179.html 题目描述 小H最近迷上了一个分隔序列的游戏.在这个游戏里,小H需要将一个长度为n的非负整数序列 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- [洛谷P4074][WC2013]糖果公园
题目大意:给一棵$n$个节点的树,每个点有一个值$C_i$,每次询问一条路径$x->y$,求$\sum\limits_{c}val_c\times \sum\limits_{i=1}^{cnt_ ...
- BZOJ5301 [Cqoi2018]异或序列 【莫队】
题目链接 BZOJ5301 题解 莫队水题 BZOJ400AC纪念 #include<algorithm> #include<iostream> #include<cst ...
- c#字典序
using System; using System.Collections.Generic; public class Example { public static void Main() { / ...
- Extra Judicial Operation
Description The Suitably Protected Programming Contest (SPPC) is a multi-site contest in which conte ...
- Nodejs express框架 浅析
http://www.expressjs.com.cn/ 1. 中间件 ①挂载中间件的函数:app.use var http = require('http'); var express = requ ...