题目:

A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

You are given integers n,k. Construct a grid AA with size n×n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k.

Let's define:

  • Ai,j as the integer in the i-th row and the j-th column.
  • Ri=Ai,1+Ai,2+...+Ai,n (for all 1≤i≤n).
  • Cj=A1,j+A2,j+...+An,j (for all 1≤j≤n).
  • In other words, RiRi are row sums and CjCj are column sums of the grid AA.
  • For the grid A let's define the value f(A)=(max(R)−min(R))^2+(max(C)−min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X).

Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any.

 

思路:我们容易想到让max尽可能的小,min尽可能的大。通过画图把1合理分配,容易得到一个画图的方式,每次都画右偏的对角线即可,如果超出数组则对应左边标记1即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <string>
 6 #include <vector>
 7 #include <cmath>
 8  
 9 using namespace std;
10  
11 #define ll long long
12 #define pb push_back
13 #define fi first
14 #define se second
15  
16 const int N = 2e5 + 10;
17 int a[310][310];
18  
19 void solve()
20 {      
21     int T;
22     cin >> T;
23     while(T--){
24         int n, k;
25         cin >> n >> k;
26  
27         for(int i = 1; i <= n; ++i){
28             for(int j = 1; j <= n; ++j)
29                 a[i][j] = 0;
30         }
31 
32         //画图
33         int d = 0;
34         while(1){
35             for(int i = 1; i <= n; ++i){
36                 if(k == 0) break;
37                 a[i][(i + d) == n ? n : (i + d) % n] = 1;
38                 k--;
39             }
40             if(k == 0) break;
41             d++;
42         }
43  
44         int maxr, minr, maxc, minc;
45         maxr = maxc = -1;
46         minr = minc = 1e9;
47         for(int i = 1; i <= n; ++i){
48             int cnt = 0;
49             for(int j = 1; j <= n; ++j){
50                 if(a[i][j]) cnt++;
51             }
52             maxr = max(maxr, cnt);
53             minr = min(minr, cnt);
54         }
55  
56         for(int j = 1; j <= n; ++j){
57             int cnt = 0;
58             for(int i = 1; i <= n; ++i){
59                 if(a[i][j]) cnt++;
60             }
61             maxc = max(maxc, cnt);
62             minc = min(minc, cnt);
63         }
64         //cout << "min_ans = ";
65         cout << (maxr - minr) * (maxr - minr) + (maxc - minc) * (maxc - minc) << endl;
66         for(int i = 1; i <= n; ++i){
67             for(int j = 1; j <= n; ++j){
68                 cout << a[i][j];
69             }
70             cout << endl;
71         }
72     }
73 }
74  
75 int main()
76 {
77     ios::sync_with_stdio(false);
78     cin.tie(0);
79     cout.tie(0); 
80     solve();
81  
82     return 0;
83 }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄