26C - Parquet - CodeForces Solution


combinatorics constructive algorithms greedy implementation *2000

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 110
int n,m,a,b,c;
char ma[N][N];
bool vis[N];
void w(int x,int y,int a,int b)
{
    for(int i = 'a';i <= 'e';++i) vis[i] = false;
    for(int i = 0;i < a;++i) vis[ma[x + i][y - 1]] = true;
    for(int i = 0;i < b;++i) vis[ma[x - 1][y + i]] = true;

    char ch = 0;
    for(int i = 'a';i <= 'e';++i) 
        if(vis[i] == false) ch = i;
    
    for(int i = 0;i < a;++i)
        for(int j = 0;j < b;++j) ma[x + i][y + j] = ch;
}
bool work()
{
    if(n % 2 == 1 && m % 2 == 1) return false;

    int needa = (n % 2 == 1 ? m / 2 : 0);
    int needb = (m % 2 == 1 ? n / 2 : 0);
    int needc = (n / 2) * (m / 2);
    int numa = (a - needa) / 2;
    int numb = (b - needb) / 2;

    if(needc > numa + numb + c || needa > a || needb > b) return false;

    for(int i = 1;i <= n;++i)
        for(int j = 1;j <= m;++j)
            if(ma[i][j] != 0) continue;
            else if(i == n) w(i,j,1,2);
            else if(j == m) w(i,j,2,1);
            else if(c > 0) w(i,j,2,2),c--;
            else if(numa > 0) w(i,j,1,2),w(i + 1,j,1,2),numa--;
            else if(numb > 0) w(i,j,2,1),w(i,j + 1,2,1),numb--;

    return true;
}
signed main()
{
    ios::sync_with_stdio(false);

    cin>>n>>m>>a>>b>>c;
    if(work() == false) cout<<"IMPOSSIBLE"<<endl;
    else for(int i = 1;i <= n;++i) cout<<ma[i] + 1<<endl;

    return 0;
}


Comments

Submit
0 Comments
More Questions

1366C - Palindromic Paths
336A - Vasily the Bear and Triangle
926A - 2-3-numbers
276D - Little Girl and Maximum XOR
1253C - Sweets Eating
1047A - Little C Loves 3 I
758D - Ability To Convert
733A - Grasshopper And the String
216A - Tiling with Hexagons
1351B - Square
1225A - Forgetting Things
1717A - Madoka and Strange Thoughts
1717B - Madoka and Underground Competitions
61B - Hard Work
959B - Mahmoud and Ehab and the message
802G - Fake News (easy)
1717C - Madoka and Formal Statement
420A - Start Up
1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys