POJ 2182&& POJ 2828:Lost Cows 从后往前 线段树
2015-09-30 19:33
316 查看
Lost Cows
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
Sample Output
有N头牛站成一列,每头牛只能看见自己前面比自己brand小的牛,这个数量给你了。问你每头牛的brand分别是多少。
就是查空位数量,如果有空位被占了,那么就往后面延。。。
只不过用了线段树的形式,以线段树的形式来找空位。len代表空位的数量。
代码:
POJ2828是它的姐妹题,买一送一。
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10544 | Accepted: 6754 |
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 从后往前 线段树
- 【Foundation-62-1】#import <Foundation/NSRange.h>范围
- 海思Hi3518平台mount /mnt/mtd/失败并目录都被删除
- 第三次作业——计划书导入软件的实现
- 写让别人能读懂的代码
- C++模板 静态成员 定义(实例化)
- HDU 1799 排列组合
- 剑指offer—从头到尾打印链表
- android 中如何分析内存泄漏
- iOS结合导航控制器和标签栏控制器
- 用栈对算术表达式求值
- 2015 ACM/ICPC Asia Regional Hefei Online
- 二分查找的递归实现和迭代实现
- SharePoint 2010 BCS - 概要
- 二分查找的递归实现和迭代实现
- WebView
- SSH搭建过程详解(一)
- 抽屉效果(DrawLayout)
- NSDictionary
- 第十周第三天