点我看题

初步想法是模拟,找到下一个位置并记录操作数,O(n^2)肯定会超时。

那么进行优化,会发现到下一位置的操作数就是两个位置之间存在的数的个数,于是就变成了计数问题。

不难想到用树状数组或线段树进行计数,时间复杂度O(nlogn)。

 #include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
const int Nmax=;
int n,now;
long long ans;
int num[Nmax],pos[Nmax];
int c[Nmax]; int lowbit(int x)
{
return x&(-x);
} int get_sum(int x)
{
int ans=;
while(x)
ans+=c[x],x-=lowbit(x);
return ans;
} int query(int l,int r)
{
return get_sum(r)-get_sum(l-);
} void update(int p,int add)
{
while(p<=n)
c[p]+=add,p+=lowbit(p);
} void build()
{
for(int i=;i<=n;i++)
c[i]=;
for(int i=;i<=n;i++)
update(i,);
} int main()
{
freopen("in.in","r",stdin);
while(scanf("%d",&n) && n!=)
{
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
pos[num[i]]=i;
}
build();
now=,ans=;
for(int i=;i<=n;i++)
{
if(pos[i]>now)
ans+=min(query(now,pos[i]),query(,now)+query(pos[i],n)); else if(pos[i]<now)
ans+=min(query(pos[i],now),query(now,n)+query(,pos[i]));
else
ans++; now=pos[i],update(pos[i],-),num[pos[i]]=;
if(i==n)
break;
while(!num[now])
now=now%n+;
}
printf("%lld\n",ans); }
return ;
}

HDU 4262 Juggler的更多相关文章

  1. HDU 4262 Juggler 树状数组

    将每个球按输入顺序编号,建立 它第几个被扔掉->编号 的映射关系. 记录当前在手里的球的编号,按扔掉的顺序查找这个球的编号,看看这个球是逆时针转到手里更近还是顺时针转到手里更近,即当前扔掉球的编 ...

  2. hdu 4262(线段树)

    题目:有一个圈,可以从某个位置取球,给出原有的顺序,有三种操作,左旋一次,右旋一次,取球,要求按顺序取球,问需要操作多少次 显然操作是确定的,每次将目标球旋转过来,找出左旋和右旋操作少的,然后取球. ...

  3. BNUOJ 26228 Juggler

    Juggler Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 42 ...

  4. HDU 4426 Palindromic Substring

    Palindromic Substring Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. Linux 学习 step by step (2)

      Linux 学习 step by step (2) Linux,想要我说爱你真的不容易了,尽管,你是ubutun,尽管,你有蛮界面.但是,操作你,还是没有操作windows那么的如鱼得水了.为了更 ...

  2. hdu 4198 Quick out of the Harbour(BFS+优先队列)

    题目链接:hdu4198 题目大意:求起点S到出口的最短花费,其中#为障碍物,无法通过,‘.’的花费为1 ,@的花费为d+1. 需注意起点S可能就是出口,因为没考虑到这个,导致WA很多次....... ...

  3. 模拟Vue之数据驱动5

    一.前言 在"模拟Vue之数据驱动4"中,我们实现了push.pop等数组变异方法. 但是,在随笔末尾我们提到,当pop.sort这些方法触发后,该怎么办呢?因为其实,它们并没有往 ...

  4. Android libyuv应用系列(二)libyuv的使用

    上篇文章Android libyuv使用系列(一)Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别中我们了解了YUV相关的知识,而本篇文章我会介绍libyuv是什么,以 ...

  5. Android 动画的分类

    分为三类: View Animation (补间动画 Tween动画) Drawable Animation(帧动画 Frame动画) Property Animation(android 3.0引入 ...

  6. Anaconda安装第三方包(whl文件)

    先说下环境 Anaconda 对应Python3.5的版本 win7,64位系统. step1:下载whl文件 step2:打开‘Anaconda Command Prompt‘, 如下图: step ...

  7. 曲演杂坛--SQLCMD下执行命令失败但没有任何错误提示的坑

    今天使用SQLCMD导入到SQL SERVER数据库中,看着数据文件都成功执行,但是意外发现有一个文件数据没有成功导入,但执行不报错,很容易导致问题被忽略. 使用存在问题的文件做下测试,从界面上看几行 ...

  8. mysql数据一致性检查及修复

    percona-toolkit-2.2.20-1.noarchmysql 5.6.29-logmaster:192.168.166.129slave:192.168.166.131 一.创建数据库校验 ...

  9. 如何得到AdoConnection.execute(sqlstr)执行的返回结果

    如何得到AdoConnection.execute(sqlstr)执行的返回结果? 1: TAdoConnection.execute有procedure.function的两种定义,function ...

  10. gridcontrol datatemplate trigger

    <TextBlock Name="textBlock" HorizontalAlignment="Left" Text="{Binding Va ...