链接:

https://codeforces.com/contest/1209/problem/E2

题意:

This is a harder version of the problem. The difference is only in constraints.

You are given a rectangular n×m matrix a. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times.

After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for i-th row it is equal ri. What is the maximal possible value of r1+r2+…+rn?

思路:

考虑状压DP, Dp[i][j], 表示已经对i列操作, 同时行状态为j的最大值.

枚举每列, 枚举每列的选行状态, 跟之前不冲突的状态比较.

枚举结束后转移状态,同时考虑可以选列最大值最大的n个列, 减少范围.

DP还是难啊

代码:

#include <bits/stdc++.h>
using namespace std; int Dp[(1<<12)+10], Tmp1[(1<<12)+10], Tmp2[(1<<12)+10];
int a[20][2010], Id[2010], Val[2010];
int n, m; bool Cmp(int l, int r)
{
if (Val[l] > Val[r])
return true;
return false;
} int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(Val, 0, sizeof(Val));
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%d", &a[i][j]);
Id[j] = j;
Val[j] = max(Val[j], a[i][j]);
}
}
sort(Id + 1, Id + 1 + m, Cmp);
m = min(n, m);
memset(Dp, 0, sizeof(Dp));
for (int i = 1; i <= m; i++)
{
memcpy(Tmp2, Dp, sizeof(Dp));
memset(Tmp1, 0, sizeof(Tmp1));
for (int ti = 0; ti < n; ti++)
{
for (int s = 0; s < (1 << n); s++)
Tmp2[s] = Dp[s];
for (int s = 0; s < (1 << n); s++)
{
for (int li = 0; li < n; li++)
{
if (!((1 << li) & s))
Tmp2[s | (1 << li)] = max(Tmp2[s | (1 << li)], Tmp2[s] + a[(ti + li) % n][Id[i]]);
}
}
for (int s = 0; s < (1 << n); s++)
Tmp1[s] = max(Tmp1[s], Tmp2[s]);
}
// for (int s = 0;s < (1 << n); s++)
// cout << Tmp1[s] << ' ' ;
// cout << endl;
for (int s = 0;s < (1 << n); s++)
Dp[s] = Tmp1[s];
}
printf("%d\n", Dp[(1<<n)-1]);
} return 0;
}

最新文章

  1. ASP.NET MVC——CodeFirst开发模式
  2. Mac 下面 apache 不解析PHP(or PHP 版本不对)的解决办法
  3. .NET魔法堂:工程构建基石-&gt;MSBuild
  4. C语言中常量
  5. Gedit中文乱码
  6. Nmap备忘单:从探索到漏洞利用 Part1
  7. ASP.NET MVC系列 框架搭建(二)之仓储层的优化
  8. js里的匿名函数 数组排序
  9. # 20145210 《Java程序设计》第06周学习总结
  10. Android注解利器:ButterKnife 的基本使用
  11. js 常用插件
  12. params SqlParameter[] commandParameters(转)
  13. Machine Learning|Andrew Ng|Coursera 吴恩达机器学习笔记
  14. CRM客户关系管理系统(十)
  15. Index API
  16. centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named &#39;_ctypes&#39;
  17. spring Cache注解详解
  18. JS Math方法
  19. nodejs故障cnpm没反应
  20. Eclipse neon 4.6 安装tomcat

热门文章

  1. C++:标准模板库vector
  2. Can you answer these queries III
  3. ajax与jsonp中的几个封装函数
  4. PostgreSql-psql命令的使用
  5. python笔记005-字符串-列表-元组
  6. hdu 6205 card card card 尺取法
  7. (十四)Activitivi5之个人任务分配
  8. (七)mybatis之多对一关系(复杂)
  9. 验证码识别的免费 OCR
  10. C# Combox控件绑定自定义数据