POJ Lost Cows


【题解】
参考https://blog.csdn.net/acmer_hades/article/details/46272605。设置数组pre_smaller,其中第i个元素即为输入的第i项,则显然pre_smaller[1] = 0。build_tree建立树结构,分解区间[1, N],其中每个节的度要么为2,要么为0。query中参数smaller_num表示自身以及队列前方比自己编号小的cows的总数。确实是非常简洁的方法,将线段数组运用得淋漓尽致!不愧是NOI大神
【代码】
#include <iostream>
#include <cstdlib>
using namespace std; #define maxn 100001 int N;
int pre_smaller[maxn], ans[maxn]; struct node
{
int left_val, right_val, len;
}s[*maxn]; void build_tree(int root, int left_val, int right_val)
{
s[root].left_val = left_val;
s[root].right_val = right_val;
s[root].len = right_val - left_val + ;
if (left_val == right_val)return;
build_tree( * root, left_val, (left_val + right_val) / );
build_tree( * root + , + (left_val + right_val) / , right_val);
} int query(int root, int smaller_num)
{
s[root].len--;
if (s[root].left_val == s[root].right_val)return s[root].left_val;
else if (s[ * root].len >= smaller_num)return query( * root, smaller_num);
else return query( * root + , smaller_num - s[ * root].len);
} int main()
{
cin >> N;
for (int i = ; i <= N; i++)cin >> pre_smaller[i];
pre_smaller[] = ;
build_tree(, , N);
for (int i = N; i >= ; i--)ans[i] = query(, pre_smaller[i] + );
for (int i = ; i <= N; i++)printf("%d\n", ans[i]);
return ;
}
POJ Lost Cows的更多相关文章
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
- POJ 2481 Cows
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16546 Accepted: 5531 Description ...
- 2018.07.08 POJ 2481 Cows(线段树)
Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...
- POJ 2481 Cows (线段树)
Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...
- POJ 2481 Cows(树状数组)
Cows Time Limit: 3000MS Memory L ...
- POJ 3348 - Cows 凸包面积
求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...
- POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)
题目链接:http://poj.org/problem?id=2186 题目大意:有n头牛和m对关系, 每一对关系有两个数(a, b)代表a牛认为b牛是“受欢迎”的,且这种关系具有传递性, 如果a牛认 ...
- POJ 2481 Cows (数组数组求逆序对)
题目链接:http://poj.org/problem?id=2481 给你n个区间,让你求每个区间被真包含的区间个数有多少,注意是真包含,所以要是两个区间的x y都相同就算0.(类似poj3067, ...
- POJ 3621Sightseeing Cows
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9851 Accepted: 3375 Description Farme ...
- POJ 3348 Cows [凸包 面积]
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9022 Accepted: 3992 Description ...
随机推荐
- jquery trigger函数和triggerHandler函数的对照
一句话的差别就是:trigger will bubbling jQuery events (not default DOM events) and triggerHnadler will not do ...
- HI3516EV100 RTMP添加音频
1.先试试只发音频 失败 2.保存音频,发送视频,成功
- python时间戳转时间
import time timestamp = 1462451334 #转换成localtime time_local = time.localtime(timestamp) #转换成新的时间格式(2 ...
- Docker和Rancher
Docker打包流程: Dockerfile文件和要打包docker的文件放在同级目录下: 1. docker build -t proj:proj-app:0.0.1 返回tagXXX 2. doc ...
- Anaconda 安装 Python 库(MySQLdb)的方法-(转)
安装python库的过程中,最重要的地方就是版本需要兼容.其中操作系统为64位,Python为2.X 64位,下载安装文件的时候也要注意版本匹配.其中文件名中包含的cp27表示CPython 2.7版 ...
- iOS开发 SourceTree将develop合并到master分支的详细步骤
- OpenWrt挂载移动硬盘实现脱机下载
一.编译选项选好好usb存储驱动,参考http://www.cnblogs.com/smbx-ztbz/p/4418245.html 并且选上kmod-usb-hid,用于usbhub. 二.编译选项 ...
- 一:elasticsearch常用操作总结
索引 搜索 mapping 分词器 1.创建索引 http://192.168.65.131:9200/smartom_index 2.查看索引: http://192.168.65.131:9200 ...
- 两招解决异常_Cannot find any information on property 'XXX' in a bean of type 'XXX'的问题
第一招 在进行Java Web项目开发的时候,我碰到过下面这个异常: Cannot find any information on property 'XXX' in a bean of type ' ...
- ALGO-12_蓝桥杯_算法训练_幂方分解(递归)
问题描述 任何一个正整数都可以用2的幂次方表示.例如: =++ 同时约定方次用括号来表示,即ab 可表示为a(b). 由此可知,137可表示为: ()+()+() 进一步:= ++ (21用2表示) ...