Lost Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10695   Accepted: 6865

Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.

Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.

Given this data, tell FJ the exact ordering of the cows.

Input

* Line 1: A single integer, N

* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.

Output

* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1
题意:
第一行给出cow的数目n,接下来2-n行给出每个排在第2-n各位置的cow的在其编号比其小的个数。最后按排队顺序依次给出每个cow的编号。
思路:
从后向前确定编号,设比最后一个cow小的cow数目为a,则最后一个cow的编号为所剩下cow 的第(a+1)大编号。
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
int n;
int deg[MAXN];
int vis[MAXN];
int ans[MAXN];
int seek(int k)
{
int pos=;
for(int i=;i<=n;i++)
{
if(!vis[i])
{
pos++;
if(pos==k) return i;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
deg[]=;
for(int i=;i<n;i++)
{
scanf("%d",&deg[i]);
}
for(int i=n-;i>=;i--)
{
int pos=seek(deg[i]+);
vis[pos]=;
ans[i]=pos;
}
for(int i=;i<n;i++)
{
printf("%d\n",ans[i]);
} } return ;
}

转化为排队问题,利用线段树求解.

#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
struct node{
int l,r;
int n;
}a[MAXN*];
int que[MAXN];
int pos[MAXN];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].n=(r-l+);
if(l==r) return;
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
} void update(int rt,int pos,int i)
{
if(a[rt].l==a[rt].r)
{
a[rt].n--;
que[i]=a[rt].l;
return ;
} if(pos<=a[rt<<].n) update(rt<<,pos,i);
else update((rt<<)|,pos-a[rt<<].n,i);
a[rt].n=a[rt<<].n+a[(rt<<)|].n;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
build(,,n);
pos[]=;
for(int i=;i<n;i++)
{
scanf("%d",&pos[i]);
}
for(int i=n-;i>=;i--)
{
update(,pos[i]+,i);
}
for(int i=;i<n;i++)
{
printf("%d\n",que[i]);
}
} return ;
}

POJ2182(排队问题)的更多相关文章

  1. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  2. Python开发——排队问题随机模拟分析

    案例:主要是基于"蒙特卡罗思想",求解排队等待时间问题 场景:厕所排队问题 1.两场电影结束时间相隔较长,互不影响: 2.每场电影结束之后会有20个人想上厕所: 3.这20个人会在 ...

  3. poj2182(线段树求序列第k小)

    题目链接:https://vjudge.net/problem/POJ-2182 题意:有n头牛,从1..n编号,乱序排成一列,给出第2..n个牛其前面有多少比它编号小的个数,记为a[i],求该序列的 ...

  4. 【POJ2182】Lost Cows

    [POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...

  5. 【poj2182】【poj2828】树状数组/线段树经典模型:逆序查找-空位插入法

    poj2182题意:有一个1~n的排列,现在给定每个人前面有多少个人的编号比他大,求这个排列是什么.n<=8000 poj2182题解: 逆序做,可以确定二分最后一个是什么,然后删除这个数.树状 ...

  6. 银行排队问题之单队列多窗口加VIP服务(30 分)

    银行排队问题之单队列多窗口加VIP服务(30 分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选 ...

  7. PTA 银行排队问题之单队列多窗口服务(25 分)

    银行排队问题之单队列多窗口服务(25 分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假 ...

  8. codevs——2956 排队问题

    2956 排队问题  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3 ...

  9. Codevs 2956 排队问题

    2956 排队问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3人组成一组,求有多少种不 ...

随机推荐

  1. MongoDB的选举过程(转)

    转自:http://www.mongoing.com/archives/295 MongoDB的复制集具有自动容忍部分节点宕机的功能,在复制集出现问题时时,会触发选举相关的过程,完成主从节点自动切换. ...

  2. 利用expload 分割字符串 变成数组

    #admin_abc去掉下划线 =>>变成 array('admin','abc') $table_Name=explode('_', $table_Name);

  3. Android 繪圖白板元件,有畫筆和板擦的功能 (转)

    package com.example.drawboard; import java.util.ArrayList; import java.util.List; import android.con ...

  4. antd引入普通html使用,将ant Design本地化

    一直想着能本地化antd的,不用npm以及dva那么复杂的配置环境来开发,并且本地化以后对以后链接flask的模板渲染机制也能很好的结合.下面是具体的实现方法: 1.将react的相关链接引入: &l ...

  5. 【Android开发-5】界面装修,五大布局你选谁

    前言:假设要开一家店,门店装修是非常重要的事情.有钱都请专门的建筑设计公司来设计装修,没钱的仅仅能自己瞎折腾.好不好看全凭自己的感觉.像Android开发.在移动端大家看到的界面视觉不咋滴,一般连打开 ...

  6. 几句话搞懂URI、URL、URN之间的关系

    1.URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. 2.RL是uniform resource locator,统一资源定位器,它是一种具体 ...

  7. iOS 打包Framework包含其他Framework的问题

    当你打包一个framework静态库包含另一个第三方静态库时,在工程中使用自己打包的framework,这时编译会出现报错,报错的大概原因是没有找到你打包framework里面的第三方framewor ...

  8. An Overview of Query Optimization in Relational Systems

    An Overview of Query Optimization in Relational Systems

  9. 常见寄存器以及常见汇编指令,常见爆破指令 good

    CPU的任务就是执行存放在存储器里的指令序列.为此,除要完成算术逻辑操作外,还需要担负CPU和存储器以及I/O之间的数据传送任务.早期的CPU芯片只包括运算器和控制器两大部分.到了近几年,为了使存储器 ...

  10. IOS 状态栏 显示与隐藏网络活动状态

    IOS中显示和隐藏状态栏的网络活动标志 //在向服务端发送请求状态栏显示网络活动标志: [[UIApplication sharedApplication] setNetworkActivityInd ...