F Partition problem
题意
有2*n个人,平均分为两队,当i和j不在同一队时,获得权值 V i j V_{ij} Vij,求权值总和的最大值
思路
暴力!
暴力!
暴力!
首先划分28个人有
C
28
14
C_{28}^{14}
C2814种情况,约为4e7,然后计算权值总和本来需要14*14的复杂度,这样肯定会T,因此需要想办法优化这个14*14,于是我在翻别人的代码的时候看到了这一段超简洁的dfs。。。。。。
#include<bits/stdc++.h>
#include<stdlib.h>
#include<string>
using namespace std;
typedef long long int ll;
const int maxn = 40;
const ll mod=1e9+7;
const int N=63;
const ll inf=0x7fffffffff;
ll c1[maxn][maxn];
ll c2[maxn];
vector<ll> c;
ll ans=0;
int n;
void dfs(int x,ll sum)
{
if(c.size()==n)
{
ans=max(ans,sum);
return ;
}
if(x>2*n)
return;
ll temp=c2[x];
for(int i=0;i<c.size();i++)
{
temp-=2*c1[x][c[i]]; //这边一定要减掉两倍,因为在c2[1]已经多算了一次
}
c.push_back(x);
dfs(x+1,sum+temp); //x与1同一队
c.pop_back();
dfs(x+1,sum); //x与1不同队
}
int main()
{
cin>>n;
for(int i=1;i<=2*n;i++)
{
for(int j=1;j<=2*n;j++)
{
cin>>c1[i][j];
c2[i]+=c1[i][j];
}
}
c.push_back(1);
dfs(2,c2[1]);
printf("%lld\n",ans);
}
H Second Large Rectangle
题意
给定由01组成的矩阵,求其中面积第二大的矩形大小
思路
通过单调栈求最大矩形,再找第二大矩形
#include<bits/stdc++.h>
#include<stdlib.h>
#include<string>
using namespace std;
typedef long long int ll;
const int maxn = 1e3+10;
const ll mod=1e9+7;
const ll inf=0x7fffffffff;
int max1,max2;
int s[maxn][maxn];
void cala(int a){
if(a>max1)
{
max2=max1;
max1=a;
}
else if(a>max2)
{
max2=a;
}
}
void cal(int x,int y)
{
cala(x*y);
cala((x-1)*y);
cala(x*(y-1));
}
int main()
{
int n,m;
char t[maxn];
while(cin>>n>>m)
{
max1=max2=0;
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++)
{
cin>>t;
for(int j=1;j<=m;j++)
{
s[i][j]=t[j-1]-'0';
if(s[i][j]==1)
s[i][j]=s[i-1][j]+1;
}
}
stack<int> st;
for(int i=1;i<=n;i++)
{
st.push(0);
s[i][0]=-2;
s[i][m+1]=-1;
for(int j=1;j<=m+1;j++)
{
while(s[i][st.top()]>s[i][j])
{
int k=st.top();
st.pop();
cal(j-1-st.top(),s[i][k]); //cal(((j-1)-(st.top()+1)+1),s[i][k]);
}
st.push(j);
}
}
printf("%d\n",max2);
}
}