CF830B:Cards Sorting
对叠放着的n张牌,第i张牌写有数字Ai,进行操作:将牌堆顶的牌取出,若是当前牌堆最小值就扔掉,否则放到牌堆底,求牌堆空时操作次数。
怎么看怎么像约瑟夫。。不过约瑟夫DP我不太熟,于是就yy了一下
“当前最小值”??优先队列。把Ai和i绑起来扔到优先队列里,就可以知道下一步要跳到哪里。
有个问题:如果有多个Ai怎么办???把这些相同的数字列出来,下标升序排列,假如上一次抽完牌的位置是now,那么它在把所有这些相同的数字取完后,会走到“离now最近的第一个比now小的下标”那里
因此优先队列中以数字大小为第一关键字而位置为第二,每次取出相同数字的所有位置,利用单调性边取边直接判断我们要找的“离now最近的第一个比now小的下标”(然而代码中我傻了,拿下标出来二分查找)
这样统计答案还有个小问题:有些牌已经被抽掉了,所以用下标统计答案是不行滴!那就加个树状数组吧,复杂度O(nlog2n)
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<queue>
//#include<iostream>
using namespace std; struct heapnode
{
int v,id;
bool operator < (const heapnode &b) const
{return v<b.v || (v==b.v && id<b.id);}
bool operator > (const heapnode &b) const {return b<*this;}
};
priority_queue<heapnode,vector<heapnode>,greater<heapnode> > q;
int n;
int x;
#define maxn 100011
int list[maxn],len;
#define LL long long
int find(int x)
{
if (x<=list[]) return ;
int L=,R=len;
while (L<R)
{
int mid=(L+R+)>>;
if (list[mid]>=x) R=mid-;
else L=mid;
}
return L;
}
struct BIT
{
int a[maxn];
int lowbit(int x) {return x&-x;}
int query(int x)
{
x++;
int ans=;
for (;x;x-=lowbit(x)) ans+=a[x];
return ans;
}
void add(int x,int v)
{
x++;
for (;x<=n;x+=lowbit(x)) a[x]+=v;
}
}t;
int main()
{
scanf("%d",&n);
for (int i=;i<n;i++)
{
scanf("%d",&x);
q.push((heapnode){x,i});
t.add(i,);
}
int now=;LL ans=;
while (!q.empty())
{
int p=q.top().v;
list[len=]=q.top().id;
q.pop();
while (!q.empty() && q.top().v==p)
{
list[++len]=q.top().id;
q.pop();
}
int tmp=find(now);
if (!tmp)
{
ans=ans+t.query(list[len])-t.query(now);
now=list[len];
}
else
{
ans=ans+t.query(list[tmp])+t.query(n-)-t.query(now);
now=list[tmp];
}
for (int i=;i<=len;i++) t.add(list[i],-);
}
printf("%I64d\n",ans);
return ;
}
CF830B: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 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 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 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 ...
- 每日一九度之 题目1041:Simple Sorting
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4883 解决:1860 题目描述: You are given an unsorted array of integer numbers. ...
- Codeforces Round #424 E. Cards Sorting
题目大意:给你一堆n张牌(数字可以相同),你只能从上面取牌,如果是当前牌堆里面最小的值则拿走, 否则放到底部,问你一共要操作多少次. 思路:讲不清楚,具体看代码.. #include<bits/ ...
- 九度OJ 1041:Simple Sorting(简单排序) (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4502 解决:1680 题目描述: You are given an unsorted array of integer numbers. ...
- 九度oj 题目1041:Simple Sorting
题目描述: You are given an unsorted array of integer numbers. Your task is to sort this array and kill p ...
随机推荐
- AJPFX对选择和冒泡两种排序的理解
冒泡排序和直接选择排序都是排序中比较简单和容易实现的算法,先简单说说两者的区别:先以按照元素从小到大为:冒泡排序:将相邻元素两两比较,如果有比较大的,就把比较大的放在右边,这样的结果就是一轮排序完毕后 ...
- Spring-bean(一)
配置形式:基于xml文件的方式:基于注解的方式 Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 依赖注入的方式:属性注入,构造器注入 ...
- mysql索引原理及创建与查询
索引介绍 一:为什么要有索引 索引是用来优化查询效率(速度)的 没有索引的话,对于大数据的表,就只能每次都遍历一遍,数据量越大,耗时越多有索引的话,可以提升好几个数量级的速度 一般的应用系统,读写比例 ...
- logging模块基础
很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...
- BZOJ3083: 遥远的国度(树链剖分)
题意 $n$个节点的树,每个点有权值,支持三种操作 1. 换根 2.把$x$到$y$路径上节点权值变为$z$ 3.询问路径最小值 Sol 啥?你说这是TopTree的裸题?那你写去啊 很显然,如果没有 ...
- NYOJ 题目42 一笔画问题
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...
- VC++绘制金刚石(MFC)
void CTxx1View::OnDraw(CDC* pDC){ CTxx1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add d ...
- DROP DATABASE - 删除一个数据库
SYNOPSIS DROP DATABASE name DESCRIPTION 描述 DROP DATABASE 删除一个现存数据库的目录入口并且删除包含数据的目录. 只有数据库所有者能够执行这条命令 ...
- PHP安全之 register_globals
一.register_globals = Off 和 register_globals = On的区别 register_globals是php.ini里的一个配置,这个配置影响到php如何接收传递过 ...
- 优先队列 || POJ 1442 Black Box
给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...