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

析:在比赛时,我是暴力做的,虽然错了好多次,后来说理解是rmq,我又用rmq写了一次,发现rmq还没有我暴力快,rwq 2000多,暴力才700.

暴力中加了一个优化条件就是前枚举 i 时,下一个 i 值不一定是i+1,而是满足条件中的最大值的位置。这样优化就是时间很短了。

如果用rmq,就得用两个dp数组分别记录最大值和最小值的下标,然后枚举 i,在i+1 - n-1这个区间中求第一个小于 a[i] 的数,然后再从 i+1 - 该数,

求最大的那个数的下标。不断更新答案即可。

代码如下:

暴力的代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn]; int main(){
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; ++i) scanf("%d", a+i);
int ans = 0;
int x;
for(int i = 0; i < n-ans; i = x+1){
x = Min(x, a[i]);
int mmax = a[i];
x = i;
for(int j = i+1; j < n; ++j){
if(mmax < a[j]){
x = j;
mmax = a[j];
}
if(a[j] < a[i]) break;
if(mmax <= a[j]){ ans = Max(ans, j-i); }
}
}
printf("%d\n", ans ? ans : -1);
}
return 0;
}

rmq的代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m; }
int dp1[maxn][20], dp2[maxn][20];
int a[maxn];
inline int Min_(int i, int j){ return a[i] < a[j] ? i : j; }
inline int Max_(int i, int j){ return a[i] < a[j] ? j : i; } void rmq_init(){
for(int i = 0; i < n; ++i) dp1[i][0] = dp2[i][0] = i;
for(int j = 1; (1<<j) <= n; ++j)
for(int i = 0; i + (1<<j) - 1 < n; ++i){
dp1[i][j] = Min_(dp1[i][j-1], dp1[i+(1<<(j-1))][j-1]);
dp2[i][j] = Max_(dp2[i][j-1], dp2[i+(1<<(j-1))][j-1]);
}
} inline int rmqmin(int l, int r){
int k = (int)(log(r-l+1.0) / log(2.0));
return Min_(dp1[l][k], dp1[r-(1<<k)+1][k]);
} inline int rmqmax(int l, int r){
int k = (int)(log(r-l+1.0) / log(2.0));
return Max_(dp2[l][k], dp2[r-(1<<k)+1][k]);
} int solve(int i){
int l = i+1, r = n-1;
while(l < r){
int mid = (l+r) >> 1;
if(a[rmqmin(l, mid)] > a[i]) l = mid + 1;
else r = mid;
}
return rmqmax(i, l);
} int main(){
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; ++i) scanf("%d", a+i);
rmq_init();
int ans = 0;
for(int i = 0; i < n - ans - 1; ++i){
int j = solve(i);
ans = Max(ans, j-i);
}
printf("%d\n", ans ? ans : -1);
}
return 0;
}

最新文章

  1. Winform 后台将指定的控件集合添加到制定容器中
  2. 转载:《TypeScript 中文入门教程》 15、可迭代性
  3. 解决NSData转NSString返回nil的问题
  4. shell命令xargs
  5. 修改vs helpview手册路径
  6. [Shell]Bash变量:环境变量的配置文件和登录信息
  7. android 进程间通信数据(二)------parcel的实现
  8. 一个NULL引发的血案
  9. 【leetcode❤python】 290. Word Pattern
  10. EditText 监听回车事件 避免2次触发
  11. Oracle DataGuard 升级 [11.2.0.1 -&gt; 11.2.0.4]
  12. Android 6.0系统动态请求系统相机权限
  13. Urozero Autumn 2016. UKIEPC 2016
  14. 【软件测试】Junit入门
  15. FW--tomcat bi-laternal https and keytool
  16. ES6 变量的解构
  17. SQL Server 2005无法远程连接的解决方法 (转帖)
  18. 【机器学习】主成分分析PCA(Principal components analysis)
  19. Chapter 3 Phenomenon——14
  20. 【转】Dubbo声明式缓存

热门文章

  1. css伪类实现文字两侧划线效果
  2. Leetcode 143.重排链表
  3. 【计算几何+极角排序+爆ll】E. Convex
  4. 搜索 比MySQL快10倍?这可能是目前AWS Aurora最详解读!
  5. 中国福利彩票,牛B,开奖和数据传输有什么关系?
  6. Servlet CDI 例子分析
  7. systemtap 作用-- SystemTap使用技巧
  8. Oracle 设置用户密码永不过期
  9. Win7 文件加密存储操作后,如何在事后备份证书、秘钥
  10. AngularJS $q 和 $q.all 单个数据源和多个数据源合并(promise的说明)