QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#332280#6309. AqreScanoWA 0ms3868kbC++203.5kb2024-02-19 13:14:452024-02-19 13:14:46

Judging History

你现在查看的是最新测评结果

  • [2024-02-19 13:14:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3868kb
  • [2024-02-19 13:14:45]
  • 提交

answer

#include <bits/stdc++.h>

#define sz(v) v.size()
#define forn(i,n) for(int i = 0; i < n; ++i)
#define fi first
#define se second
#define el '\n'
#define all(v) v.begin(),v.end()
#define pb push_back
#define d(x) cout << #x << " : " << x << el

using namespace std;

typedef pair<int,int> ii;

int dr[] = {1,0,-1,0};
int dc[] = {0,1,0,-1};

bool check(int i, int j, int n, int m){
  return i >= 0 && j >= 0 && i < n && j < m;
}

int bfs(vector<vector<int>> &a){
  int n = sz(a);
  int m = sz(a[0]);
  queue<ii> q;
  int ans = 0;
  vector<vector<bool>> seen(n, vector<bool>(m));
  forn(i,n){
    forn(j,m){
      if(a[i][j]==1){
        q.push({i,j});
        while(sz(q)){
          ii cur = q.front();
          q.pop();
          ++ans;
          forn(k,4){
            int ni = i + dr[k];
            int nj = j + dc[k];
            if(check(ni,nj,n,m) && !seen[ni][nj] && a[ni][nj]){
              q.push({ni,nj});
              seen[ni][nj] = 1;
            }
          }
        }
        break;
      }
    }
  }
  return ans;
}

int contar(vector<vector<int>> &a){
  int ans = 0;
  for(vector<int> &b : a){
    for(int x : b){
      ans += x;
    }
  }
  return ans;
}

vector<vector<int>> solve(int n, int m){
  vector<vector<int>> g(n, vector<int>(m));
  
  if(n<=3){
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < m; ++j){
        if(i%2 == 0){
          g[i][j] = (j%4!=3);
        }else if(i == 1){
          g[i][j] = (j%4!=1);
        }
      }
    }
    return g;
  }
  if(n%4){
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < m; ++j){
        if(i%4==0){
          g[i][j] = (j%4!=3);
        }else if(i%4==1){
          g[i][j] = (j%4!=2);
        }else if(i%4==2){
          g[i][j] = (j%4!=0);
        }else if(i%4==3){
          g[i][j] = (j%4!=1);
        }
      }
    }
    if(bfs(g) != contar(g)){
      g = vector<vector<int>>(n, vector<int>(m));
    }
  }
  vector<vector<int>> g2(n, vector<int>(m));
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < m; ++j){
      if(i%4==0){
        g2[i][j] = (j%4!=3);
      }else if(i%4==1){
        g2[i][j] = (j%4!=1);
      }else if(i%4==2){
        g2[i][j] = (j%4!=2);
      }else if(i%4==3){
        g2[i][j] = (j%4!=0);
      }
    }
  }
  if(contar(g) < contar(g2)) return g2;
  return g;
}

vector<vector<int>> transpose(vector<vector<int>> &a){
  
  int n = sz(a);
  int m = sz(a[0]);
  vector<vector<int>> b(m, vector<int>(n));
  forn(i,n){
    forn(j,m){
      b[j][i] = a[i][j];
    }
  }
  return b;
}



int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  cout << setprecision(20)<< fixed;

  int t;
  cin >> t;
  while(t--){
    int n, m;
    cin >> n >> m;
    if(n <= 3 && m <= 3){
      cout << n * m << el;
      forn(i,n){
        forn(j,m){
          cout << 1;
        }
        cout << el;
      }
      continue;
    }
    vector<vector<int>> ans;
    if(n >= 4 && m>= 4){
      ans = solve(n, m);
      auto ans2 = solve(m, n);
      ans2 = transpose(ans2);
      if(contar(ans) < contar(ans2)) ans = ans2;
    }else{
      if(n > m){
        ans = solve(m, n);
        ans = transpose(ans);
      }else{
        ans = solve(n, m);
      }
    }
    int to = 0;
    forn(i,n){
      forn(j,m){
        to += ans[i][j];
      }
    }
    cout << to << el;
    forn(i,n){
      forn(j,m){
        cout << ans[i][j];
      }
      cout << el;
    }
    
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3768kb

input:

3
2 2
3 4
3 8

output:

4
11
11
9
1110
1011
1110
18
11101110
10111011
11101110

result:

ok ok (3 test cases)

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3868kb

input:

361
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
2 19
2 20
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
3 14
3 15
3 16
3 17
3 18
3 19
3 20
4 2
4 3
4 4
4 5
4 6
4 7
4 8
4 9
4 10
4 11
4 12
4 13
4 14
4 15
4 16
4 17
4 18
4 19
4 20
5 2
5 3
5 4
5 5
5 6
5 7
5 8
5 9
5 1...

output:

4
11
11
6
111
111
6
1110
1011
8
11101
10111
9
111011
101110
11
1110111
1011101
12
11101110
10111011
14
111011101
101110111
15
1110111011
1011101110
17
11101110111
10111011101
18
111011101110
101110111011
20
1110111011101
1011101110111
21
11101110111011
10111011101110
23
111011101110111
1011101110111...

result:

wrong answer 1s are not connected. (test case 61)