Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity.
There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange.
![](https://i-blog.csdnimg.cn/blog_migrate/a0b1a2230ebad7802ee9dc030d3e4a02.png)
Given the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch!
The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the box. The next line contains n space-separated integer numbers. The i-th number ai (1 ≤ ai ≤ 100) denotes the number of cubes in the i-th column.
Output n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switch.
4 3 2 1 2
1 2 2 3
3 2 3 8
2 3 8
The first example case is shown on the figure. The top cube of the first column falls to the top of the last column; the top cube of the second column falls to the top of the third column; the middle cube of the first column falls to the top of the second column.
In the second example case the gravity switch does not change the heights of the columns.
题目大意:
给一个积木矩阵,有n列,每列有a[i]个,首先是重心在下方,然后突然把重心调到右边,然后再计算每一列有多少个
解法:
模拟一下,当倒过来,就计算每一个积木停留在哪里,最后再输出.
PS:以后拿到题目想到什么算法并且理论上可以过就写,不要再是否有更先进的算法,耽误时间.这题想是否有先进的算法,结果18MIN才提交= =,血的教训
#include <cstdio>
int n, len;
bool map[110][110];
void init() {
scanf("%d",&n);
for (int i = 1; i <= n; i++) {
int tmp;
scanf("%d",&tmp);
if (tmp > len) len = tmp;
for (int j = 1; j <= tmp; j++) map[i][j] = true;
map[i][0] = true;
}
}
void solve() {
for (int j = 1; j <= len; j++) {
for (int i = n-1; i >= 1; i--) {
int k = i;
while (map[k][j] && !map[k+1][j] && k < n) {
map[k+1][j] = true;
map[k][j] = false;
k++;
}
}
}
for (int i = 1; i <= n; i++) {
int j = 0;
while (map[i][j+1]) j++;
printf("%d ", j);
}
}
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
init();
solve();
}