↓ Skip to main content
  1. Posts/

hdu 2888 check corners (二维rmq模板题)

·484 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

hdu2888题目链接

题意:问某个矩阵内的最大值,并且问最大值是否是在四个角中出现。 思路:二维rmq.需要注意数组稍微开大1就会MLE,因为是四维数组,一维大一点,整个就会大很多==。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年05月16日 星期一 16时51分00秒
  4File Name :code/hdu/2888.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=301;
 34int n,m,q;
 35int a[N][N];
 36int dp[N][N][9][9];
 37
 38
 39void init_rmq()
 40{
 41    for ( int i = 1 ; i <= n ; i++)
 42	for ( int j = 1; j <= m ; j++) dp[i][j][0][0] = a[i][j];
 43
 44    for ( int i = 0 ; (1<<i)<= n ; i++)
 45	for ( int j = 0 ; (1<<j)<= m  ;j++)
 46	    if (i==0&&j==0) continue;
 47	    else for ( int p = 1 ; p + (1<<i)-1 <= n ; p++)
 48		      for ( int q = 1 ; q + (1<<j)-1 <= m ;q ++)
 49			  if (i==0)
 50			      dp[p][q][i][j] = max(dp[p][q][i][j-1],dp[p][q+(1<<(j-1))][i][j-1]);
 51			  else dp[p][q][i][j] = max(dp[p][q][i-1][j],dp[p+(1<<(i-1))][q][i-1][j]);
 52
 53}
 54
 55
 56
 57int rmq_max(int x1,int y1,int x2,int y2)
 58{
 59    int k1 = 0 ;
 60    int k2 = 0 ;
 61
 62    while (1<<(k1+1)<=x2-x1+1) k1++;
 63    while (1<<(k2+1)<=y2-y1+1) k2++;
 64
 65    int tmp1 = dp[x1][y1][k1][k2];
 66    int tmp2 = dp[x2-(1<<k1)+1][y1][k1][k2];
 67    int tmp3 = dp[x1][y2-(1<<k2)+1][k1][k2];
 68    int tmp4 = dp[x2-(1<<k1)+1][y2-(1<<k2)+1][k1][k2];
 69
 70    return max(max(tmp1,tmp2),max(tmp3,tmp4));
 71}
 72
 73int main()
 74{
 75	#ifndef  ONLINE_JUDGE
 76	freopen("code/in.txt","r",stdin);
 77  #endif
 78
 79	while (scanf("%d %d",&n,&m)!=EOF)
 80	{
 81	    for ( int i = 1 ; i <= n ; i++)
 82		for ( int j = 1 ; j <= m ; j++) scanf("%d",&a[i][j]);
 83	    init_rmq();
 84
 85	    scanf("%d",&q);
 86	    while (q--)
 87	    {
 88		int r1,c1,r2,c2;
 89		scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
 90		int ans = rmq_max(r1,c1,r2,c2);
 91		printf("%d ",ans);
 92		if (a[r1][c1]==ans||a[r1][c2]==ans||a[r2][c1]==ans||a[r2][c2]==ans)
 93		{
 94		    puts("yes");
 95		}
 96		else
 97		{
 98		    puts("no");
 99		}
100	    }
101
102	}
103
104
105
106  #ifndef ONLINE_JUDGE
107  fclose(stdin);
108  #endif
109    return 0;
110}

Related

BZOJ 1636: [Usaco2007 Jan]Balanced Lineup (RMQ模板题)

·1014 words·3 mins
1636: [Usaco2007 Jan]Balanced Lineup # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 680 Solved: 493 [Submit][Status][Discuss] Description # For the daily milking, Farmer John’s N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height. Farmer John has made a list of Q (1 <= Q <= 200,000) potential groups of cows and their heights (1 <= height <= 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

hdu 3183 A Magic Lamp ( 暴力)

·455 words·1 min
hdu3183题目链接 题意:n位长的数字串(n<=1000),删掉m个(m<=n),使得剩下的数字串表示的数字最小。 忽略前导0. 思路:暴力搞就可以。要注意每位数字是有一定位置的范围的。比如当前是第i位数字,后面还要取n-m-i位数字,那么第i位数字最多只能取到第k位,k=m+i,因为这样才能保证后面还有n-m-i位数字。

BZOJ 1689: [Usaco2005 Open] Muddy roads 泥泞的路 (模拟)

·785 words·2 mins
1689: [Usaco2005 Open] Muddy roads 泥泞的路 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 311 Solved: 227 [Submit][Status][Discuss] Description # Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainstorms and now contains (1 <= N <= 10,000) mud pools. Farmer John has a collection of wooden planks of length L that he can use to bridge these mud pools. He can overlap planks and the ends do not need to be anchored on the ground. However, he must cover each pool completely. Given the mud pools, help FJ figure out the minimum number of planks he needs in order to completely cover all the mud pools.

hdu 4513 吉哥系列故事——完美队形II (回文串,manacher)

·473 words·1 min
题目链接:hdu4513 题意:给出一个n的数的序列,求出一个最长的回文字串,并且满足从[l,mid]单调增(非严格单调,可以相等),[mid,r]单调减(同样是可以相等) 思路:manacher…int型的也是可以搞的。。要求单调的话。。。while扩展的时候判一下就好了。。。

poj 3294 Girls' research (manacher,回文串)

·1257 words·3 mins
poj 3294 题意:先做个简单替换,然后求替换后的字符串的最长回文串,以及这个最长回文串的开始和结束位置。 思路:manacher。需要注意的是,返回下标的时候如果字符串长度为偶数,那么中间是没有字符的,需要特判一下(我的做法是 left+(ans%2==0))。