状压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 ...
随机推荐
- sql语句中where 1=1和 0=1 的作用
sql where 1=1和 0=1 的作用 where 1=1; 这个条件始终为True,在不定数量查询条件情况下,1=1可以很方便的规范语句. 一.不用where 1=1 在多条件查询中的 ...
- python中的break continue用法
Break break跳出循环,并且终止最小封闭循环. Continue continue跳过本次循环,继续执行下一次的循环. 二者区别就是break会终止循环,continue不终止循环.
- 4 LinkedList
1 LinkedList public class LinkedList<E> extends AbstractSequentialList<E> implements Lis ...
- ElasticSearch1:基本概念
ElasticSearch的基本概念 es基本概念: Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档,用JSON作为文档序列化的格式 NRT:Nearly Real Time ...
- HearthBuddy版本收集
Hearthbuddy-20190811-010-0b563c92.exe 20190810-003 SHA-256: b2a03c10124b038d2c48279cc50947907a55c8 ...
- Flutter移动电商实战 --(12)首页导航区域编写
1.导航单元素的编写 从外部看,导航是一个GridView部件,但是每一个导航又是一个上下关系的Column.小伙伴们都知道Flutter有多层嵌套的问题,如果我们都写在一个组件里,那势必造成嵌套严重 ...
- Kbengine游戏引擎-【4】demo-kbengine_unity3d_demo 在容器docker上安装测试
git地址:https://github.com/kbengine/kbengine_unity3d_demo Demo中文地址:https://github.com/kbengine/kbengin ...
- NaN情况下无法比较大小
<pre name="code" class="java">package nan; /** * NaN情况下无法比较大小 * @author ro ...
- easyUI之Pagination(分页)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 006-top查看计算机信息
1.使用top命令基本信息 us:用户态使用的cpu时间比sy:系统态使用的cpu时间比ni:用做nice加权的进程分配的用户态cpu时间比id:空闲的cpu时间比wa:cpu等待磁盘写入完成时间hi ...