CF 1371D Grid-00100

题目:ios

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

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.spa

Let's define:code

  • 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.blog

 

思路:咱们容易想到让max尽量的小,min尽量的大。经过画图把1合理分配,容易获得一个画图的方式,每次都画右偏的对角线便可,若是超出数组则对应左边标记1便可。ci

 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 }
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息