对叠放着的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的更多相关文章

  1. codeforces 830 B Cards Sorting

    B. Cards Sorting  http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...

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

  3. Codeforces 830B - Cards Sorting 树状数组

    B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

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

  6. 每日一九度之 题目1041:Simple Sorting

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4883 解决:1860 题目描述: You are given an unsorted array of integer numbers. ...

  7. Codeforces Round #424 E. Cards Sorting

    题目大意:给你一堆n张牌(数字可以相同),你只能从上面取牌,如果是当前牌堆里面最小的值则拿走, 否则放到底部,问你一共要操作多少次. 思路:讲不清楚,具体看代码.. #include<bits/ ...

  8. 九度OJ 1041:Simple Sorting(简单排序) (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4502 解决:1680 题目描述: You are given an unsorted array of integer numbers. ...

  9. 九度oj 题目1041:Simple Sorting

    题目描述: You are given an unsorted array of integer numbers. Your task is to sort this array and kill p ...

随机推荐

  1. 【转】哪个更快:Java堆还是本地内存

    译文出处: shenzhang   原文出处:原文链接 使用Java的一个好处就是你可以不用亲自来管理内存的分配和释放.当你用new关键字来实例化一个对象时,它所需的内存会自动的在Java堆中分配.堆 ...

  2. T4308 数据结构判断

    https://www.luogu.org/record/show?rid=2143639 题目描述 在世界的东边,有三瓶雪碧. ——laekov 黎大爷为了虐 zhx,给 zhx 出了这样一道题.黎 ...

  3. jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码

    //获取当前时间 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToS ...

  4. IOS 根据身份证号码获取 年龄 生日 性别

    /** 从身份证上获取年龄 18位身份证 */ -(NSString *)getIdentityCardAge:(NSString *)numberStr { NSDateFormatter *for ...

  5. RxJava尝试取代Handler初探

    在之前的一篇文章中,我们探究了RxJava的使用方法,详细请看https://www.cnblogs.com/yanyojun/p/9745675.html 根据扔物线大神的描述,如果用一个词来概括R ...

  6. LR 两种录制:html与url

    一直在使用LR,对于Html_based script和Url-based script 两种录制方式之间,要如何选择,仍是一知半解.最近测试时遇到同样的业务功能,两种录制方式的脚本,单次执行时间差别 ...

  7. 接口自动化- 基于 Python

    准备工作 这部分其实在谷歌或者百度上搜索下就可以完成的,可是我就是想再啰嗦一遍,说不定有比我更懒的同学呢哈哈~ 第一步 Python的安装配置 打开官网: https://www.python.org ...

  8. UVA 11971 Polygon 多边形(连续概率)

    题意: 一根长度为n的木条,随机选k个位置将其切成k+1段,问这k+1段能组成k+1条边的多边形的概率? 思路: 数学题.要求的是概率,明显与n无关. 将木条围成一个圆后再开切k+1刀,得到k+1段. ...

  9. SAP成都研究院姚瑶:软件质量保证工作的变迁

    大家好,我是来自SAP成都研究院Revenue Cloud 团队的质量工程师 , yoyo.很高兴可以和大家分享我个人的工作体会.每个团队都有QE(Quality Engineer), 相信大家对QE ...

  10. C ++ _基础之共用体

    由以下代码来进一步学习共用体 #include <stdio.h> #include<iostream> void main() { union un { int a; cha ...