Rotate Columns (easy version) CodeForces - 1209E1

Rotate Columns (easy version) 

题意:

每一列的数字能够进行循环位移,问每一行最大值的和最大是多少

思路:

数据量较小,暴力模拟出每一种情况,然后取最大值即可

一开始想用贪心的思路去做,每次取总体增量最大

然后发现有一种情况会导致错误

第一种:

6 7

6 7

6 7

第二种:

6 6

6 6

6 9

虽然两种增量相同,但是应该是下面这种情况更优,所以错了很久,一开始有想到这种情况,但是脑子转不过来,弄了很久才灵光一闪想到这个样例

  1 #include<cstdio>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<iostream>
  6 #include<vector>
  7 #include<queue>
  8 #include<set>
  9 #include<map>
 10 #include<cctype>
 11 #define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
 12 #define mem(a,x) memset(a,x,sizeof(a))
 13 #define lson rt<<1,l,mid
 14 #define rson rt<<1|1,mid + 1,r
 15 #define P pair<int,int>
 16 #define ull unsigned long long
 17 using namespace std;
 18 typedef long long ll;
 19 const int maxn = 1e6 + 10;
 20 const ll mod = 1000000007;
 21 const int inf = 0x3f3f3f3f;
 22 const long long INF = 0x3f3f3f3f3f3f3f3f;
 23 const double eps = 1e-7;
 24 
 25 inline ll read()
 26 {
 27     ll X = 0, w = 0; char ch = 0;
 28     while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
 29     while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
 30     return w ? -X : X;
 31 }
 32 
 33 int n , m , N;
 34 int mp[10][200];
 35 int ans;
 36 struct node
 37 {
 38     int id, maxx;
 39 }res[200];
 40 
 41 int cmp(node& a, node& b)
 42 {
 43     return a.maxx > b.maxx;
 44 }
 45 
 46 
 47 void dfs(int pos)
 48 {
 49     if (pos >= N + 1)
 50     {
 51         int sum = 0;
 52         for (int i = 1; i <= n; ++i)
 53         {
 54             int tmp = 0;
 55             for (int j = 1; j <= N; ++j)
 56             {
 57                 tmp = max(tmp, mp[i][res[j].id]);
 58             }
 59             sum += tmp;
 60         }
 61         ans = max(ans, sum);
 62     }
 63     else
 64     {
 65         int tmp[10];
 66         for (int i = 1; i <= n; ++i)
 67         {
 68             for (int j = 1; j <= n; ++j)
 69             {
 70                 tmp[j] = mp[j % n + 1][res[pos].id];
 71             }
 72             for (int j = 1; j <= n; ++j)
 73             {
 74                 mp[j][res[pos].id] = tmp[j];
 75             }
 76             dfs(pos + 1);
 77         }
 78     }
 79 }
 80 
 81 
 82 void init()
 83 {
 84     N = 0;
 85     mem(res, 0);
 86     ans = 0;
 87 }
 88 
 89 int main() 
 90 {
 91     int T;
 92     T = read();
 93     while (T--)
 94     {
 95         init();
 96         n = read(), m = read();
 97         for (int i = 1; i <= n; ++i)
 98         {
 99             for (int j = 1; j <= m; ++j)
100             {
101                 mp[i][j] = read();
102                 res[j].id = j;
103                 res[j].maxx = max(res[j].maxx, mp[i][j]);
104             }
105         }
106         sort(res + 1, res + 1 + m, cmp);
107         N = min(n, m);
108         dfs(1);
109         cout << ans << endl;
110     }
111     return 0;
112 }
AC代码

 

posted @ 2020-04-15 20:18  当然是斗笠呀  阅读(206)  评论(0编辑  收藏  举报