// Slower C++ solution:
/*9:00 UTC+4.5

Contest2 [Cloned]

End: 2019-09-02 13:00 UTC+4.5

04:00:00

Ended

Overview

Problem

Status

Rank (04:00:00)

0 Comments

Previous12345…Next

Username



Amirack

Prob

	Result 

	Time

(ms)	Mem

(MB)	Lang 

	Submit Time

Amirack

E

Wrong Answer

C++

6 days ago

Amirack

E

Language Rejected

C++

6 days ago

Amirack

B

Accepted

6	1.4	

C++

6 days ago

Amirack

B

Wrong Answer

C++

6 days ago

Amirack

A

Accepted

1	0.3	

C++

6 days ago

           

All Copyright Reserved © 2010-2019 Xu Han

Server Time: 2019-09-08 18:16:44 UTC+4.5



#21598412 | Amirack's solution for [AtCoder-2687] [Problem B]

Status

Accepted

Time

6ms

Memory

1408kB

Length

1046

Lang

C++14 (GCC 5.3.0)

Submitted

2019-09-02 10:16:44

Shared



RemoteRunId

7304502

25.0 / 25.0

Select Code

*/

#include <bits/stdc++.h>

using namespace std;

typedef  long long ll ;

const int maxn=1e2+3,maxxn=1e4+2;

vector<pair<int,int> >v[maxn][maxn];

bool mark[maxn][maxn];

int ans[maxn][maxn];

pair<int,int>p[maxxn];

int c;

void dfs(int u,int k){

    mark[u][k]=1;

    if(p[c].first<=0)c++;

    ans[u][k]=p[c].second;

    p[c].first--;

    for(int i=0;i<v[u][k].size();i++){

        int x1=v[u][k][i].first,y1=v[u][k][i].second;

        if(!mark[x1][y1])dfs(x1,y1);

    }

}

int main() {

    int h,w,n;

    cin>>h>>w>>n;

    for(int i=0;i<n;i++){

        int x;

        cin>>x;

        p[i]=make_pair(x,i+1);

    }

    for(int i=1;i<=h;i++){

        for(int j=1;j<=w;j++){

            if(j+1<=w)v[i][j].push_back(make_pair(i,j+1));

            if(j-1>0)v[i][j].push_back(make_pair(i,j-1));

            if(i+1<=h)v[i][j].push_back(make_pair(i+1,j));

            if(i-1>0)v[i][j].push_back(make_pair(i-1,j));

        }

    }

    dfs(1,1);

    for(int i=1;i<=h;i++){

        for(int j=1;j<=w;j++)cout<<ans[i][j]<<" ";

        cout<<endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.249329030810117x):
#include <bits/stdc++.h>





#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>



#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

#define all(x) x.begin(),x.end()

#define sz(x) (int)x.size()





using namespace std;

using namespace __gnu_pbds;



template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

using ll  = long long;



using point = complex<double>;





void err(istream_iterator<string> it) {cerr << endl;}

template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {cerr << *it << " = " << a << endl;err(++it, args...);}





const double EPS = 1e-8;

const long long mod = 1e9+7;



const int dx[] = {0, 0,1,-1,1,-1, 1,-1};

const int dy[] = {1,-1,0, 0,1,-1,-1, 1};



const int N = 3e5 + 5;





int n , m;

int arr[200][200];

int x;

int col[10060];

bool vis[205][205];



int main()

{

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;

    cin >> x;

    for(int i = 0; i < x; ++i)

        cin >> col[i];

    int lx , ly;

    lx = 0,ly = 0;

    bool isr = 1;

    for(int i = 0; i < x; ++i){

        for(int j = 0; j < col[i]; ++j){

           arr[lx][ly] = i+1;

           if(isr)ly++;

           else ly--;

           if(ly == m){

                isr = 0;

                ly = m - 1;

                lx++;

           }else if(ly == -1){

                isr = 1;

                ly = 0;

                lx++;

           }



        }

    }

    for(int i = 0; i < n; ++i)

        for(int j = 0; j < m; ++j)

            cout << arr[i][j] << " \n"[j == m - 1];

    return 0;

}