結果

問題 No.1123 Afforestation
ユーザー catupper
提出日時 2020-07-23 00:58:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 481 ms / 2,500 ms
コード長 2,601 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 100,648 KB
実行使用メモリ 24,960 KB
最終ジャッジ日時 2024-06-23 03:43:54
合計ジャッジ時間 13,299 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 90
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<complex>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

bool edge[4400][4400];
Int h, w;
Int a[4400], b[4400];
string ans[2200];

void ok(){
    cout << "Yes" << endl;
    exit(0);
}

void ng(){
    cout <<  ":(" << endl;
    exit(0);
}

bool come[4400];

bool f(int s){
    if(come[s])return false;
    come[s] = true;
    if(s >= h && b[s-h]){
        b[s-h]--;
        return true;
    }
    for(int i = 0;i < 4400;i++){
        if(edge[s][i] && f(i)){
            edge[s][i] = false;
            edge[i][s] = true;
            return true;
        }
    }
    return false;
}

int main(){
    cin >> h >> w;
    for(int i = 0;i < h;i++)ans[i] = string(w, '.');
    for(int i = 0;i < h;i++)cin >> a[i];
    for(int i = 0;i < w;i++)cin >> b[i];
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            edge[i][j+h] = true;
        }
    }
    for(int i = 0;i < h;i++){
        vector<P> bs;
        for(int j = 0;j < w;j++)bs.push_back({b[j], j});
        sort(bs.rbegin(), bs.rend());
        for(int j = 0;j < w;j++){
            if(a[i]>0 && bs[j].first>0){
//                cout << i << " " << bs[j].second << endl;
                a[i]--;
                b[bs[j].second]--;                
                edge[i][bs[j].second+h] = false;
                edge[bs[j].second+h][i] = true;
            }
        }
    }
    
    for(int i = 0;i < h;i++)if(a[i])ng();
    for(int i = 0;i < w;i++)if(b[i])ng();
    int k;
    cin >> k;
    for(int i = 0;i < k;i++){
        int x, y;
        cin >> x >> y;x--,y--;
        ans[x][y] = 'x';
        if(edge[y+h][x]){
            edge[y+h][x] = false;
            a[x]++;
            b[y]++;
        }
        else{
            edge[x][y+h] = false;
        }
    }

    for(int i = 0;i < h;i++){
        while(a[i]){
            fill(come, come + 4400, false);
            if(f(i))a[i]--;
            else ng();
        }
    }
    
    cout << "Yay!" << endl;
    for(int i = 0;i < h;i++){
        int cnt = 0;
        for(int j = 0;j < w;j++){
            if(edge[j+h][i]){
                ans[i][j] = 'o';
                cnt++;
            }            
        }

        cout << ans[i] << endl;
    }
    return 0;
}
0