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 ...
随机推荐
- 【学习笔记】深入理解js原型和闭包(18)——补充:上下文环境和作用域的关系
本系列用了大量的篇幅讲解了上下文环境和作用域,有些人反映这两个是一回儿事.本文就用一个小例子来说明一下,作用域和上下文环境绝对不是一回事儿. 再说明之前,咱们先用简单的语言来概括一下这两个的区别. 0 ...
- Android 常见的工具类
/** * Wifi 管理类 * * @author Administrator * 使用方法 * WifiManagerUtils wifiManager = new WifiManagerUtil ...
- 项目适配iOS9遇到的一些问题及解决办法 ,以及URL 白名单配置方法
1.网络请求报错.升级Xcode 7.0发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Secur ...
- 在proe模型文件里面存储用户数据
存储外部数据 author:visualsan 2014.2 上海 1.简介 利用外部数据存储外部接口,可以在模型文件里面尺寸用户自定义数据.在模型保存时数据自动存储,在模型载入时数据自动载入.外部数 ...
- VBA Promming——分支语句(解二元一次方程)
分支语句 If expression1 Then expressions ElseIf expression2 Then expressions Else expression End If 注:VB ...
- ubuntu卡机
卡机了用ctrl+alt+t打开终端然后top看后台程序 最后kill -9 + PID就能把最影响问题的程序杀掉 我之前就杀了一个占100%cpu的程序
- 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
*题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数) public class 第三十九题按条件计算 ...
- 【转】C#中的==、Equal、ReferenceEqual
[转]C#中的==.Equal.ReferenceEqual 转载自: http://www.cnblogs.com/zagelover/articles/2741409.html 1. Refere ...
- 阿里云ECS搭建node/mongodb开发环境及部署
一.前端的er在window或mac上安装开发环境应该再清楚不过了.但在服务器上安装还是有点不同的,毕竟是 centOS,从此不得不走上用命令操作…… 二.前期准备 1.首先,我们去阿里云网站阿里云服 ...
- C++ 给自己的扫盲笔记
1.运算符new分配内存的格式: 指针变量名 = new 类型: 如分配一个20字节的name变量 :name = new char[20]; 2.strlen(s);函数: 返回字符串s的长度 ...