我是C ++的新手,所以这可能是一个入门的问题。我编写了此程序,该程序读取字符串中的N个数字,将它们放入向量中,然后使用它们。每个数字可以大到10 ^ 7,并且数字量可以是5 * 10 ^ 5。 这是我的程序:
#include <iostream>
#include <vector>
using namespace std;
int readint() {
int n = 0;
char c = getchar();
while ( !( '0' <= c && c <= '9' ) ) {
c = getchar();
}
while ( '0' <= c && c <= '9' ) {
n = n * 10 + c - '0';
c = getchar();
}
return n;
}
int CeilIndex(vector<int>& v,int l, int r, int key) {
while (r - l > 1) {
int m = l + (r - l) / 2;
if (v[m] >= key)
r = m;
else
l = m;
}
return r;
}
int LongestIncreasingSubsequenceLength(vector<int>& v) {
if (v.size() == 0)
return 0;
vector< int> tail(v.size(), 0);
vector< int> tail2(v.size(), 0);
int length = 1;
int length2 = 1;
int pos=0;
tail2[0]=v[0];
tail[0]=v[0];
for (size_t i = 1; i < v.size();i++){
if(v[i]<tail[length-1]){
if(tail2[length]>v[i]||tail2[length]==0){
{
pos=length;
tail2[length]=v[i];}}
if(length==length2){
length2=length2+1;
}}
if (v[i] < tail[0]){
tail[0] = v[i];
}
else if (v[i] > tail[length - 1]){
tail[length++] = v[i];
}
else{
tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
}
if (v[i] > tail2[length2 - 1]){
tail2[length2++] = v[i];}
if (v[i]<tail2[length2-1]){
if(v[i]>tail2[pos]){
tail2[CeilIndex(tail2, pos, length2 - 1, v[i])] = v[i];
}}}
return length2;
}
int main() {
int N=readint();
vector<int>v(N);
for(long long int i=0;i<N; ++i){
v[i]=readint();
}
cout << LongestIncreasingSubsequenceLength(v) << '\n';
return 0;
}
对于中小型输入似乎没有问题。但是,当输入接近上限时,程序将无法编译且不会返回值。所以问题是,我如何声明输入,以便我能够涵盖最大的输入值?