↓ 跳过正文
  1. Posts/

poj 2452 Sticks Problem (rmq+二分,需要返回最值位置)

·683 字·2 分钟

poj2452题目链接

题意:给你一组数a[n],求满足a[i] < a[k] < a[j] (i <= k <= j)的最大的j-i。

思路:大概能想到是rmq,然后想出了一个错误复杂度的错误思路,还直到对拍才发现==

转载一篇题解:poj2452解题报告

收获最大的是:

对于最大值和最小值返回val还是位置的转化竟然可以这样容易!

对于最大值和最小值返回val还是位置的转化竟然可以这样容易!

对于最大值和最小值返回val还是位置的转化竟然可以这样容易!

只要

1int _min(int l,int r)
2{
3    if (a[l]<a[r]) return l;
4    return r;
5}

这样一个函数就可以实现完美转化。。。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年05月16日 星期一 13时42分56秒
  4File Name :code/poj/2452.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=5E4+7;
 34int n;
 35int a[N];
 36int dp[N][20];
 37int dp2[N][20];
 38
 39
 40int _min(int l,int r)
 41{
 42    if (a[l]<a[r]) return l;
 43    return r;
 44}
 45int _max( int l,int r)
 46{
 47    if (a[l]>a[r]) return l;
 48    else return r;
 49}
 50void max_init()
 51{
 52    for ( int i = 1 ; i <= n ; i++) dp[i][0] = i;
 53
 54    for ( int j = 1 ; (1<<j)<=n ; j++)
 55	for (int i = 1 ; i + (1<<j)-1 <= n ;i++)
 56	    dp[i][j] = _max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
 57}
 58
 59void min_init()
 60{
 61    for ( int i = 1 ;i  <= n ; i++) dp2[i][0] = i;
 62
 63    for ( int j = 1 ; (1<<j)<= n ; j++)
 64	for ( int i = 1 ; i + (1<<j) -1 <= n ; i++)
 65	    dp2[i][j] = _min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
 66}
 67
 68int rmq_max(int l,int r)
 69{
 70    int k = 0 ;
 71    while (1<<(k+1)<=r-l+1) k++;
 72    return _max(dp[l][k],dp[r-(1<<k)+1][k]);
 73}
 74
 75int rmq_min( int l,int r)
 76{
 77    int k = 0  ;
 78    while (1<<(k+1)<=r-l+1) k++;
 79    return _min(dp2[l][k],dp2[r-(1<<k)+1][k]);
 80}
 81
 82int bin (int x,int l,int r)
 83{
 84    while (l<=r)
 85    {
 86	if (l==r) return l;
 87	int m = (l+r)>>1;
 88	if (a[x]<a[rmq_min(l,m)])
 89	    l = m + 1;
 90	else r = m;
 91    }
 92}
 93void solve()
 94{
 95
 96    int ans = 0;
 97    for ( int i = 1 ; i+ans < n ; i++)
 98    {
 99	int r = bin(i,i+1,n);
100	int k = rmq_max(i,r);
101	if (a[k]>a[i])
102	    ans = max(ans,k-i);
103    }
104    if (ans==0) puts("-1");
105    else printf("%d\n",ans);
106}
107int main()
108{
109	#ifndef  ONLINE_JUDGE
110	freopen("code/in.txt","r",stdin);
111  #endif
112	 while (scanf("%d",&n)!=EOF)
113	{
114	    for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
115	    max_init();
116	    min_init();
117
118	    solve();
119	//    cout<<"sadsad"<<endl;
120
121	}
122
123  #ifndef ONLINE_JUDGE
124  fclose(stdin);
125  #endif
126    return 0;
127}

相关文章

lightoj 1081 Square Queries (二维rmq,降维)

·434 字·1 分钟
lightoj 1081 题目链接 题意:和上一道一样,但是由于size变成了500,如果按照之前的做法会tle + mle… 很容易发现,由于是方阵,长宽是相等的,所以有一维是可以省略的。 也就是所谓的降维?

poj 2019 Cornfields (二维rmq)

·497 字·1 分钟
poj2019题目链接 题意:给一个方阵,k个查询,每个查询求某个方阵的最大值和最小值之差。 思路:二维rmq.同时用到最大值和最小值的话可以把初始化写在一起。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年05月16日 星期一 18时31分23秒 4File Name :code/poj/2019.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=251; 34int a[N][N]; 35int dp[N][N][8][8]; 36int dp2[N][N][8][8]; 37int n,b,q; 38 39void init_rmq() 40{ 41 for ( int i = 1 ;i <= n ; i++) 42 for ( int j = 1 ; j <= n ; j++) 43 dp[i][j][0][0] = dp2[i][j][0][0] = a[i][j]; 44 45 46 for ( int i = 0 ; (1<<i)<= n ; i++) 47 for ( int j = 0 ; (1<<j) <= n ; j++) 48 if (i==0 && j==0) continue; 49 else for ( int p = 1 ; p + (1<<i)-1 <= n ; p++) 50 for ( int q = 1 ; q + (1<<j)-1 <= n ; q++) 51 if (i==0) 52 { 53 dp[p][q][i][j] = max(dp[p][q][i][j-1],dp[p][q+(1<<(j-1))][i][j-1]); 54 dp2[p][q][i][j] = min(dp2[p][q][i][j-1],dp2[p][q+(1<<(j-1))][i][j-1]); 55 } 56 else 57 { 58 dp[p][q][i][j] = max(dp[p][q][i-1][j],dp[p+(1<<(i-1))][q][i-1][j]); 59 dp2[p][q][i][j] = min(dp2[p][q][i-1][j],dp2[p+(1<<(i-1))][q][i-1][j]); 60 } 61} 62 63 64int _rmq(int x1,int y1,int x2,int y2) 65{ 66 int k1 = 0 ; 67 int k2 = 0 ; 68 while (1<<(k1+1)<=x2-x1+1) k1++; 69 while (1<<(k2+1)<=y2-y1+1) k2++; 70 71 int tmp1 = dp[x1][y1][k1][k2]; 72 int tmp2 = dp[x2-(1<<k1)+1][y1][k1][k2]; 73 int tmp3 = dp[x1][y2-(1<<k2)+1][k1][k2]; 74 int tmp4 = dp[x2-(1<<k1)+1][y2-(1<<k2)+1][k1][k2]; 75 76 int mx = max(max(tmp1,tmp2),max(tmp3,tmp4)); 77 78 tmp1 = dp2[x1][y1][k1][k2]; 79 tmp2 = dp2[x2-(1<<k1)+1][y1][k1][k2]; 80 tmp3 = dp2[x1][y2-(1<<k2)+1][k1][k2]; 81 tmp4 = dp2[x2-(1<<k1)+1][y2-(1<<k2)+1][k1][k2]; 82 83 int mn = min(min(tmp1,tmp2),min(tmp3,tmp4)); 84 85 // cout<<"mx:"<<mx<<" mn:"<<mn<<endl; 86 87 return mx - mn; 88} 89 90 91int main() 92{ 93 #ifndef ONLINE_JUDGE 94 freopen("code/in.txt","r",stdin); 95 #endif 96 scanf("%d %d %d",&n,&b,&q); 97 for ( int i = 1 ; i <= n ; i++) 98 for ( int j = 1 ; j <= n ; j++) scanf("%d",&a[i][j]); 99 init_rmq(); 100 101 while (q--) 102 { 103 int x1,y1; 104 scanf("%d %d",&x1,&y1); 105 printf("%d\n",_rmq(x1,y1,x1+b-1,y1+b-1)); 106 } 107 108 109 110 #ifndef ONLINE_JUDGE 111 fclose(stdin); 112 #endif 113 return 0; 114}

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

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

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

·1014 字·3 分钟
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.

BZOJ 1650: [Usaco2006 Dec]River Hopscotch 跳石子 (二分)

·1042 字·3 分钟
1650: [Usaco2006 Dec]River Hopscotch 跳石子 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 440 Solved: 290 [Submit][Status][Discuss] Description # Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 <= L <= 1,000,000,000). Along the river between the starting and ending rocks, N (0 <= N <= 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L). To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river. Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 <= M <= N). FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.