题目链接

戳这

题目大意

\(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. CGI/MIME/servlet术语解释

    CGI→一种协议, 一种标准, 一种规范 使用CGI协议, 能够让用户访问某些动态资源的时候, 触发web服务器, 让web服务器根据CGI协议能够调用外部(web服务器外部)的程序来执行处理这个动态 ...

  2. java成神之——jaxb操作xml的基本使用

    JAXB 依赖 读取xml配置 写配置 自定义写配置 结语 JAXB 依赖 <dependency> <groupId>javax.activation</groupId ...

  3. java 删除文件夹 / 删除某文件夹下的所有文件

    import java.io.File; /*************************删除文件夹delFolder / 删除文件夹中的所有文件delAllFile *start******** ...

  4. leetcode12

    public class Solution { public string IntToRoman(int num) { string[] M = { "", "M&quo ...

  5. delphi 在桌面屏幕上模拟鼠标单击

    delphi 在桌面屏幕上模拟鼠标单击 procedure TFrmUnicom.Button1Click(Sender: TObject); var oldPoint, newPoint: TPoi ...

  6. PyInstaller打包成exe可执行文件

    PyInstaller 安装pyinstaller 对于那些网络比较稳定,能够流畅使用pip源地址的用户,直接下面的命令就可以搞定: pip install pyinstaller 通常我们会下载源码 ...

  7. Android适配器Adapter的学习

    Android中有很多的适配器,首先看看这些适配器的继承结构 这些适配器中,BaseAdapter用的最多,也用的最熟,先放过他,从ArrayAdapter开始 一个listAdapter用来管理一个 ...

  8. python利用utf-8编码判断中文字符

    下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符. 全角符号转半角符号. unicode字符串归一化等工作. 还有一个能处理多音字的汉字转拼音的程序,还在整理中. #!/u ...

  9. SpringMVC总结二:Controller的请求映射方式(RequestMapping)简单介绍

    在SpringMVC总结一:快速入门的基础上简单介绍一下请求映射的方式: 1,标准映射规则 1. @RequestMapping可以设置在类上,也可以设置在方法上 2. 请求的映射规则是:类上的Req ...

  10. 3-1 Git下载与安装

    https://desktop.github.com/