状压DP--Rotate Columns (hard version)-- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
题意:https://codeforc.es/problemset/problem/1209/E2
给你一个n(1-12)行m(1-2000)列的矩阵,每一列都可以上下循环移动(类似密码锁)。
问你移动后,对每一行取最大值,这些最大值再加起来的MAX是多少。
思路:
1. 首先我们有一个思维上的优化,就是我们已知n很小,m会很大。我们按照每列最大的元素值为排序标准对列进行排序。
我们发现最多只会用到前n列(就是全取每列的最大值)。
2. 枚举每列对行生效的数(就是该行这个数最大),也就是有2^n种情况,DP记录的是之前所有列对于2^n种情况的最有解,更新时对于每一种情况枚举子集并更新,
就是找新加列的互补状态的值(取max)。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int a[][];
struct node
{
int id,max_;
friend bool operator<(node a,node b)
{
return a.max_>b.max_;
}
}key[];
int dp[],DP[]; void solve()
{
int n,m;
sc("%d%d",&n,&m);
for(int i=;i<=m;++i)
key[i]={i,};
for(int i=;i<=n;++i)
for(int j=;j<=m;++j)
sc("%d",&a[i][j]),key[j].max_=max(key[j].max_,a[i][j]);
sort(key+,key++m);
m=minn(m,n);
int top=(<<n)-;
for(int i=;i<=top;++i)DP[i]=;
for(int o=;o<=m;++o)
{
for(int i=;i<=top;++i)dp[i]=;
for(int i=;i<=top;++i)
{
int sum=;
for(int pos=;pos<n;++pos)
if((i>>pos)&)
sum+=a[pos+][key[o].id];
for(int j=,now=i;j<=n;++j)
{
dp[now]=max(dp[now],sum);
if(now&)now+=<<n;
now>>=;
}
}
for(int i=top;i>=;--i)//注意要从大到小开始更新,因为大的要用到小的;
{
for(int k=i;k;k=(k-)&i)
DP[i]=max(DP[i],DP[i-k]+dp[k]);
}
}
pr("%d\n",DP[top]);
} int main()
{
// cout<<(1<<12)<<endl;
int T;
sc("%d",&T);
while(T--)solve();
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
状压DP--Rotate Columns (hard version)-- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)的更多相关文章
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2) G1. Into Blocks (easy version)
题目:https://codeforc.es/contest/1209/problem/G1 题意:给你一个序列,要你进行一些操作后把他变成一个好序列,好序列的定义是,两个相同的数中间的数都要与他相同 ...
- Cow and Snacks(吃点心--图论转换) Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
题意:https://codeforc.es/contest/1209/problem/D 有n个点心,有k个人,每个人都有喜欢的两个点心,现在给他们排个队,一个一个吃,每个人只要有自己喜欢的点心就会 ...
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)C
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;string s;pair<int,in ...
- 状压dp找寻环的个数 Codeforces Beta Round #11 D
http://codeforces.com/problemset/problem/11/D 题目大意:给你n个点,m条边,找该图中有几个换 思路:定义dp[i][j]表示i是圈的集合,j表示该集合的终 ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces Round #584 E2. Rotate Columns (hard version)
链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
随机推荐
- Django基础之template
1. 模板系统的介绍 def current_datetime(request): now = datetime.datetime.now() html = "<html>< ...
- Java线程之创建线程
翻译自:https://www.journaldev.com/1016/java-thread-example 进程 进程是一个自包含的执行环境,它可以被看成一个程序或应用程序.然而一个应用程序本身包 ...
- [CTS2019]珍珠——二项式反演
[CTS2019]珍珠 考虑实际上,统计多少种染色方案,使得出现次数为奇数的颜色数<=n-2*m 其实看起来很像生成函数了 n很大?感觉生成函数会比较整齐,考虑生成函数能否把n放到数值的位置,而 ...
- java随机生成6位随机数 5位随机数 4位随机数
随机数,应用会相当广,验证数,订单号,流水号拼接. 下面是java随机数生成语句: 生成6位随机数(不会是5位或者7位,仅只有6位): System.+)*)); 同理,生成5位随机数: System ...
- python 查看源代码
使用包inspect import inspect from mxnet import contrib 查看模块所在路径: inspect.getsourcefile(contrib) 查看源码: i ...
- oracle存储过程中进行分页
create or replace procedure APP_BUSSINESS_CARD_LIST(p_in_str in varchar2,p_out_str out varchar2) is ...
- Process.Net
ProcessSharp的构造函数,对应的测试是 https://github.com/lolp1/Process.NET/blob/master/test/Process.NET.Test/Core ...
- mybatis之动态SQL操作之插入
1) 根据条件,插入一个学生 /** * 持久层*/ public class StudentDao { /** * 动态SQL--插入 */ public void dynaSQLwithInse ...
- Hibernate3核心API-Session接口
Session是应用程序与数据库之间交互操作的一个单线程对象,是 Hibernate 运作的中心,所有持久化对象必须在 session 的管理下才可以进行持久化操作.此对象的生命周期很短.Sessio ...
- 基于Python使用scrapy-redis框架实现分布式爬虫
1.首先介绍一下:scrapy-redis框架 scrapy-redis:一个三方的基于redis的分布式爬虫框架,配合scrapy使用,让爬虫具有了分布式爬取的功能.github地址: https: ...