为什么我用upper_bound出来的是0?
  • 板块学术版
  • 楼主Stevehim
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/11/13 22:26
  • 上次更新2023/10/27 03:02:12
查看原帖
为什么我用upper_bound出来的是0?
759274
Stevehim楼主2022/11/13 22:26

源代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 10000010
using namespace std;
//用前缀和?
int a[maxn];

int b[maxn] = {0};
int temp;
int i = 1; //从1开始计数
int last = 1;
void sss() {
	for (int j = 1; j <= last; j++) {
		cout << b[j] << ' ';
	}
	cout << endl;
}
int main() {
	int n;
	cin >> n;

	for (int j = 1; j <= n; j++) {
		cin >> a[j];
	}
//	while (scanf("%d", &temp) != EOF) {
//		a[i++] = temp;
//	}
	b[last] = a[1];
	for (int j = 2; j <= n; j++) {
		if (b[last] >= a[j]) {
			last++;
			b[last] = a[j];
//			cout << 1 << endl;
		} else {
//			cout << a[j] << endl;
//			sss();
			int p = upper_bound(b + 1, b + last + 1, a[j]) - b;
			b[p] = a[j];
		}
	}
	cout << last << endl;
	return 0;
}

数据如下;

389 207 155 300 299 170 158 65

我尝试输出第一次的p,结果显示是0。

是哪里错了吗???

我是真没发现

哭了

2022/11/13 22:26
加载中...