poj 2452 想法题
来源:互联网 发布:淘宝商品推广 编辑:程序博客网 时间:2024/05/16 14:03
题意:给出一组数据,求出满足data[i]<data[k]<data[j]且k=i+1~j-1,max(j-i)。
分析:枚举每个起点data[i],求出从i+1开始大于data[i]的连续的子区间,假设k是最后一个满足data[k]>data[i]的数据,且data[k+1]<data[i],
那么可以求出这段子区间中的最大值,并保存最大值的位置posmax,则以i为起点得到的最大长度为posmax-i;
由于位于i~posmax之间的值所得到的最大长度都不可能大于posmax-i,故可以直接跳到posmax+1作为起点。
为什么不可以直接跳到k+1呢,因为posmax+1~k有可能存在最大长度大于posmax-i的点,必须考虑。
#include<iostream>#include<cstdio>#include<climits>using namespace std;int main(){ int n,data[50005],len,i,curmax,posmax,j; //freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { for(i=1; i<=n; i++) scanf("%d",&data[i]); for(len=0,i=1; i<n; i=posmax+1)//直接跳到posmax+1作为起点。 { for(curmax=INT_MIN,posmax=i,j=i+1; j<=n&&data[j]>data[i]; j++) if(curmax<data[j]) { curmax=data[j]; posmax=j; }//求这段子区间的最大值并保存位置。 if(len<posmax-i) len=posmax-i; //printf("j=%d posmax=%d len=%d\n",j,posmax,len); } if(!len) len=-1; printf("%d\n",len); } return 0;}
0 0
- poj 2452 想法题
- poj 2008 #数学想法题
- POJ 1990 MooFest(想法题&树状数组)
- POJ 2352 Stars (线段树&想法题)
- POJ 3210 Coins (很好的想法题)
- Building for UN - POJ 3566 想法题
- POJ 3045 Cow Acrobats (想法题)
- [poj 1019] Number Sequence 数学 想法题
- poj 3104 二分想法
- POJ-1852-想法
- POJ—1852—Ants—【入门题】【想法题】
- POJ 2965 The Pilots Brothers' refrigerator (想法题)
- POJ 2479的简单想法
- POJ 2828 Buy Tickets (想法题&后序插入&线段树下的二分查找)
- UVa 11235 / POJ 3368 Frequent values (想法题&RMQ线段树维护最长区间长度)
- POJ 2184 Cow Exhibition (想法题&双变量0-1背包)
- POJ-2182 Lost Cows 简单的想法
- POJ 2689 Prime Distance [素数筛+想法]
- 【链接】9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- 【麦子学院】OpenCV教程函数总结
- 普通android工程转换为C/C++工程之后,再还原成android工程的解决方案
- android GPS定位(locationmanager,高德,百度)定位的比较
- jsp乱码问题处理方法
- poj 2452 想法题
- HTTP文件断点上传
- xcode 开发工具下载和使用
- 数据结构学习----线性表的链式表示之循环双链表(Java实现)
- 【麦子学院】Linux cmd命令大全
- 关于hosts文件
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”)细微区别
- Mybatis通用DAO设计封装(mybatis)
- 基于XMPP协议的Android即时通信系