题目链接

戳这

题目大意

\(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些数据,输出的确切排序。

题解

令输入数组为b[i];

可以发现最后一个奶牛可以确定为b[n]+1,由最后一个又可以推出倒数第二个,由此递推就好了,每次的答案就是序列中的第b[i]+1小的数,询问完后就将这个数从序列中删除。

至于怎么选那个数,就看你用什么数据结构维护了,我用的平衡树。

code

#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
f=(c=='-')?-1:1,c=getchar();
while(c>='0'&&c<='9')
x=x*10+c-'0',c=getchar();
return x*f;
}
int cnt,root;
struct node{
int fa,ch[2],v,size,son;
}a[500001];
void pushup(int k){
a[k].son=a[a[k].ch[0]].son+a[a[k].ch[1]].son+a[k].size;
}
void turn(int x){
int y=a[x].fa,z=a[y].fa;
int k=x==a[y].ch[1],k2=y==a[z].ch[1];
a[z].ch[k2]=x,a[x].fa=z;
a[y].ch[k]=a[x].ch[k^1],a[a[x].ch[k^1]].fa=y;
a[x].ch[k^1]=y,a[y].fa=x;
pushup(y),pushup(x);
}
void splay(int x,int u){
while(a[x].fa!=u){
int y=a[x].fa,z=a[y].fa;
if(z!=u)
(a[y].ch[0]==x)^(a[z].ch[0]==y)?turn(x):turn(y);
turn(x);
}
if(u==0)
root=x;
}
void add(int x){
int u=root,f=0;
while(u&&a[u].v!=x)
f=u,u=a[u].ch[x>a[u].v];
if(u)
a[u].size++;
else{
++cnt;
u=cnt;
if(f)
(x>a[f].v)?a[f].ch[1]=u:a[f].ch[0]=u;
a[cnt].fa=f;
a[cnt].son=1;
a[cnt].size=1;
a[cnt].v=x;
}
splay(u,0);
}
void find(int x){
int u=root;
if(!u)
return ;
while(x!=a[u].v&&a[u].ch[x>a[u].v])
u=a[u].ch[x>a[u].v];
splay(u,0);
}
int hou(int x){
find(x);
int u=root;
if(a[u].v>x)
return u;
u=a[u].ch[1];
while(a[u].ch[0])
u=a[u].ch[0];
return u;
}
int qian(int x){
find(x);
int u=root;
if(a[u].v<x)
return u;
u=a[u].ch[0];
while(a[u].ch[1])
u=a[u].ch[1];
return u;
}
void delet(int x){
int l=qian(x),r=hou(x);
splay(l,0),splay(r,l);
int u=a[r].ch[0];
if(a[u].size>1)
a[u].size--,splay(u,0);
else
a[r].ch[0]=0;
}
int k_th(int x){
int u=root;
if(a[u].son<x)
return 0;
while(1){
int s=a[u].ch[0];
if(x>a[s].son+a[u].size)
x-=a[s].son+a[u].size,u=a[u].ch[1];
else
if(a[s].son>=x)
u=s;
else
return a[u].v;
}
}
int hh[100001],b[10001];
int main(){
add(-21474847);
add(21474837);
int n=read(),x;
for(int i=1;i<=n;i++)
add(i);
for(int i=2;i<=n;i++)
b[i]=read();
for(int i=n;i>=1;i--)
x=k_th(b[i]+2),hh[i]=x,delet(x);
for(int i=1;i<=n;i++)
printf("%d\n",hh[i]);
return 0;
}

「POJ 2182」 Lost Cows的更多相关文章

  1. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

  2. 【POJ 2182】Lost Cows

    [题目链接] http://poj.org/problem?id=2182 [算法] 树状数组 + 二分 [代码] #include <algorithm> #include <bi ...

  3. 「POJ 3268」Silver Cow Party

    更好的阅读体验 Portal Portal1: POJ Portal2: Luogu Description One cow from each of N farms \((1 \le N \le 1 ...

  4. 「POJ Challenge」生日礼物

    Tag 堆,贪心,链表 Solution 把连续的符号相同的数缩成一个数,去掉两端的非正数,得到一个正负交替的序列,把该序列中所有数的绝对值扔进堆中,用所有正数的和减去一个最小值,这个最小值的求法与「 ...

  5. 「POJ 1135」Domino Effect(dfs)

    BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...

  6. 「POJ - 1003」Hangover

    BUPT 2017 summer training (16) #2C 题意 n个卡片可以支撑住的长度是1/2+1/3+1/4+..+1/(n+1)个卡片长度.现在给出需要达到总长度,求最小的n. 题解 ...

  7. 「POJ - 2318」TOYS (叉乘)

    BUPT 2017 summer training (16) #2 A 题意 有一个玩具盒,被n个隔板分开成左到u右n+1个区域,然后给每个玩具的坐标,求每个区域有几个玩具. 题解 依次用叉积判断玩具 ...

  8. 「POJ 2699」The Maximum Number of Strong Kings

    题目链接 戳我 \(Describe\) 一场联赛可以表示成一个完全图,点表示参赛选手,任意两点u, v之间有且仅有一条有向边\((u, v)\)或\((v, u)\),表示\(u\)打败\(v\)或 ...

  9. 「POJ 1741」Tree

    题面: Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define ...

随机推荐

  1. selenium自动化浏览器后台运行headless模式

    通过selenium做WEB自动化的时候,必须要启动浏览器, 浏览器的启动与关闭会影响执行效率. 当我们在自己电脑运行代码时,还会影响做别的事情. 鉴于这种情况,Google针对Chrome浏览器新增 ...

  2. andriod/ios webview与js交互 html_demo

    <html> <head> <title>测试</title> </head> <body> <h3>Android ...

  3. 用css实现图片在div内的垂直居中

    已知一个div内有一个img,两者的高度均不知道,但图片的高度一定小于div的高度 代码如下: .box { /*非IE的主流浏览器识别的垂直居中的方法*/ display: table-cell;  ...

  4. Python Twisted系列教程14:Deferred用于同步环境

    作者:dave@http://krondo.com/when-a-deferred-isnt/  译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列. 介绍 这部分我们要介绍Deferred的 ...

  5. linux安装xgboost

    在学校服务器上安装xgboost,事先我已经安装了anaconda,但是因为师兄已经装了python所以没加入到path. 网上的方法一般都要编译,另外官方的下载方法要联网..总之出了一堆错,最终还是 ...

  6. vc6.0出现“cannot add new member”解决办法

    在编辑一个工程的时候,添加一个按钮,对这个按钮产生消息函数 双击按钮之后出现 cannot add new member 网上说删除.clw文件 我发现并没有这个后缀的文件 以下是我的操作办法:   ...

  7. 相关不同Linux系统的性能监控命令整理

    Linux系统 查看系统版本情况: $uname -a 监控进程的CPU,MEM使用情况: $ps –aux 过滤方式命令:$ ps -aux|awk '{print $3,$4,$11}'|sort ...

  8. error: undefined reference to 'av_register_all()'

    cygwin下ndk编译工程中使用ffmpeg时出现的错误:“error: undefined reference to 'av_register_all()'” 使用ffmpeg的源文件是  *.c ...

  9. Checker Challenge跳棋的挑战(n皇后问题)

    Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...

  10. jquery中选中复选框1.8之前与1.8之后的区别

    在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下: 判断是否选中 $('#checkbox').prop('checked') 设置选中与不选中状态: $('#che ...