题面

钻石游戏(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. 002-Django数据库及后台admin配置

    连接mysql数据库 数据库准备 如果连接本机数据库,mysql安装及配置可参考https://www.cnblogs.com/feizisy/p/11882521.html 如果连接阿里云RDS,需 ...

  2. LTUI v1.1, 一个基于lua的跨平台字符终端UI界面库

    简介 LTUI是一个基于lua的跨平台字符终端UI界面库. 此框架源于xmake中图形化菜单配置的需求,类似linux kernel的menuconf去配置编译参数,因此基于curses和lua实现了 ...

  3. 第九周课程总结&实验报告七

    实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. package ...

  4. [转帖]功耗降50%,性能升35%!三星3nm GAA 2021年量产

    功耗降50%,性能升35%!三星3nm GAA 2021年量产 http://www.chinaflashmarket.com/Instructor 在三星晶圆代工技术论坛(Samsung Found ...

  5. P2562kitty猫基因

    这道题是安徽NOI省选题,但是难度并没有那么难. 这道题是一个字符串的递归题,有很多大佬用线段树来写的(我也想学线段树,lowbit当头一棒).题意为全部相同则输出B或A,不同则分成长度相同的两个子串 ...

  6. 类———用类定义对象———error:C++表达式必须包含类类型

    //原文参考https://blog.csdn.net/lanchunhui/article/details/52503332 你以为你定义了一个类的对象,其实在编译器看来你是声明了一个函数 clas ...

  7. [LeetCode] 45. 跳跃游戏 II

    题目链接 : https://leetcode-cn.com/problems/jump-game-ii/ 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位 ...

  8. Scrapy 教程(二)-操作流程

    scrapy 是个爬虫框架,是由python编写的,用法类似 django 框架. 创建工程 在开始爬虫之前,先创建工程 scrapy startproject projectname 目录结构如下图 ...

  9. Servlet&Http&Request笔记

    # 今日内容:     1. Servlet     2. HTTP协议     3. Request ## Servlet:     1. 概念     2. 步骤     3. 执行原理      ...

  10. CSS中:first-child伪类

    使用 :first-child 伪类来选择作为某个元素的第一个子元素.这个特定伪类很容易遭到误解,所以有必要举例来说明.考虑以下标记: 如下: html: <div> <p>T ...