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 ...
随机推荐
- 简单webpack plugin 开发
重要是学习下怎么开发webpack plugin,同时记录下 插件模型 webpack 是一个插件,可以是javascript class ,或者具名 class 定义apply 方法 指定一个绑定到 ...
- pipenv 方便的python 开发工作流工具
pipenv 将 composer.bundler.npm.yarn.cargo 等比较方便的包管理工具添加到了python 语言中,可以 帮助我们自动的管理virtualenv ,同时可以方便的从p ...
- DevExpress开发win8风格界面
由于近期在对项目软件界面进行优化,找到了一款效果挺炫的插件,DevExpress15.2,可以制作win8可以滑动图标那个界面的效果,不多说,先贴图: (你没看错,这是用C#winform实现的) 可 ...
- Quest for sane signals in Qt - step 1 (hand coding a Q_OBJECT)
探索qt的信号ref: http://crazyeddiecpp.blogspot.hk/2011/01/quest-for-sane-signals-in-qt-step-1.html If it ...
- ORACLE 12C 之集群日志位置变化
如果你还是使用 oracle 11g RAC 的目录结构方式寻找集群的日志,你会发现目录中所有的日志都是空的.actdb21:/oracle/app/12.2.0/grid/log/actdb21(+ ...
- C#:decimal的去0显示
public static string DecimalToString(decimal d) { return d.ToString("#0.######"); } 后面的0会给 ...
- Spring Boot基础知识
Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...
- SourceInsight宏插件1(非常好用,强力推荐)
对于一直使用sourceinsight编辑C/C++代码的工程师们,sourceinsight是一个非常好用的编辑工具可以任意定位,跳转,回退,本人一直使用该工具做C/C++开发,sourceinsi ...
- inf 启动
How to install an INF file using Delphi If you need to install an "inf" file using Delphi, ...
- URL中传递参数给视图函数
1. 采用在url中使用变量的方式: 在path的第一个参数中,使用<参数名>的方式可以传递参数.然后在视图函数中也要写一个参数,视图函数中的参数必须和url中的参数名称保持一致,不然就找 ...