列表

详情


NC51101. Lost Cows

描述

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

输入描述

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

输出描述

* 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

输入:

5
1
2
1
0

输出:

2
4
5
3
1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 103ms, 内存消耗: 388K, 提交时间: 2023-04-09 11:59:11

#include<stdio.h>
int main() {
	int a[8000];
	int n;
	scanf("%d", &n);
	a[0] = 1;
	for (int i = 1; i < n; i++)
		scanf("%d", &a[i]);
	for (int i = 1; i < n; i++) {
		a[i] = a[i] + 1;
		for (int j = 0; j < i; j++)
			if (a[j] >= a[i])
				a[j] = a[j] + 1;
	}
	for (int i = 0; i < n; i++)
		printf("%d\n", a[i]);
	return 0;
}

pypy3 解法, 执行用时: 87ms, 内存消耗: 23612K, 提交时间: 2023-04-09 10:43:02

from sys import stdin, stdout
input = lambda: stdin.readline().strip()
print = lambda x: stdout.write(str(x) + '\n')


n = int(input())
lis = [1]
for i in range(2, n + 1):
    t = int(input())
    lis.insert(t, i)
dit = {}
for i in range(n):
    dit[lis[i]] = i + 1
for i in range(1, n + 1):
    print(dit[i])

C++11(clang++ 3.9) 解法, 执行用时: 41ms, 内存消耗: 480K, 提交时间: 2019-11-24 21:30:10

#include<stdio.h>
int a[8005],vis[8005],b[8005];
int main(){
	int n;
	scanf("%d",&n);
	a[1]=0;
	for(int i=2;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=n;i>=1;i--){
		int t=a[i]+1;
		for(int j=1;j<=t;j++)
			if(vis[j])
				t++;
		b[i]=t;
		vis[t]=1;
	}
	for(int i=1;i<=n;i++)
		printf("%d\n",b[i]);
}

Python3 解法, 执行用时: 324ms, 内存消耗: 5256K, 提交时间: 2023-04-09 10:39:56

a=int(input())
b=[]
for i in range(a-1):
    b.append(int(input()))
b=b[::-1]
c=list(range(1,a+1))
d=[]
for i in b:
    d.append(c[i])
    c.remove(c[i])
d.append(c[0])
d = d[::-1]
for i in range(a):
    print(d[i])

上一题