[HG]钻石游戏diamond 题解
题面
钻石游戏(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 题解的更多相关文章
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...
- java9新特性-8-语法改进:钻石操作符(Diamond Operator)使用升级
1.使用说明 我们将能够与匿名实现类共同使用钻石操作符(diamond operator) 在java8中如下的操作是会报错的: 编译报错信息:'<>' cannot be used ...
- 【ZJ选讲·钻石游戏】
N×M的棋盘(M,N<=500)中,每个格子有一个颜色(颜色数1~9) P次操作(P<=1000),每次给出两个相邻的位置(保证颜色不同,两个格子有一条公共边),把这两个格子交换. 定 ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- 洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- 洛谷 P2356 【弹珠游戏】题解
自我感觉应该没有用结构体做的吧 这道题其实非常水 很适合初学贪心的同学做一下 我好像没有用贪心做,嘻嘻 首先先读题, 题目中说这个游戏只能消灭当前所在位置的行.列的敌人 首先特判一下: if(tt== ...
- 【luogu P1558 色板游戏】 题解
题目链接:https://www.luogu.org/problemnew/show/P1558 我知道三十棵线段树很暴力,可是我们可以状压啊. 颜色最多30,不会爆int 另外 吐槽评测机 #inc ...
随机推荐
- suitecrm配置(nginx设置)
suitecrm配置在nginx下的一些设置 server { listen 88; server_name 192.168.2.253; #charset koi8-r; #access_log / ...
- Charles抓包过滤的四种方式
日常测试中,经常要抓包看请求的request,response是不是传的对,返回的字段值对不对,众多的请求中如何找到自己想要的请求,就需要过滤请求,Charles有4种过滤方式,用那一种都可以,看个人 ...
- kafka producer发送消息 Failed to update metadata after问题
提示示例: ERROR Error when sending message to topic test with key: null, value: 2 bytes with error: Fail ...
- 最少多少人说谎(dp)
https://ac.nowcoder.com/acm/contest/1168/H 题意:n个学生,邓志聪想知道这些学生的考试情况,于是一个一个叫这些学生叫去办公室问他们,但是有些学生并没有讲真话, ...
- composer安装thinkphp5
之前安装过composer,里面的一些命令符可以看看,安装tp5我也是按照文档来的,也没什么难度.但是也出现一些问题: 安装tp5: 安装在本地php环境的www目录下,通过命令窗口切换到www目录下 ...
- STL之 stack
栈的常用操作函数:top()push()pop()size()empty() 建栈: stack<int> st; stack<int> st[4]; 四个栈 //可以使用li ...
- mysql两种备份方法总结:mysqldump 和 xtrabackup
mysqldump工具基本使用 1. mysqldump [OPTIONS] database [tables…] 还原时库必须存在,不存在需要手动创建 --all-databases: 备份 ...
- react 错误处理
https://www.jianshu.com/p/61d09e488743 https://codepen.io/sgroff04/pen/dVbgJy/
- git合并丢失代码问题分析与解决(错误操作导致)
问题描述 我们在主干dev和branch1分支上进行并行开发.当要把branch1功能的代码合并到dev上时,发现dev上开发的部分功能代码找不到了. 那么,是在branch1上,作了删除提交导致的吗 ...
- 时间戳转换日期格式 - Vue
日常开发中经常会遇到时间相关的问题,服务端返回的数据都是以时间戳的方式,那么需要将其处理转化为对应的时间格式,具体方式如下: 一.filters 中 formatDate 方法实现 <scrip ...