时间限制:0.75s

空间限制:65M

题意:

在一个n*m(n,m<=15)的网格中,每个格子有一个值,现在从网格中取出k(k<=n*m)个,保证在选中的格子中从任意一个格子去另外的所有格子最多只用到四种(上,下,左,右)操作中的两种,并使得到的值最大。输出该值和选中的格子坐标。


Solution

    从选中的网格的任意一个去所有格子只用两种操作:

左图一种满足条件的网格集合,右图的(1,3)到(3,2)必需使用(左,下,右)三种操作才能到达,是不符合要求的。

注意到,满足条件的网格集合一定不会出现凹形,因为凹形的上下两个突出点中的格子不可能只用两个操作到达。

由此用0,1记录左右两边是 向外突出还是向内收缩。

当一行的一边向内收缩时便不可以再向外突出了。

接下来是按行dp

f[row][l][r][opl][opr][k],代表第row行,取第l列带第r列的格子,左边格子突出状态为opl,右边为opr,当前共选择了k个格子

记录路径时如果用结构体记录下所有的状态的上一个状态需要15*15*2*2*225 * size(int)*6的空间.

爆一次内存后,想到对于上一个状态row和k可以推出,只需要记录(l,r,opl,opr)即可,而(l,r,opl,opr)都是不超过16的数,那么可以用一个int形用16进制存下

存的时候用 record=(l << 12) + (r << 8) + (opl << 4) + (opr)

用的时候

上一个(row,l,r,opl,opr,k)

可以由当前的(row - 1, (record >> 12) % 16, (record >> 8) % 16, (record >> 4) % 16, record % 16, k - r + l - 1)得到

这道题需要注意的地方有很多,不一一列举.总的来说是一道值得一做的好题

参考代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#define pdx pr[row][l][r][opl][opr][k]
#define val(r,a,b) g[r][b] - g[r][a - 1]
using namespace std;
int f[16][16][16][2][2][230], g[16][16];
int pr[16][16][16][2][2][230];
int n, m, k, ans;
int a, al, ar, aol, aor;
/*
op=0 / 左突
op=1 \ 右突
*/
//当前状态向下更新
void update (int row, int l, int r, int opl, int opr, int s) {
if (row == n) return ;
for (int nl = (opl == 0 ? 1 : l); nl <= r; nl++)
for (int nr = max (l, nl); nr <= (opr == 0 ? r : m); nr++) {
int t1, t2;
if (nl == l) t1 = opl;
else
t1 = (nl < l ? 0 : 1);
if (nr == r) t2 = opr;
else
t2 = (nr < r ? 0 : 1);
if (f[row + 1][nl][nr][t1][t2][s + (nr - nl + 1)] <
f[row][l][r][opl][opr][s] + val (row + 1, nl, nr) ) {
f[row + 1][nl][nr][t1][t2][s + (nr - nl + 1)] =
f[row][l][r][opl][opr][s] + val (row + 1, nl, nr);
//记录上一个状态 , 16进制状态压缩
pr[row + 1][nl][nr][t1][t2][s + (nr - nl + 1)] =
(l << 12) + (r << 8) + (opl << 4) + (opr);
}
}
} void dp() {
for (int row = 1; row <= n; row++)
for (int l = 1; l <= m; l++)
for (int r = l; r <= m; r++)
for (int opl = 0; opl < 2; opl++)
for (int opr = 0; opr < 2; opr++) {
f[row][l][r][opl][opr][r - l + 1] = g[row][r] - g[row][l - 1];
for (int s = r - l + 1; s <= k; s++)
if (f[row][l][r][opl][opr][s] > 0 ) {
update (row, l, r, opl, opr, s);
if (s == k)//记录最大解
if (f[row][l][r][opl][opr][s] > ans) {
ans = f[row][l][r][opl][opr][s];
a = row, al = l, ar = r, aol = opl, aor = opr;
}
}
}
}
//输出
void write (int row, int l, int r, int opl, int opr, int k) {
if (row == 0 || k <= 0) return;
for (int i = l; i <= r; i++)
printf ("%d %d\n", row, i);
write (row - 1, (pdx >> 12) % 16, (pdx >> 8) % 16,
(pdx >> 4) % 16, pdx % 16, k - r + l - 1);
}
int main() {
#ifndef ONLINE_JUDGE
freopen ("in.txt", "r", stdin);
#endif // oline_judge
scanf ("%d %d %d", &n, &m, &k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf ("%d", &g[i][j]);
g[i][j] += g[i][j - 1];
}
memset (f, -1, sizeof f);
dp();
printf ("Oil : %d\n", ans);
write (a, al, ar, aol, aor, k);
return 0;
}

  

  

SGU 167.I-country的更多相关文章

  1. SGU 520 Fire in the Country(博弈+搜索)

    Description This summer's heat wave and drought unleashed devastating wildfires all across the Earth ...

  2. 新概念英语(1-67)The weekend

    新概念英语(1-67)The weekend What are the Johnsons going to do at the weekend? A:Hello. Were you at the bu ...

  3. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  4. wifi的country code

    转自:http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.htmlCountry A 2 A 3 Number ------------- ...

  5. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  6. ural 1073. Square Country

    1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square ...

  7. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  8. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

  9. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

随机推荐

  1. u-boot中分区和内核MTD分区关系

    一.u-boot中环境变量与uImage中MTD的分区关系 分区只是内核的概念,就是说A-B地址放内核,C-D地址放文件系统,(也就是规定哪个地址区间放内核或者文件系统)等等. 一般我们只需要分3-4 ...

  2. 高效的TCP消息发送组件

    目前的.net 架构下缺乏高效的TCP消息发送组件,而这种组件是构建高性能分布式应用所必需的.为此我结合多年的底层开发经验开发了一个.net 下的高效TCP消息发送组件.这个组件在异步发送时可以达到每 ...

  3. [转]33 useful Keyboard Shortcuts for Run commond

    原文: http://www.shortcutworld.com/en/win/Run-command.html 1. Calling Run CommandWin + r               ...

  4. 6N137的使用

    (1)引脚图 (2)功能表 (3)内部结构图 信号从2.3脚输入,反向偏置的光敏二极管受光照后导通,经过电流电压转换,输入到与门一端,与门另一端为使能端.由于输入信号为集电极开路,需要加上拉电阻.当使 ...

  5. HDOJ 1716 排列2(next_permutation函数)

    Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...

  6. 《图解密码技术》-chaper1-概述

    密码和信息安全常识:  (1)不要使用保密的密码算法.  (2)不要使用低强度密码算法.  (3)密码一定会被破解.  (4)密码只是信息安全的一部分.

  7. CHS与LBA之间转换程序

    原理及介绍来自维基百科 http://zh.wikipedia.org/wiki/%E9%82%8F%E8%BC%AF%E5%8D%80%E5%A1%8A%E4%BD%8D%E5%9D%80 个人用的 ...

  8. 【设计模式 - 2】之单例模式(Singleton)

    1      模式简介 单例模式在代码中是非常常用的,如线程池.数据库连接.注册表.共享资源.一些非常消耗资源的组件,等等. 单例模式主要解决如下问题: 确保一个特殊类的实例是独一无二的: 确保这个类 ...

  9. 【Android - 框架】之Retrofit的使用

    Retrofit是Square公司发布的一个可以应用在Android和Java中的Http客户端访问框架,其底层应用的是OkHttp. 在这个帖子中,我们以下面这个Http请求为例: https:// ...

  10. spring项目中监听器作用-ContextLoaderListener(转)

    1 spring框架的启动入口 ContextLoaderListener 2 作用:在启动Web 容器时,自动装配Spring applicationContext.xml 的配置信息. 因为它实现 ...