题目链接:传送门

题目大意:有一个 3*n 的矩阵,每个格子里有一个数,你可以选择相邻的两个格子合并成一个,并且权值变为两数乘积,若一个数未合并,权值变为0,求最后权值总和最大值。

题目思路:以 2^3 状态压缩。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 2000005
#define maxn 100005
typedef pair<int,int> PII;
typedef long long LL; int n,m,cnt,ans;
int a[][];
int dp[][];
int deal(int x,int u,int e){
for(int i=;i<;++i)if((u&(<<i))&&(e&(<<i)))return ;
int temp=;
for(int i=;i<;++i){
if(e&(<<i)){
temp+=a[x-][i+]*a[x][i+];
}
}
int t1=,t2=;
e|=u;
if((!(e&))&&(!(e&)))t1=a[x][]*a[x][];
if((!(e&))&&(!(e&)))t2=a[x][]*a[x][];
temp+=max(t1,t2);
return temp;
}
int main(){
int i,j,Case=;
while(scanf("%d",&n)!=EOF&&n){
mst(dp,);
for(i=;i<=;++i)for(j=;j<=n;++j)scanf("%d",&a[j][i]);
for(i=;i<=n;++i){
for(j=;j<=;++j)
for(int k=;k<=;++k){
int temp=deal(i,j,k);
dp[i][j]=max(dp[i][j],dp[i-][k]+temp);
}
}
ans=;
for(i=;i<=;++i) ans=max(ans,dp[n][i]);
printf("Case %d: %d\n",++Case,ans);
}
return ;
}

UVALive 6560 The Urge to Merge的更多相关文章

  1. UVAlive 6560 - The Urge to Merge(状压dp)

    LA 6560 - The Urge to Merge option=com_onlinejudge&Itemid=8&page=show_problem&problem=45 ...

  2. 状压DP uvalive 6560

    // 状压DP uvalive 6560 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值 // 思路: // dp[i][j]:第i行j状态下的值 ...

  3. 2014 UESTC 暑前集训队内赛(2) 部分解题报告

    B.Cuckoo for Hashing 模拟题. 代码: #include <iostream> #include <cstdio> #include <cstring ...

  4. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并

    题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...

  6. 逆序数 UVALive 6508 Permutation Graphs

    题目传送门 /* 题意:给了两行的数字,相同的数字连线,问中间交点的个数 逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数 用归并排序或线段树求.想到了就简单了 ...

  7. 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR

    ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...

  8. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  9. SQL 提示介绍 hash/merge/concat union

    查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...

随机推荐

  1. iOS 滑块拼图游戏(Puzzle8)

    代码地址如下:http://www.demodashi.com/demo/11505.html 一.准备工作 先了解一个定义和定理 定义:在一个1,2,...,n的排列中,如果一对数的前后位置与大小顺 ...

  2. Xilinx资源优化问题

    资源优化问题:Xilinx ise 出现资源不够的问题(ERROR:Cpld:868 - Cannot fit the design into any of the specified devices ...

  3. ios的设计原则

    ios的设计原则 iOS设计的详细的主要宗旨例如以下: 1.易操作,所关心的主题清新 2.UI控件布局合理,图片质量清新 3.用户的使用习惯 4.字体的大小,主次分明 5.舒适的动画效果 在创建一个新 ...

  4. redis命令_ZREVRANGEBYSCORE

    ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] 返回有序集 key 中, score 值介于 max 和 min 之间(默 ...

  5. php 36进制与10进制转换

    php 36进制与10进制转换 /** * @desc im:十进制数转换成三十六机制数 * @param (int)$num 十进制数 * return 返回:三十六进制数 */ function ...

  6. KVC之-setValue:forKey:方法实现原理与验证

    KVC之-setValue:forKey:方法实现原理与验证 - (void)setValue:(id)value forKey:(NSString *)key方法,实现原理与验证 功能:使用一个字符 ...

  7. django的html模板中获取字典的值

    在django的html模板中获取字典中的值应当直接使用 字典.[key] 的方式 {% for i in lists %} <li id="{{i.id}}" class ...

  8. C# MessageBox 消息对话框

    在程序中,我们经常使用消息对话框给用户一定的信息提示,如在操作过程中遇到错误或程序异常,经常会使用这种方式给用于以提示.在C#中,MessageBox消息对话框位于System.Windows.For ...

  9. NumberUtils

    package cn.edu.hbcf.common.utils; import java.math.BigDecimal; import java.text.NumberFormat; import ...

  10. 猫猫学iOS 之微博项目实战(5)微博自己定义搜索框searchBar

    猫猫分享.必须精品 原创文章.欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 用UITextField简单定义一个搜索框 二:调用 ...