An Easy Problem
2013-04-17 18:59
176 查看
Description
Now we define that:
A(j)={i|0<i<j&&a[i]<a[j]}
Here a is an array with n element(1<=n<=1000000);
And then we define a function f that:
int f(int j){
if(A(j) is not empty) return max( A(j) );
else return -1;
}
Input
The first line contain an integer n(1<=n<=1000000),the size of the array a.
The second line contain n integers describe the array a.
The third line is an integer m(1<=m<=1000000),the number of the query.
And then follow m lines,each line is an integer j(1<=j<=n).
Output
For each query j,output a[f(j)] if f(j)>=1,Otherwise output -1 instead.
Sample Input
3
1 2 3
3
1
2
3
Sample Output
-1
1
2
单调DP,用另一数组记录下a[f(i)],可用stack实现。
Now we define that:
A(j)={i|0<i<j&&a[i]<a[j]}
Here a is an array with n element(1<=n<=1000000);
And then we define a function f that:
int f(int j){
if(A(j) is not empty) return max( A(j) );
else return -1;
}
Input
The first line contain an integer n(1<=n<=1000000),the size of the array a.
The second line contain n integers describe the array a.
The third line is an integer m(1<=m<=1000000),the number of the query.
And then follow m lines,each line is an integer j(1<=j<=n).
Output
For each query j,output a[f(j)] if f(j)>=1,Otherwise output -1 instead.
Sample Input
3
1 2 3
3
1
2
3
Sample Output
-1
1
2
单调DP,用另一数组记录下a[f(i)],可用stack实现。
#include<iostream> #include<stack> #include<cstdio> using namespace std; #define N 1000005 int a ,b ; int main() { int n,m,i,temp,x; stack<int> S; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); if(S.empty()) { b[i]=-1; S.push(a[i]); } else { temp=S.top(); if(temp<a[i]) { b[i]=temp; S.push(a[i]); } else { S.pop(); while(!S.empty()) { temp=S.top(); if(temp<a[i]) { b[i]=temp; S.push(a[i]); break; } else S.pop(); } if(S.empty()) { b[i]=-1; S.push(a[i]); } } } } scanf("%d",&m); while(m--) { scanf("%d",&x); printf("%d\n",b[x-1]); } return 0; }
相关文章推荐
- An Easy Problem
- [POJ2826]An Easy Problem?!(计算几何)
- hdu 2132 An easy problem
- hdu2601 An easy problem(数学)
- An Easy Problem?! - POJ 2826(求面积)
- POJ1152 UVA10093 An Easy Problem!【进制】
- An Easy Physics Problem HDU - 5572 直线与圆
- 水题堆1.V An easy problem
- 线段树:CDOJ1592-An easy problem B (线段树的区间合并)
- HDU-5475 An easy problem(模拟||(倒着计算+线段树))
- UESTC - 1597 An easy problem C (线段树)
- 【杭电-oj】-2055-An easy problem
- HDU 2132 An easy problem
- 位运算 | 2453 | An Easy Problem
- An Easy Problem
- POJ 2826 An Easy Problem?! (计算几何、线段相交、思维)
- HDU2601,An easy problem
- HDU2601-An easy problem
- [HDU 5572] An Easy Physics Problem (点在线上判定+对称)
- poj——1152——An Easy Problem!