我只能说真的看不懂题解的做法
我的做法就是线段树维护,毕竟每个数的顺序不变嘛
那么单点维护 区间剩余卡片和最小值

每次知道最小值之后,怎么知道需要修改的位置呢
直接从每种数维护的set找到现在需要修改的数的在初始卡片的位置

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
#define lson l,m, rt<<1
#define rson m+1, r, rt<<1|1
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
int MOD;
int A[N];
int Min[N << 2];
int Sum[N << 2];
void Build(int l, int r, int rt) {
if(l == r) {
Min[rt] = A[l]; Sum[rt] = 1;
return;
}
int m = (l + r) >> 1;
Build(lson); Build(rson);
Sum[rt] = Sum[rt << 1] + Sum[rt << 1|1];
Min[rt] = min(Min[rt <<1] , Min[rt << 1|1]);
}
void Update(int pos, int l, int r, int rt) {
if(l == r) {
Min[rt] = INF; Sum[rt] = 0;
return;
}
int m = (l + r) >> 1;
if(pos <= m) Update(pos, lson);
else Update(pos, rson);
Sum[rt] = Sum[rt<<1] + Sum[rt<<1|1];
Min[rt] = min(Min[rt<<1], Min[rt<<1|1]);
}
int Total(int pos, int l, int r, int rt) {
if(pos == 0) return 0;
if(pos == r) {
return Sum[rt];
}
int m = (l + r) >> 1;
if(pos <= m) return Total(pos, lson);
else return Sum[rt<<1] + Total(pos, rson);
} map<int, set<int> > mp;
set<int> ::iterator it; int main() {
int n;
while(~scanf("%d", &n)) {
ll ans = 0;
mp.clear();
for(int i = 1; i <= n; ++i) {
scanf("%d",&A[i]);
mp[A[i]].insert(i);
}
Build(1, n, 1); int pos = 1;
for(int i = 1; i <= n; ++i) {
int tar = Min[1]; int nwpos;
it = mp[tar].lower_bound(pos); if(it == mp[tar].end()) {
it = mp[tar].begin();
}
nwpos = *it;
mp[tar].erase(it); // printf("%d %d\n", tar, nwpos); Update(nwpos, 1, n, 1);
int t1 = Total(nwpos, 1,n,1); int t2 = Total(pos - 1, 1,n,1);
// printf("%d %d\n", t1, t2);
if(pos <= nwpos) {
ans += t1 - t2 + 1;
}else {
ans += Sum[1] - t2 + t1 + 1;
}
pos = nwpos;
} printf("%d\n", ans);
}
return 0;
}

Codeforces Round #424 Div2 E. Cards Sorting的更多相关文章

  1. codeforces round #424 div2

    A 暴力查询,分三段查就可以了 #include<bits/stdc++.h> using namespace std; ; int n, pos; int a[N]; int main( ...

  2. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  3. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  4. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  5. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

    http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...

  9. Codeforces Round #361 div2

    ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...

随机推荐

  1. SpringMVC源码情操陶冶-HandlerAdapter适配器简析

    springmvc中对业务的具体处理是通过HandlerAdapter适配器操作的 HandlerAdapter接口方法 列表如下 /** * Given a handler instance, re ...

  2. BZOJ 2743: [HEOI2012]采花 [树状数组 | 主席树]

    题意: 查询区间中出现次数$>2$的颜色个数 一眼主席树,区间中$l \le last[i] \le r$的个数减去$l \le last[last[i]] \le r$的个数,搞两颗主席树来做 ...

  3. 51NOD 1376 最长递增子序列的数量 [CDQ分治]

    1376 最长递增子序列的数量 首先可以用线段树优化$DP$做,转移时取$0...a[i]$的最大$f$值 但我要练习$CDQ$ $LIS$是二维偏序问题,偏序关系是$i<j,\ a_i< ...

  4. Java线程池ThreadPoolExector的源码分析

    前言:线程是我们在学习java过程中非常重要的也是绕不开的一个知识点,它的重要程度可以说是java的核心之一,线程具有不可轻视的作用,对于我们提高程序的运行效率.压榨CPU处理能力.多条线路同时运行等 ...

  5. MySQL创建用户与授权

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  6. Docker命令行安装Shipyard

    1.下载自动部署Shell脚本 curl -sSL https://shipyard-project.com/deploy | bash -s 自动部署脚本中, 包括以下参数: ACTION: 表示可 ...

  7. dedecms data文件夹外迁

    出于网站安全考虑,我们一般要把data文件夹迁移到网站根目录外面. dedecms data文件夹外迁方法: 1. 修改首页文件中配置文件路径 打开/index.php,把代码 if(!file_ex ...

  8. 为何要部署IPV6

    ·IPv4的局限性:   1.地址空间的局限性:IP地址空间的危机由来已久,并正是升级到IPv6的主要动力.   2.安全性:IPv4在网络层没有安全性可言,安全性一直被认为是由网络层以上的层负责. ...

  9. TensorFlow实战之实现AlexNet经典卷积神经网络

    本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.AlexNet模型及其基本原理阐述 1.关于AlexNet ...

  10. Asp.Net Core 基于QuartzNet任务管理系统

    之前一直想搞个后台任务管理系统,零零散散的搞到现在,也算完成了. 这里发布出来,请园里的dalao批评指导! 废话不多说,进入正题. github地址:https://github.com/YANGK ...