对叠放着的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. springdata-jpa 八种查询方法

    使用:maven+Spring+jpa+Junit4 查询方式:SQL,JPQL查询,Specification多条件复杂查询 返回类型:list<POJO>,list<Stinrg ...

  2. TigerGraph REST++API

    简介 - 什么是REST ++? TigerGraph TM 系统使用着名的REpresentational State Transfer(REST)架构来管理与TigerGraph核心组件,图形处理 ...

  3. IOS的水滴文件效果

    @implementation ViewController - (void)viewDidLoad{ [super viewDidLoad]; NSDictionary *dict = [NSDic ...

  4. IOS颜色块设置

    + (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIG ...

  5. Hystrix 断路器

    断路器: 当客户端访问服务端,发现服务端有异常不能进行访问时,就会执行一个fallback 方法.

  6. Boxes And Balls(三叉哈夫曼编码)

    题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...

  7. 骑芯供应链(W 笔试)

    单选题 1.调用本身构造方法 答案:this(X) 2.若y=3,a=2,b=4,运行y+=a++/--b,求y的值 答案: 3.不能控制servlet生命周期的方法是? 选项:init().serv ...

  8. 命令终端执行python

    windows进入cmd 1.进入cmd窗口,找到存放py文件的地址(如E:\learn_mock) 2.退出python,输入exit() linux下一样

  9. 浏览器报错 SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 2 of the JSON data

    ajax调用是200,结果返回的不是json字符串(字符串格式不是json应该有的格式),进入了ajax的error回调函数,改为返回json字符串,问题解决了.

  10. springboot @test 使用

    @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class Springtest { ...