https://codeforces.com/problemset/problem/1517/C
题目大意是说给我们一个长度为n的数组表示一个n*n的矩阵的主对角线
能否让每个数字的值输出相应个数的元素
因为题目的输入是从1到n,所以不需要考虑溢出的情况
我们可以模拟一下这个过程,可以发现,只要输入合法,那么就一定会有答案
因此,我们可以采用从左到右的方法来进行输出,使数据尽量贴近左侧,这样输出就可以实现题目的目标了
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 4e2 + 10;
typedef long long ll;
ll a[505][505];
ll b[505][505];
ll n;
void dfs(ll x, ll y, ll k)
{
if (b[k][k] <= 1)return;
if (y - 1 >= 1 && !a[x][y - 1])
{
a[x][y - 1] = a[k][k];
b[k][k]--;
dfs(x, y - 1, k);
}
else if (x + 1 <= n && !a[x + 1][y])
{
a[x + 1][y] = a[k][k];
b[k][k]--;
dfs(x + 1, y, k);
}
else
{
b[k][k]--;
a[x][y + 1] = a[k][k];
dfs(x, y + 1, k);
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i][i];
b[i][i] = a[i][i];
}
for (int i = 1; i <= n; i++)dfs(i, i, i);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << a[i][j] << ' ';
}
cout << endl;
}
return 0;
}