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 ...
随机推荐
- day 49 html 学习 css 学习
画圆 <style> div{width: 100px; height:100px; border: solid red 3px; /*当弧度为.圆角半径为30时 得到的图像*/ bord ...
- The difference among ioctl, unlocked_ioctl and compat_ioctl (RT)
Meta-answer: All the raw stuff happening to the Linux kernel goes through lkml (the Linux kernel mai ...
- 10 Rules of Highly Successful Project Management
I commited the information below to report PDU of PMI. ^_^. In this paper, the author introduces his ...
- css属性的继承性
不可继承属性: 盒子模型属性:width height padding border margin 背景相关属性:background相关(background-image background-si ...
- linux shell创建目录、遍历子目录
1.创建目录 代码1: #!/bin/bash#如果没有tmp_dir目录则创建static_dir="/web/fyunw.com/static"if [ ! -d $staic ...
- 解决java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
启动eclipse 发现如下错误 Error configuring application listener of class org.springframework.web.util.Log4jC ...
- Eureka的高可用
问题: 现在Eureka和Client是1对1,但是Eureka挂了,就不能用了. 如何解决呢,创建多个Erurka.并且Eureka进行相互注册.如下图 怎么相互注册呢 1. 创建两个Eureka ...
- Tornado 文件操作笔记
import tornado.web import tornado.ioloop import tornado.options import tornado.httpserver from torna ...
- format()的简单实用 笔记
# 关于format和format_map的使用# 如果要使用输出的字符串对其不仅仅是可以使用format,还可以使用ljust/rjust/center来处理,输出当然也可以是使用%来进行操作,但是 ...
- [MySQL FAQ]系列 — processlist中哪些状态要引起关注 解决mysql cpu过高问题
show processlist; 一般而言,我们在processlist结果中如果经常能看到某些SQL的话,至少可以说明这些SQL的频率很高,通常需要对这些SQL进行进一步优化. 今天我们要说的是, ...