191113-Lost cows

191113-Lost cows

题目描述

传送门

解析

完整题意翻译:已知第i头牛前面有 a i a_i ai头牛比它低,求每头牛的身高(已知每头牛的身高为1~n且互不相同) n < = 1 e 5 n<=1e5 n<=1e5

解析:首先我们倒着来考虑整个奶牛序列,如果第n头牛前面有 a n a_n an头牛比它低,那么它的身高必为 a n + 1 a_n+1 an+1,再来考虑第n-1头牛
1,如果 a n − 1 < a n a_{n-1}<a_n an1<an,即第n-1头牛比第n头牛低,那么它的身高必为 a n − 1 + 1 a_{n-1}+1 an1+1,
2,否则,他的身高为 a n − 1 + 2 a_{n-1}+2 an1+2
后面的牛以此类推

那么其实题意可以简化为,维护一个01序列,倒序扫描每头牛,每次进行两次操作,假设当前扫描到第i头牛
1,在整个01序列中找第 a i + 1 a_i+1 ai+1个1的位置,该位置号即为第i头牛的身高
2,将原序列该位置变为0

那么我们考虑如何维护这个01序列?
1,树状数组+二分,单次操作: O ( l o g 2 n ) O(log^2n) O(log2n)
树状数组维护前缀1的个数,每次查找时,二分这个位置,最后再单点修改即可

2,树状数组+倍增 单次操作: O ( l o g n ) O(logn) O(logn)
不会,先咕掉

题解

1,树状数组+二分

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define M 100009
using namespace std;
int a[M],h[M],tree[M],n;
int read(){
	int f=1,re=0;
	char ch;
	for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
	if(ch=='-'){
		f=-1;
		ch=getchar();
	}
	for(;isdigit(ch);ch=getchar())
		re=(re<<3)+(re<<1)+ch-'0';
	return re*f;
}
int lowbit(int i){
	return i&(-i);
}
void update(int x,int y){
	while(x<=n){
		tree[x]+=y;
		x+=lowbit(x);
	}
}
int query(int x){
	int sum=0;
	while(x>0){
		sum+=tree[x];
		x-=lowbit(x);
	}
	return sum;
}
int main(){
	n=read();
	for(int i=2;i<=n;i++) a[i]=read();
	for(int i=1;i<=n;i++) update(i,1);
	for(int i=n;i>=1;i--){
		int l=1,r=n;
		while(l<r){
			int mid=(l+r)>>1;
			if(query(mid)>a[i]) r=mid;
			else l=mid+1;
		}
		h[i]=r;
		update(r,-1);
	}
	for(int i=1;i<=n;i++)
		printf("%d\n",h[i]);
	return 0;
}
his past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides work in both directions: a cow can slide from the slide's start to the end instantly, or from the end to the start. If a cow steps on a space that hosts either end of a slide, she must use the slide. The outside of the corn maze is entirely corn except for a single exit. The maze can be represented by an N x M (2 <= N <= 300; 2 <= M <= 300) grid. Each grid element contains one of these items: * Corn (corn grid elements are impassable) * Grass (easy to pass through!) * A slide endpoint (which will transport a cow to the other endpoint) * The exit A cow can only move from one space to the next if they are adjacent and neither contains corn. Each grassy space has four potential neighbors to which a cow can travel. It takes 1 unit of time to move from a grassy space to an adjacent space; it takes 0 units of time to move from one slide endpoint to the other. Corn-filled spaces are denoted with an octothorpe (#). Grassy spaces are denoted with a period (.). Pairs of slide endpoints are denoted with the same uppercase letter (A-Z), and no two different slides have endpoints denoted with the same letter. The exit is denoted with the equals sign (=). Bessie got lost. She knows where she is on the grid, and marked her current grassy space with the 'at' symbol (@). What is the minimum time she needs to move to the exit space? 输入格式 第一行:两个用空格隔开的整数 N 和 M。 第 2∼N+1 行:第 i+1 行描述了迷宫中的第 i 行的情况(共有M个字符,每个字符中间没有空格)。 输出格式 一个整数,表示起点到出口所需的最短时间。 隐藏翻译 题意翻译 奶牛们去一个 N×M 玉米迷宫,2≤N≤300,2≤M≤300。 迷宫里有一些传送装置,可以将奶牛从一点到另一点进行瞬间转移。这些装置可以双向使用。 如果一头奶牛处在这个装置的起点或者终点,这头奶牛就必须使用这个装置,奶牛在传送过后不会立刻进行第二次传送,即不会卡在传送装置的起点和终点之间来回传送。 玉米迷宫除了唯一的一个出口都被玉米包围。 迷宫中的每个元素都由以下项目中的一项组成: 玉米,# 表示,这些格子是不可以通过的。 草地,. 表示,可以简单的通过。 传送装置,每一对大写字母 A 到 Z 表示。 出口,= 表示。 起点, @ 表示 奶牛能在一格草地上可能存在的四个相邻的格子移动,花费 1 个单位时间。从装置的一个结点到另一个结点不花时间。
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值