Lost Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10544   Accepted: 6754

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

有N头牛站成一列,每头牛只能看见自己前面比自己brand小的牛,这个数量给你了。问你每头牛的brand分别是多少。

就是查空位数量,如果有空位被占了,那么就往后面延。。。

只不过用了线段树的形式,以线段树的形式来找空位。len代表空位的数量。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no
{
int L,R;
int len;
}tree[32005]; void buildtree(int root,int L,int R)
{
tree[root].L=L;
tree[root].R=R; tree[root].len = R-L+1; if(L!=R)
{
int mid = (L+R)>>1;
buildtree((root<<1)+1,L,mid);
buildtree((root<<1)+2,mid+1,R);
}
} int query(int root,int num)
{
tree[root].len--;
if(tree[root].L==tree[root].R)
{
return tree[root].L;
}
if(tree[(root<<1)+1].len >= num)
{
return query((root<<1)+1,num);
}
else
{
return query((root<<1)+2,num-tree[(root<<1)+1].len);
}
} int n;
int val[8005];
int ans[8005]; int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
buildtree(0,1,n);
val[1]=0; for(i=2;i<=n;i++)
{
scanf("%d",val+i);
}
for(i=n;i>=1;i--)
{
ans[i]=query(0,val[i]+1);
}
for(i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
}
return 0;
}

POJ2828是它的姐妹题,买一送一。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no
{
int L,R;
int len;
}tree[800005]; int n;
int val[200005];
int pos[200005];
int cnt[200005]; void buildtree(int root,int L,int R)
{
tree[root].L=L;
tree[root].R=R; tree[root].len = R-L+1; if(L!=R)
{
int mid = (L+R)>>1;
buildtree((root<<1)+1,L,mid);
buildtree((root<<1)+2,mid+1,R);
}
} void query(int root,int num,int val)
{
tree[root].len--;
if(tree[root].L==tree[root].R)
{
cnt[tree[root].L]=val;
return;
}
if(tree[(root<<1)+1].len >= num)
{
query((root<<1)+1,num,val);
}
else
{
query((root<<1)+2,num-tree[(root<<1)+1].len,val);
}
} int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
buildtree(0,1,n); for(i=1;i<=n;i++)
{
scanf("%d%d",pos+i,val+i);
}
for(i=n;i>=1;i--)
{
query(0,pos[i]+1,val[i]);
}
for(i=1;i<=n;i++)
{
if(i==1)
printf("%d",cnt[i]);
else
printf(" %d",cnt[i]);
}
printf("\n");
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2182&& POJ 2828:Lost Cows 从后往前 线段树的更多相关文章

  1. POJ 2182 / HDU 2711 Lost Cows(平衡树)

    Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular di ...

  2. POJ 2182 Lost Cows(牛排序,线段树)

    Language: Default Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9207   Acce ...

  3. poj 2482 Stars in Your Window + 51Nod1208(扫描线+离散化+线段树)

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13196   Accepted:  ...

  4. POJ 3162 Walking Race(树形dp+单调队列 or 线段树)

    http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...

  5. POJ 2886 Who Gets the Most Candies?(反素数+线段树)

    点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...

  6. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  7. POJ 2352 Stars(线段树)

    题目地址:id=2352">POJ 2352 今天的周赛被虐了. . TAT..线段树太渣了..得好好补补了(尽管是从昨天才開始学的..不能算补...) 这题还是非常easy的..维护 ...

  8. poj 3468 A Simple Problem with Integers(原来是一道简单的线段树区间修改用来练练splay)

    题目链接:http://poj.org/problem?id=3468 题解:splay功能比线段树强大当然代价就是有些操作比线段树慢,这题用splay实现的比线段树慢上一倍.线段树用lazy标记差不 ...

  9. POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

    题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...

随机推荐

  1. js保留两位小数的数字格式化方法

    // 格式化数字(保留两位小数) numberFormat (num) { let percent = Number(num.toString().match(/^\d+(?:\.\d{0,2})?/ ...

  2. 微信web版接口api(转)

    安卓微信的api,个人微信开发API协议,微信 ipad sdk,微信ipad协议,微信web版接口api,微信网页版接口,微信电脑版sdk,微信开发sdk,微信开发API,微信协议,微信接口文档sd ...

  3. 如何使用Docker部署PHP开发环境

    本文主要介绍了如何使用Docker构建PHP的开发环境,文中作者也探讨了构建基于Docker的开发环境应该使用单容器还是多容器,各有什么利弊.推荐PHP开发者阅读.希望对大家有所帮助. 环境部署一直是 ...

  4. 使用onclick报SyntaxError: identifier starts immediately after numeric literal

    少了‘’ 错误 onclick="onlineWatch(${row.title})" 正确 onclick="onlineWatch('${row.title}')&q ...

  5. leetcode142 Linked List Cycle II

    """ Given a linked list, return the node where the cycle begins. If there is no cycle ...

  6. loadrunner-11安装+破解+汉化

    一.loadrunner-11安装下载地址:链接:https://pan.baidu.com/s/10meUz5DfkS8WleLSOalCtQ 提取码:iw0p 由于LR11安装包三个多G,没办法上 ...

  7. [Luogu][P2458] [SDOI2006]保安站岗

    题目链接 看起来似乎跟最小点覆盖有点像.但区别在于: 最小点覆盖要求所有边在其中,而本题要求所有点在其中. 即:一个点不选时,它的儿子不一定需要全选. 画图理解: 对于这样一幅图,本题中可以这样选择: ...

  8. 二进制中1的个数(n=(n&n-1))

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题:利用Java系统提供的函数Integer.toBinaryString(n),将整数转化为二进制,之后再将二进制的0用 ...

  9. SNOI2019 选做

    施工中... d1t1 字符串 题面 考虑两个字符串 \(s_i,s_j(i<j)\) ,实质是 \(s[i+1,\dots j]\) 和 \(s[i,\dots ,j-1]\) 的字符串字典序 ...

  10. C++ mfc 简易文本编辑器 遇到的一些问题

    [题目40]简易文本编辑器. 设计一个简易的文本编辑器. 设计要求: (1) 具有图形菜单界面: (2) 查找,替换(等长,不等长),插入(插串,文本块的插入).文本块移动(行块,列块移动),删除; ...