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 ...
随机推荐
- Javaweb学习笔记3—Serverlet
今天来讲javaweb的第三个阶段学习. 老规矩,首先先用一张思维导图来展现今天的博客内容. ps:我的思维是用的xMind画的,如果你对我的思维导图感兴趣并且想看到你们跟详细的备注信息,请点击下载 ...
- 多线程中 CountDownLatch CyclicBarrier Semaphore的使用
CountDownLatch 调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行.也可以传入时间,表示时间到之后,count还没有为0的时候,就会继续执行. package ...
- python struct.pack方法报错argument for 's' must be a bytes object 解决
参考 https://blog.csdn.net/weixin_38383877/article/details/81100192 在python3下使用struct模块代码 fileHead = s ...
- C++ isalpha、isalnum、islower、isupper用法
1. isalpha isalpha()用来判断一个字符是否为字母,如果是字符则返回非零,否则返回零. cout<<isalpha('a'); //返回非零 cout<<isa ...
- linux下的基础操作
Xfce 终端: Linux 命令行终端,打开后会进入 zsh Shell 环境,可以使用 Linux 命令. NetSurf 网络浏览器:浏览器,可以用在需要前端界面的课程里,只需要打开环境里写的 ...
- Android项目源码分享
http://blog.csdn.net/gao_chun/article/details/47263063 Android项目源码分享 给大家分享几个Android开发项目源码,大部分功能相信可以在 ...
- Spring框架针对dao层的jdbcTemplate操作之jdbc数据库连接原始操作方法 所需安装包下载
crud指数据库或者持久层的基本操作,包括 增加(Create).读取查询(Retrieve 取回).更新(Update)和删除(Delete) Spring不仅对JDBC进行了封装,也对Hibern ...
- MySQL数据库常见面试题
什么是存储过程?有哪些优缺点? 存储过程简单来说就是为了以后使用而保存的一条或多条预编译SQL语句,这些语句块像一个方法一样执行一些功能. 优点: 类似于封装,简化操作: 不用反复建立一系列处理步骤, ...
- PHP+Apache2.x+phpMyAdmin安装和配置
1>各个部件的下载 目前在windows下开发 PHP官网下载地址:https://windows.php.net/download PHP有TS(ThreadSafe)和NTS两个版本,所以按 ...
- Django中对接第三方支付(支付宝)实现支付的流程
1. 业务逻辑准备 1. 使用沙箱提供的商家环境 沙箱环境:是支付宝提供给开发者的模拟支付的环境 沙箱应用:https://docs.open.alipay.com/200/105311 沙箱账号:h ...