GDUT_22级寒假训练专题五J - Lost Cows

这是一个编程问题,描述了一群喝醉的奶牛未能按品牌号顺序排队。FJ只知道每头奶牛前面有多少品牌更小的奶牛。给定这些信息,我们需要实现一个算法,根据这个统计数据恢复原始的顺序。提供的C++代码示例使用倒序遍历来解决此问题。
摘要由CSDN通过智能技术生成

题目描述:

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.

大意:

N(2<=N<=8000)头奶牛拥有1..N范围内的独特品牌。他们参观了附近的“水坑”,并在晚餐前喝了太多啤酒。当该排队吃晚餐时,他们并没有按照品牌的升序排列。

遗憾的是,FJ没有办法对它们进行排序。此外,他不太善于观察问题。他没有写下每头牛的品牌,而是确定了一个相当愚蠢的统计数字:对于每一头奶牛,他知道排在这头奶牛前面的奶牛的数量,事实上,这些奶牛的品牌比这头奶牛小。

根据这些数据,告诉FJ奶牛的确切顺序。

输入格式

* 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.

*第1行:单个整数,N

*第2..N行:这些N-1行描述了排在给定奶牛前面且品牌小于该奶牛的奶牛数量。当然,没有奶牛排在第一头奶牛之前,所以她没有被列出来。输入的第2行描述了其品牌小于槽#2中奶牛的在先奶牛的数量;第3行描述了其品牌小于槽#3中奶牛的在先奶牛的数量;等等

输出格式:

* 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.

*第1..N行:每N行输出告诉一头牛的品牌。输出的第1行告诉第一头牛的品牌;第二行告诉第二头牛的品牌;等等

输入输出样例

输入 #1

5

1

2

1

0

输出 #1

2

4

5

3

1

思路:

倒序查询,最后一个的排名一定是ai+1,排除其影响,继续从后往前依次查询,重复遍历全部即可。

代码:

#include <iostream>
#include <string.h>
using namespace std;



int main(){
	int n;
	cin>>n;
	int a[10000]={},sum1[10000]={};
	bool b[10000]={};
	memset(b,false,sizeof(b));
	for(int i=2;i<=n;i++){
		cin>>a[i];
	}
	for(int i=n;i>0;i--){
		int sum=0;
		for(int j=1;j<=n;j++){
			if(sum==a[i]&&b[j]==false){
				b[j]=true;
				sum1[i]=j;
				break;
			}
			if(b[j]==false){
				sum++;
			}
		}
	}
	for(int i=1;i<=n;i++){
		printf("%d\n",sum1[i]);
	}
	
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值