[Usaco2003 Open]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.
1~n,乱序排列,告诉每个位置的前面的数字中比它小的数的个数,求每个位置的数字是多少
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 i
n slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in sl
ot #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
一个比较有趣的题目,线段树里面维护当前区间内还有多少个数,每次从找出原序列最后一个数(因为不会对其他数造成影响)
至于怎么找数?在线段树里找到第k大的数,这是非常简单的
(ps:这题除了用到线段树思想外,就和线段树没有任何关系了……)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=8e3;
int tree[N*3+10],a[N+10],ans[N+10];
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*f;
}
#define ls (p<<1)
#define rs (p<<1|1)
void build(int p,int l,int r){
tree[p]=r+1-l;
if (l==r) return;
int mid=(l+r)>>1;
build(ls,l,mid),build(rs,mid+1,r);
}
int query(int p,int l,int r,int t){
tree[p]--;//记得减掉
if (l==r) return l;
int mid=(l+r)>>1;
return t<=tree[ls]?query(ls,l,mid,t):query(rs,mid+1,r,t-tree[ls]);//两边判断查询
}
int main(){
int n=read();
build(1,1,n);
for (int i=2;i<=n;i++) a[i]=read();
for (int i=n;i;i--) ans[i]=query(1,1,n,a[i]+1);//a[i]+1是其本身的位置
for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
}
[Usaco2003 Open]Lost Cows的更多相关文章
- BZOJ(begin) 1328 [Usaco2003 Open]Jumping Cows:贪心【波峰波谷模型】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1328 题意: 给你一个长度为n的正整数序列. 可以选任意个数字,只能从左往右选. 偶数 ...
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- Poj2186Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31533 Accepted: 12817 De ...
随机推荐
- Java多线程导致的的一个事物性问题
业务场景 我们如今有一个类似于文件上传的功能.各个子网站接受业务,业务上传文件,各个子网站的文件须要提交到总网站保存.文件是按批次提交到总网站的,也就是说,一个批次以下约有几百个文件. 考虑到白天提交 ...
- Office EXCEL 的绝对引用和相对引用如何理解
比如C1 = A1+B1,则我把C1的单元格往下拖拉的时候,C2会自动等于A2+B2,C3会自动等于A3+B3,而如果让G1 = $E$1+$F$1,则把G1单元格往下拖拉的时候,G2G3单元格都不会 ...
- Android从无知到有知——NO.6
紧随上一篇,说一下创建ip拨号器过程中出现的一些问题. 1)在一開始监听外拨电话的时候会报这样一个警告: Permission Denial: receiving Intent { act=andro ...
- leetcode ----Trie/stack专题
一:Implement Trie (Prefix Tree) 题目: Implement a trie with insert, search, and startsWith methods. Not ...
- Nerv --- React IE8 兼容方案
创建项目 创建一个目录,使用npm快速初始化 $ mkdir my-project && npm init -y 安装依赖 安装webpack以及babel $ npm install ...
- Jenkins系列之-—02 email-ext 邮件模板
邮件通知配置 系统管理 → 系统设置 → 邮件通知 SMTP 服务器:配置 SMTP 服务器.(不填默认本地运行服务器) 用户默认邮件后缀:注册用户邮件只需填写用户名即可,后缀会加该后缀,如果填写,则 ...
- 基于 Vue.js 之 iView UI 框架非工程化实践记要 使用 Newtonsoft.Json 操作 JSON 字符串 基于.net core实现项目自动编译、并生成nuget包 webpack + vue 在dev和production模式下的小小区别 这样入门asp.net core 之 静态文件 这样入门asp.net core,如何
基于 Vue.js 之 iView UI 框架非工程化实践记要 像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引 ...
- mysql + Fluently NHibernate + WebAPI + Autofac
MySQL.Fluently NHibernate.WebAPI.Autofac,对我来说每一个都是麻烦疙瘩,现在它们为了一个共同的项目而凑合到一起了.一路磕磕碰碰,现在貌似有了一点眉目. 作为一个步 ...
- eclispe pydev tab改回 空格找到方法了,这个链接:http://stackoverflow.com/questions/23570925/eclipse-indents-new-line-with-tabs-instead-of-spaces
看这个链接: 3down votefavorite 1 I've followed all the suggestions here. When I press return, I get a new ...
- 使用变量作为js对象的属性名
<script> var test={aa:12,bb:34};//或者var test={}; var cc= "acqId" test[cc]=12; alert( ...