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. 【Java】事件驱动模型和观察者模式

    你有一件事情,做这件事情的过程包含了许多职责单一的子过程.这样的情况及其常见.当这些子过程有如下特点时,我们应该考虑设计一种合适的框架,让框架来完成一些业务无关的事情,从而使得各个子过程的开发可以专注 ...

  2. gitlab多人协同工作

    gitlab多人协同工作 本文为亨利向<Git权威指南>的作者蒋鑫老师的答疑邮件写成. 这里特别感谢蒋鑫老师对我询问gitlab的协同工作流程问题的详细解答. 蒋鑫老师的细致专业的解答让我 ...

  3. [转]screen 的使用

    当我们使用securecrt,putty, 等连接远程服务器时,如果正在执行一个程序,比如shell 脚本,退出当前的窗口会导致程序终止!其原理如下: 根据POSIX.1定义: 1 挂断信号(SIGH ...

  4. Mac Security工具使用总结find-identity

    Security是Mac系统中钥匙串和安全模块的命令行管理工具,(图形化工具为Keychain Access.app).钥匙串(Keychain)实质上就是一个用于存放证书.密钥.密码等安全认证实体的 ...

  5. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  6. Spark源代码分析之六:Task调度(二)

    话说在<Spark源代码分析之五:Task调度(一)>一文中,我们对Task调度分析到了DriverEndpoint的makeOffers()方法.这种方法针对接收到的ReviveOffe ...

  7. Unity编辑器扩展之RequireComponent等详解

    RequireComponent的使用: 当你添加的一个用了RequireComponent组件的脚本,需要的组件将会自动被添加到game object(游戏物体).这个可以有效的避免组装错误.举个例 ...

  8. uva--10714+找规律

    题意: 一根长度为len的木棍上有n仅仅蚂蚁.蚂蚁们都以1cm/s的速度爬行;假设一仅仅蚂蚁爬到了木棍的端点,那么他就会掉下去;假设两仅仅蚂蚁碰到一起了,他们就会掉头往相反方向爬行.输入len和n仅仅 ...

  9. android菜鸟学习笔记3----关于AndroidMainfest.xml

    每个android项目都包含一个AndroidMainfest.xml文件,它包含了组成应用程序的每一个Acitivity.Service.Content Provider和Broadcast Rec ...

  10. Nodejs 中常见的加密算法:RSA(1)

    Linux用户(以Ubuntu为例) $ openssl 进入OpenSSL程序 OpenSSL> genrsa -out rsa_private_key.pem 1024 生成私钥 OpenS ...