题目:
You are looking for a particular family photo with you and your favorite relatives Alice and Bob. Each family photo contains a line-up of n people. On the photo you’re looking for, you remember that Alice, who is taller than you, was somewhere on your left from the perspective of the photographer. Also, Bob who is taller than both you and Alice, was standing somewhere on your right. Since you have a large number of family photos, you want to use your computer to assist in finding the photo. Many of the photos are quite blurry, so facial recognition has proven ineffective. Luckily, the Batch Apex Photo Classifier, which detects each person in a photo and outputs the sequence of their (distinct) heights in pixels, has produced excellent results. Giventhissequenceofheightsfor k photos, determinewhichofthesephotoscouldpotentially be the photo you’re looking for.
Input
The first line contains 1≤ k ≤1000, the number of photos you have to process.
Then follow two lines for each photo.
– The first line contains a single integer 3 ≤ n ≤ 3·105, the number of people on this photo.
– The second line contains n distinct integers 1 ≤ h1,...,hn ≤ 109, the heights of the people in the photo, from left to right.
It is guaranteed that the total number of people in all photos is at most 3·105.
Output
On the first line, output the number of photos k that need further investigation.
Then print k lines each containing a single integer 1≤ ai ≤ n, the sorted indices of the photos you need to look at.
Sample Input
1
3
2 1 3
Sample Output
1
1
Sample Input
4
4
140 157 160 193
5
15 24 38 9 30
6
36 12 24 29 23 15
6
170 230 320 180 250 210
Sample Output
2
2
4
题意:
给定几个数字序列,判断符不符合这种情况:一个数它左边存在一个比它大的数,右边存在一个比它俩都大的数。
输出符合这种情况的序列个数,再分别输出序列号。
思路:
找右边的大数的时候,可用通过求后缀最大值,O(n)遍历一遍即可。
然后遍历每一个数,找到它前面比它大的并且尽量小的数,然后判断这个位置的后缀最大是否大于他们俩,依次遍历所有的数。
用一个set,每遍历过去一个数,就将它放进去。找一个数前面比它大的并且尽量小的数时,用set的upper_bound()二分查找函数即可。
(不知道是谁给我的勇气比赛时一直坚定不移的以为set不能自动排序...明明两周前刚写过一个类似的博客...)
爱吃代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
using namespace std;
int a[300010],maxx[300010],maxcnt;
int book[1010],cnt;
int main()
{
int t,n,i,j,x,vis;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
maxcnt=0;
for(j=n-1;j>=0;j--)
{
maxcnt=max(maxcnt,a[j]);
maxx[j]=maxcnt;
}
set<int>s;
set<int>::iterator it;
vis=0;
for(j=0;j<n;j++)
{
it=s.upper_bound(a[j]);
if(it!=s.end()&&*it>a[j]&&*it<maxx[j])
{
vis=1;
break;
}
s.insert(a[j]);
}
if(vis==1)
book[cnt++]=i;
}
printf("%d\n",cnt);
for(i=0;i<cnt;i++)
printf("%d\n",book[i]);
return 0;
}