\(\mathrm{Sleepy Cow Sorting}\)

问题描述

LG5200

题解

树状数组。

设\(c[i]\)代表\([1,i]\)中归位数。

显然最终的目的是将整个序列排序为一个上升序列,于是倒序枚举,先把最后有序的插入。

剩下来前面无序的就是要操作的,于是直接输出操作次数。

接下来方案很容易构造。

\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std; void read(int &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
} const int maxn=100000+7;
#define lowbit(pos) (pos&(-pos)) int c[maxn],n,a[maxn]; void change(int pos,int k){
while(pos<=n){
c[pos]+=k;pos+=lowbit(pos);
}
} int query(int pos){
int ret=0;
while(pos){
ret+=c[pos];pos-=lowbit(pos);
}
return ret;
} int ans,cnt; int main(){
freopen("sleepy.in","r",stdin);freopen("sleepy.out","w",stdout);
read(n);
for(int i=1;i<=n;i++) read(a[i]);
for(int i=n;i>=1;i--){
ans++;--cnt;change(a[i],1);
if(a[i-1]>a[i]) break;
}
cnt+=n;
printf("%d\n",cnt);
for(int i=1;i<=n-ans;i++){
--cnt;printf("%d ",query(a[i])+cnt);
change(a[i],1);
}
fclose(stdin);fclose(stdout);
return 0;
}

LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组的更多相关文章

  1. hdu 2838 Cow Sorting (树状数组)

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用

    这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...

  4. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  5. LOJ #2719. 「NOI2018」冒泡排序(组合数 + 树状数组)

    题意 给你一个长为 \(n\) 的排列 \(p\) ,问你有多少个等长的排列满足 字典序比 \(p\) 大 : 它进行冒泡排序所需要交换的次数可以取到下界,也就是令第 \(i\) 个数为 \(a_i\ ...

  6. 「模拟赛20180307」三元组 exclaim 枚举+树状数组

    题目描述 给定 \(n,k\) ,求有多少个三元组 \((a,b,c)\) 满足 \(1≤a≤b≤c≤n\)且\(a + b^2 ≡ c^3\ (mod\ k)\). 输入 多组数据,第一行数据组数\ ...

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

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

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

  9. [bzoj2058][Usaco2010 Nov]Cow Photographs_树状数组_动态规划

    Cow Photographs bzoj-2058 Usaco-2010 Nov 题目大意:给定一个n的排列.每次操作可以交换相邻两个数.问将序列变成一个:$i,i+1,i+2,...,n,1,2,. ...

随机推荐

  1. Java 虚拟机编程接口JVMIT

    JVMTI(JVM Tool Interface)是 Java 虚拟机所提供的 native 编程接口,是 JVMPI(Java Virtual Machine Profiler Interface) ...

  2. CF-579 D. "Or" Game

    题意: 给一列数,任选一个数,乘x,最多操作k次,问最后a[1]|a[2]|...|a[n]的最大值是多少; 思路: 或运算是0|0=0,1|0=1,0|1=1,1|1=1,那么每次乘一个大于等于2的 ...

  3. 【笔记】Java微服务之路(持续更新)

    微服务架构的说明: 微服务的架构风格是将一个单体的应用程序开发拆解为一组"小"的服务,这里的"小"是以业务边界 来区分的,而不是根据代码的多少区分.每个服务都运 ...

  4. Leetcode算法题 7. Reverse Integer2

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  5. Vue router-link路由不同的写法,不一样的效果

    我想要的路径:http://localhost:8080/#/main/hato/realtime/eventDetail/238 情况一:未进行路由配置:  {      path: 'eventD ...

  6. Kubernetes Deployment(部署无状态应用)

    Kubernetes Deployment(部署无状态应用) Pod与controllers的关系 • controllers:在集群上管理和运行容器的对象 • 通过label-selector相关联 ...

  7. 启明星MRBS会议室预约系统V30.0发布

    MRBS系统官方网址 https://www.dotnetcms.org/ 在线演示 http://demo.dotnetcms.org/mrbs 用户名admin,密码123456 Meeting ...

  8. Oracle 原生驱动带来的精度问题的分析与解决

    问题 Oracle 官方提供了 dotnet core 驱动,但我们在使用中遇到了精度问题. 复现 以下代码运行数学运算 1/3,无论是 OracleCommand.ExecuteScalar() 还 ...

  9. Ubuntu Err:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not resolve 'us.archive.ubuntu.com' 错误

    Ubuntu 更新 apt-get update 的时候 出现 Err: http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not ...

  10. WPF-如何添加用户控件(同一个程序集与非同一个程序集)

    在WPF中,假如十个按钮与十个文本框需要在窗体中多次使用,每次都都要重新添加这二十个按钮,显然是不可取的.这时,可以把这二十个按钮封装成一个UserControl,然后多次引用. 一.新建一个用户控件 ...