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

  1. 5
  2. 1
  3. 2
  4. 1
  5. 0

Sample Output

  1. 2
  2. 4
  3. 5
  4. 3
  5. 1
    题意:
    第一行给出cow的数目n,接下来2-n行给出每个排在第2-n各位置的cow的在其编号比其小的个数。最后按排队顺序依次给出每个cow的编号。
    思路:
    从后向前确定编号,设比最后一个cow小的cow数目为a,则最后一个cow的编号为所剩下cow 的第(a+1)大编号。
  1. #include"cstdio"
  2. #include"cstring"
  3. #include"algorithm"
  4. using namespace std;
  5. const int MAXN=;
  6. int n;
  7. int deg[MAXN];
  8. int vis[MAXN];
  9. int ans[MAXN];
  10. int seek(int k)
  11. {
  12. int pos=;
  13. for(int i=;i<=n;i++)
  14. {
  15. if(!vis[i])
  16. {
  17. pos++;
  18. if(pos==k) return i;
  19. }
  20. }
  21. }
  22. int main()
  23. {
  24. while(scanf("%d",&n)!=EOF)
  25. {
  26. memset(vis,,sizeof(vis));
  27. deg[]=;
  28. for(int i=;i<n;i++)
  29. {
  30. scanf("%d",&deg[i]);
  31. }
  32. for(int i=n-;i>=;i--)
  33. {
  34. int pos=seek(deg[i]+);
  35. vis[pos]=;
  36. ans[i]=pos;
  37. }
  38. for(int i=;i<n;i++)
  39. {
  40. printf("%d\n",ans[i]);
  41. }
  42.  
  43. }
  44.  
  45. return ;
  46. }

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

  1. #include"cstdio"
  2. #include"cstring"
  3. #include"algorithm"
  4. using namespace std;
  5. const int MAXN=;
  6. struct node{
  7. int l,r;
  8. int n;
  9. }a[MAXN*];
  10. int que[MAXN];
  11. int pos[MAXN];
  12. void build(int rt,int l,int r)
  13. {
  14. a[rt].l=l;
  15. a[rt].r=r;
  16. a[rt].n=(r-l+);
  17. if(l==r) return;
  18. int mid=(l+r)>>;
  19. build(rt<<,l,mid);
  20. build((rt<<)|,mid+,r);
  21. }
  22.  
  23. void update(int rt,int pos,int i)
  24. {
  25. if(a[rt].l==a[rt].r)
  26. {
  27. a[rt].n--;
  28. que[i]=a[rt].l;
  29. return ;
  30. }
  31.  
  32. if(pos<=a[rt<<].n) update(rt<<,pos,i);
  33. else update((rt<<)|,pos-a[rt<<].n,i);
  34. a[rt].n=a[rt<<].n+a[(rt<<)|].n;
  35. }
  36.  
  37. int main()
  38. {
  39. int n;
  40. while(scanf("%d",&n)!=EOF)
  41. {
  42. build(,,n);
  43. pos[]=;
  44. for(int i=;i<n;i++)
  45. {
  46. scanf("%d",&pos[i]);
  47. }
  48. for(int i=n-;i>=;i--)
  49. {
  50. update(,pos[i]+,i);
  51. }
  52. for(int i=;i<n;i++)
  53. {
  54. printf("%d\n",que[i]);
  55. }
  56. }
  57.  
  58. return ;
  59. }

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. python学习(五)列表

    #!/usr/bin/python # 列表的学习, 列表的概念不陌生, 就是熟悉一下python中的列表是如何操作的 # 1. 序列的操作 L = [ 123, 'spam', 1.23] # 里面 ...

  2. 深入Asyncio(十一)优雅地开始与结束

    Startup and Shutdown Graceful 大部分基于asyncio的程序都是需要长期运行.基于网络的应用,处理这种应用的正确开启与关闭存在惊人的复杂性. 开启相对来说更简单点,常规做 ...

  3. JavaScript之this的工作原理

    JavaScript 有一套完全不同于其它语言的对 this 的处理机制. 在五种不同的情况下 ,this 指向的各不相同. 1.全局范围内 当在全部范围内使用 this,它将会指向全局对象. 2.函 ...

  4. 15 nginx反向代理实现nginx+apache动静分离

    一:nginx反向代理实现nginx+apache动静分离-------------概念--------------------------- nginx反向代理服务器+负载均衡 用nginx做反向代 ...

  5. 再看GS接包过程

    再看GS接包过程 bool GameServer::ProcessLoop(packet& rPkt) { if(false == m_spDataLayer->Recv(rPkt)) ...

  6. Mybatis中resultMap的基础配置

    一.概述 resultMap 元素是 MyBatis 中最重要最强大的元素.它就是让你远离 90%的需要从结果集中取出数据的 JDBC 代码的那个东西,而且在一些情形下允许你做一些 JDBC 不支持的 ...

  7. C#中GroupBox控件的使用(转)

    GroupBox(框架)控件是C#中用来组织其他控件形成一个控件组,它的使用方法为[工具箱]->[所有Windows窗体](或者是[容器]列表中)->[GroupBox],拖拽到窗体界面中 ...

  8. django url匹配过程

    ROOT_URLCONF root URLconf module urlpatterns “include” other URLconf modules chops off whatever part ...

  9. 关于dubbo的负载均衡

    1 dubbo的集群 将同一个服务部署到多个机器上,然后全部注册到注册中心.这样的多个机器就是一个dubbo集群了. 2 dubbo的负载均衡是怎么回事 由于多台机器上都有同一个服务,因此consum ...

  10. div+css清除浮动代码

    <style type="text/css"> .div1{ background:#000080; border:1px solid red;} .div2{ bac ...