POJ 2182 Lost Cows(暴力or线段树or树状数组+二分)(思维)

点击这里去寻找母牛的顺序

Lost Cows

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14186 Accepted: 9043

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. 

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 in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #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

每头牛都有自己的标号,他们的顺序乱了,但是知道他的前面有几头比自己标号小的牛  我们存在数组a[n]中

因为刚做完一道差分的Tallest Cows ,测试就碰到这个了,还以为是差分呢,,,事实并不是这样

咱们先看最后一头牛,标号最大,那么他的位置一定是a[n]+1,想想是为什么

这时候我们从后往前看,我们不看最后一头牛的位置,道理也是一样的

我们就可以开一个标记数组  book[n],一开始的时候置零,那么从后往前遍历a[i]的时候,第a[i]+1个 0 的位置就是当前这个 i 的位置,存起来,找到后我们将这个位置的标记改成 1 。遍历完就全部找到喽

这里还有一个类似的题目可以趁机来练练手,原理一模一样,我就不多说了

http://poj.org/problemlistBuy Tickets

这是暴力算法

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=8888;

int main()
{
	int a[mm];
	a[1]=0;
	int n;
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
		scanf("%d",&a[i]);
	int book[mm];
	int res[mm];//
	mem(book,0);
	res[n]=a[n]+1;
	book[a[n]+1]=1;
	
	int cnt=0;
	for(int i=n-1;i>=1;i--){
		for(int j=1;j<=n;j++){
			if(book[j]==0)
				cnt++;
			if(cnt==a[i]+1){
				res[i]=j;
				cnt=0;	
				book[j]=1;
				break;
			}	
		}
	}
	for(int i=1;i<=n;i++)
		printf("%d\n",res[i]);
	return 0;
}

其实这个道理明白之后,还可以用线段树给写出来,同理我们先将每个叶子结点置为1,同样是从后往前遍历,只不过这里的位置就要用线段树的 sum 来反应,同样是找到之后置零   仔细想想 原理是一样的

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=8888;

int a[mm];
int res[mm];
struct node{
	int l,r;
	int sum;
}pp[mm<<2];
 
void build(int k,int l,int r){
	pp[k].l=l;
	pp[k].r=r;
	if(l==r){
		pp[k].sum=1;
		return ;
	}
	int mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	pp[k].sum=pp[k<<1].sum+pp[k<<1|1].sum;
}

int get(int k,int pos){
	pp[k].sum--;//一定在区间内
	if(pp[k].l==pp[k].r)
		return pp[k].l;
	if(pos<=pp[k<<1].sum)
		return get(k<<1,pos);
	else 
		return get(k<<1|1,pos-pp[k<<1].sum);//注意右边的查询	
}

int main()
{
	int n;
	a[1]=0;
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
		scanf("%d",&a[i]);
	build(1,1,n);
	for(int i=n;i>=1;i--)
		res[i]=get(1,a[i]+1);
	for(int i=1;i<=n;i++)
		printf("%d\n",res[i]);
	return 0;
}

还有就是可以用树状数组,但你需要确定第 a[i]+1 个 1 的位置,这就要用到二分去查找

其实树状数组就是线段树的简化,而线段树本身就含有二分的性质,所以二者是一个意思

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=8888;
int n;
int a[mm],c[mm];
int res[mm];

int lbt(int x){
	return x&-x;
}

void change(int pos,int k){
	while(pos<=n){
		c[pos]+=k;
		pos+=lbt(pos);
	}
}

int getsum(int pos){
	int res=0;
	while(pos>0){
		res+=c[pos];
		pos-=lbt(pos);
	}
	return res;
}

int find(int num){//二分查找位置 确定第a[i]+1个1的位置 
	int l=1,r=n;
	while(l<=r){
		int mid=(l+r)>>1;
		int sum=getsum(mid);
		if(sum>=num)
			r=mid-1;
		else 
			l=mid+1;
	}
	return l;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		change(i,1);//初始化为1  
		
	a[1]=0;
	for(int i=2;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=n;i>=1;i--){
		int pos=find(a[i]+1);
		change(pos,-1);
		res[i]=pos;
	} 
	for(int i=1;i<=n;i++)
		printf("%d\n",res[i]);

	return 0;
 } 

 

POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值