HDU4495 Rectangle
求组成的等腰三角形面积最大值。
对此题的总结:暴力出奇迹
组成的三角形放置方式一共只有4种,用ans表示目前已知的最长三角形的边长,从上到下,从左到右枚举顶点,再枚举边长,一个重要剪枝是枚举边长l时先判断l = ans时的边能不能对称。
最终暴力只要200多ms,而时间限制为10s
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = , INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
char g[N][N];
int n, m;
int ans = ; bool judge(int x1, int y1, int x2, int y2){
while(y1 < y2){
if(g[x1][y1] != g[x2][y2]){
return false;
}
x1--;
x2++;
y1++;
y2--;
}
return true;
} bool judge2(int x1, int y1, int x2, int y2){
while(x1 < x2){
if(g[x1][y1] != g[x2][y2]){
return false;
}
x1++;
x2--;
y1++;
y2--;
}
return true;
} void solve1(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){
if(i + ans - < n && j + ans - < m){ if(!judge(i + ans - , j, i, j + ans - )){
continue;
}
for(int l = ; i + l - < n && j + l - < m; l++){
if(!judge(i + l - , j, i, j + l - )){
break;
}
ans = max(ans , l);
}
}
}
}
} void solve2(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){ if(j - ans + >= && i + ans - < n){ if(!judge2(i, j - ans + , i + ans - , j )){
continue;
} for(int l =; j - l + >= && i + l - < n; l++){
if(!judge2(i, j - l + , i + l - , j )){
break;
}
ans = max(ans , l);
}
}
}
}
} void solve3(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){
if(i - ans + >= && j + ans - < m){ if(!judge2(i - ans + , j, i , j + ans - )){
continue;
}
for(int l = ; i - l + >= && j + l - < m; l++){
if(!judge2(i - l + , j, i , j + l - )){
break;
}
ans = max(ans , l);
}
}
}
}
} void solve4(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){
if(j - ans + >= && i - ans + >= ){ if(!judge(i, j - ans + , i - ans + , j)){
continue;
}
for(int l = ; j - l + >= && i - l + >= ; l++){
if(!judge(i, j - l + , i - l + , j)){
break;
}
ans = max(ans , l);
}
}
}
}
} int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i= ; i< n; i++){
scanf("%s", g[i]);
}
ans = ;
int mini = min(n ,m);
solve1();
if(ans != mini){
solve2();
}
if(ans != mini){
solve2();
} if(ans != mini){
solve3();
}
if(ans != mini){
solve4();
} cout<<ans * (ans + )/<<'\n';
} return ;
}
HDU4495 Rectangle的更多相关文章
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Maximal Rectangle
很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
随机推荐
- ubuntu安装cacti错误
安装cacti时,明明mysql信息都配置正确了,权限也分配好了,可是仍然报错,如下: 这时可以试试到/etc/cacti目录下,修改debian.php中的mysql配置信息,问题应该就能解决了.
- hadoop学习过程中一系列问题
12---修改host .修改主机名5.1 修改当前会话的主机名sudo hostname SY-0217查看当前会话的主机名hostname默认是我装Linux 虚机时输入的名字Ubuntu5.2 ...
- 如何发布及部署asp.net网站
用vs开发完asp.net网站之后如何发布到服务器上去变成正式网站? 1.发布首先需要发布网站,这个步骤就是生成正式网站的目录(把源代码编译成.cs代码文件编译成.dll文件)详细步骤:http:// ...
- 一致性哈希算法以及其PHP实现
在做服务器负载均衡时候可供选择的负载均衡的算法有很多,包括: 轮循算法(Round Robin).哈希算法(HASH).最少连接算法(Least Connection).响应速度算法(Respons ...
- espcms列表页ajax获取内容 - 并初始化swiper
<link rel="stylesheet" href="swiper.min.css" type="text/css" media= ...
- Java Dao设计模式
一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是客户端,简单的来说就是浏览器. 2.显示层:JSP/S ...
- IPC-----POSIX消息队列
消息队列可以认为是一个链表.进程(线程)可以往里写消息,也可以从里面取出消息.一个进程可以往某个消息队列里写消息,然后终止,另一个进程随时可以从消息队列里取走这些消息.这里也说明了,消息队列具有随内核 ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- Java集合中Map接口的使用方法
Map接口 Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value: Map中的键值对以Entry类型的对象实例形式存在: 建(key值 ...
- Android webView解析URL参数
2015年6月18日 13:56:21 星期四 又当爹又当娘啊............ public void onPageFinished(WebView view, String url) { s ...