题面

钻石游戏(diamond)

问题描述:

一个\(M\)行\(N\)列的棋盘,里面放了\(M \times N\)个各种颜色的钻石。

每一次你可以选择任意两个相邻的颜色不同的钻石,进行交换。两个格子相邻的定义是两个格子有一条公共边。

每次交换的分值为通过这次交换后能够形成的最大矩形的面积,具体请见样例。

跟传统的钻石游戏不太一样的是,交换后钻石不会消除。现在告诉你每一次操作,

请输出每一次所能得到的分值。

问题输入:

 第一行二个整数\(M,N\)。

 接下来\(M\)行\(N\)列,表示第\(I\)行第\(J\)列的钻石的颜色\((1-9)\)。

 第\(M+2\)行有一个正整数\(P\),表示钻石交换的次数。

 接下来\(P\)行,每行四个正整数\(x1,y1,x2,y2\),\(1 \leq x1,x2 \leq M,1 \leq y1,y2 \leq N\),表示交换\((x1,y1)\)和\((x2,y2)\)的钻石。

 保证\((x1,y1),(x2,y2)\)的颜色不相同,并且其必定相邻。

问题输出:

 \(P\)行,输出每次交换得到的分值

题解

这道题目的主要失误在于对数据范围估计错误,其实暴力模拟加小优化后的最劣复杂度为\(\Theta(n^2 \times q)\)

约为\(2500\)万,是可以通过的。

这题的暴力很简单,对于每次交换,向外扩展,然后在从内至外枚举,

然后在枚举内部继续往另一个方向扩展,即可计算矩形面积。

稍加分情况讨论即可,代码稍长,但是基本可以复制。

代码

#include <cstdio>
#include <algorithm> using namespace std; int read(){
int x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
} //define n the row number, m the col number. int n, m;
int _max;
int mp[505][505]; int getL(int x, int y, int val){
int cnt = 0;
for (int i = y; i && mp[x][i] == val && (y - i) <= _max; --i)
++cnt;
return cnt;
} int getR(int x, int y, int val){
int cnt = 0;
for (int i = y; i <= m && mp[x][i] == val && (i - y) <= _max; ++i)
++cnt;
return cnt;
} int getU(int x, int y, int val){
int cnt = 0;
for (int i = x; i && mp[i][y] == val && (x - i) <= _max; --i)
++cnt;
return cnt;
} int getD(int x, int y, int val){
int cnt = 0;
for (int i = x; i <= n && mp[i][y] == val && (i - x) <= _max; ++i)
++cnt;
return cnt;
} int main(){
n = read(), m = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
mp[i][j] = read();
int p = read();
while (p--){
int x1 = read(), y1 = read(), x2 = read(), y2 = read();
if (x1 == x2){
if (y1 > y2)
swap(x1, x2), swap(y1, y2);
}
else if (x1 > x2)
swap(x1, x2), swap(y1, y2);
swap(mp[x1][y1], mp[x2][y2]);
if (x1 == x2){
int ans = 0;
_max = 505; int upper = getL(x1, y1, mp[x1][y1]);
int minu = 505, mind = 505;
for (int i = 1; i <= upper; ++i){
int j = y1 - i + 1;
minu = min(minu, getU(x1, j, mp[x1][y1]));
mind = min(mind, getD(x1, j, mp[x1][y1]));
_max = min(_max, max(minu, mind));
if ((minu + mind - 1) * i > ans)
ans = (minu + mind - 1) * i;
}
minu = 505, mind = 505;
_max = 505, upper = getR(x2, y2, mp[x2][y2]);
for (int i = 1; i <= upper; ++i){
int j = y2 + i - 1;
minu = min(minu, getU(x2, j, mp[x2][y2]));
mind = min(mind, getD(x2, j, mp[x2][y2]));
_max = min(_max, max(minu, mind));
if ((minu + mind - 1) * i > ans)
ans = (minu + mind - 1) * i;
}
printf("%d\n", ans);
}
else{
int ans = 0;
_max = 505; int upper = getU(x1, y1, mp[x1][y1]);
int minl = 505, minr = 505;
for (int i = 1; i <= upper; ++i){
int j = x1 - i + 1;
minl = min(minl, getL(j, y1, mp[x1][y1]));
minr = min(minr, getR(j, y1, mp[x1][y1]));
_max = min(_max, max(minl, minr));
if ((minl + minr - 1) * i > ans)
ans = (minl + minr - 1) * i;
}
minl = 505, minr = 505;
_max = 505, upper = getD(x2, y2, mp[x2][y2]);
for (int i = 1; i <= upper; ++i){
int j = x2 + i - 1;
minl = min(minl, getL(j, y2, mp[x2][y2]));
minr = min(minr, getR(j, y2, mp[x2][y2]));
_max = min(_max, max(minl, minr));
if ((minl + minr - 1) * i > ans)
ans = (minl + minr - 1) * i;
}
printf("%d\n", ans);
}
}
return 0;
}

[HG]钻石游戏diamond 题解的更多相关文章

  1. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  2. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  3. 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

        解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...

  4. java9新特性-8-语法改进:钻石操作符(Diamond Operator)使用升级

    1.使用说明 我们将能够与匿名实现类共同使用钻石操作符(diamond operator) 在java8中如下的操作是会报错的:   编译报错信息:'<>' cannot be used ...

  5. 【ZJ选讲·钻石游戏】

    N×M的棋盘(M,N<=500)中,每个格子有一个颜色(颜色数1~9) P次操作(P<=1000),每次给出两个相邻的位置(保证颜色不同,两个格子有一条公共边),把这两个格子交换. 定 ...

  6. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...

  7. 洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...

  8. 洛谷 P2356 【弹珠游戏】题解

    自我感觉应该没有用结构体做的吧 这道题其实非常水 很适合初学贪心的同学做一下 我好像没有用贪心做,嘻嘻 首先先读题, 题目中说这个游戏只能消灭当前所在位置的行.列的敌人 首先特判一下: if(tt== ...

  9. 【luogu P1558 色板游戏】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1558 我知道三十棵线段树很暴力,可是我们可以状压啊. 颜色最多30,不会爆int 另外 吐槽评测机 #inc ...

随机推荐

  1. 【MM系列】SAP MM模块-货物移动对标准价的影响

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-货物移动对标准价的 ...

  2. iptables规则

    iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分 iptables文件设置路径:命令:vim /etc/sysconfig/iptables-config 0x02 ...

  3. Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.1 Propositional Logic

    propositional variables (or statement variables), letters used for propositional variables are p, q, ...

  4. opencv.js小项目demo

    1.博客连接 https://blog.csdn.net/weixin_38361925/article/details/82528529 2.demo连接 https://github.com/mt ...

  5. 深入理解java:1.3. 垃圾收集

    Java垃圾收集(Garbage Collection,GC) 某一个时点,一个对象如果有一个以上的引用(Rreference)指向它,那么该对象就为活着的(Live), 否则死亡(Dead),视为垃 ...

  6. 20191128 Spring Boot官方文档学习(9.11-9.17)

    9.11.消息传递 Spring Boot提供了许多包含消息传递的启动器.本部分回答了将消息与Spring Boot一起使用所引起的问题. 9.11.1.禁用事务JMS会话 如果您的JMS代理不支持事 ...

  7. [转帖]华为海思Hi1620芯片发布在即 7nm制程ARM架构最高可达3.0GHz

    华为海思Hi1620芯片发布在即 7nm制程ARM架构最高可达3.0GHz https://www.cnbeta.com/articles/tech/850561.htm 中电科旗下的普华软件 支持国 ...

  8. CentOS 7.6 RPM方式安装Oracle19c的过程

    1. 下载需要的安装包: 1.1 preinstall http://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/orac ...

  9. P1079Vigenère密码

    这是2012年noip提高组的的DAY1T1,我用了一下午的时间,一次性AC^^. 这是一个字符串的模拟题.首先给出了一个密码对应法则,我们发现在同一对角线的明文通过密钥得出来的密文是相同的.根据八皇 ...

  10. 【Python】循环结构中的else

    else在循环结构中,只有循环正常结束后才执行else,如果使用break跳出了循环,不会执行else for i in range(0,10): print(i)else: print(" ...