操作3(求x的前驱)是不是题目描述有误
查看原帖
操作3(求x的前驱)是不是题目描述有误
67890
Soray楼主2020/8/3 16:32

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
inline int read()
{
	int num=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		num=(num<<1)+(num<<3)+ch-'0';
		ch=getchar();
	}
	return num*w;
}
int a[10001],n;
//lower_bound(a+1,a+n+1,x)-a返回第一个大于等于x的数所在位置
//upper_bound(a+1,a+n+1,x)-a返回第一个大于x的数所在位置 
int main()
{
	int q=read();
	for(int i=1;i<=q;i++)
	{
		int k=read(),x=read();
		if(k==1) cout<<lower_bound(a+1,a+n+1,x)-a<<endl;
		if(k==2) cout<<a[x]<<endl;
		if(k==3)
		{
			int temp=lower_bound(a+1,a+n+1,x)-a;
			cout<<a[temp-1]<<endl;
		//	if(temp!=n+1&&temp!=1) cout<<a[temp-1]<<endl;
		//	else cout<<"-2147483647"<<endl;
		}
		if(k==4)
		{
			int temp=upper_bound(a+1,a+n+1,x)-a;
			if(temp!=n+1) cout<<a[temp]<<endl;
			else cout<<"2147483647"<<endl;
		}
		if(k==5)
		{
			int temp=lower_bound(a+1,a+n+1,x)-a;
			if(temp!=n+1)
			{
				for(int j=n;j>=temp;j--) a[j+1]=a[j];
				a[temp]=x,n++;
			}
			else a[++n]=x;
		}
	}
	return 0;
}

把判断是否找得到前驱去掉后41、42行,AC了 如果判断找不到时输出-2147483647,只过了第一个点

望巨佬解答

2020/8/3 16:32
加载中...