万卷网 > 题目详情
题型:简答题

(两元序列)试求一个整数序列中,最长的仅包含两个不同整数的连续子序列。如有多个子序列并列最长,输出任意一个即可。例如,序列“1 1 2 3 2 3 2 3 3 1 1 1 3 1”中,有两段满足条件的最长子序列,长度均为7,分别用下划线和上划线标出。

#include <iostream>

using namespace std;


int main()

{

const int SIZE = 100;

int n, i, j, a[SIZE], cur1, cur2, count1, count2,

ans_length, ans_start, ans_end;

//cur1, cur2 分别表示当前子序列中的两个不同整数

//count1, count2 分别表示 cur1, cur2 在当前子序列中出现的次数

cin>>n;

for (i = 1; i <= n; i++)

cin>>a[i];

i = 1;

j = 1;

//i, j 分别表示当前子序列的首尾,并保证其中至多有两个不同整数

while ((j <= n) && (a[j] == a[i]))

j++;

cur1 = a[i];

cur2 = a[j];

count1 =      (1)      //(3 分)

count2 = 1;

ans_length = j - i + 1;

while (j < n) {

j++;

if (a[j] == cur1)

count1++;

else if (a[j] == cur2)

count2++;

else {

if (a[j - 1] ==      (2)      ) { //(3 分)

while (count2 > 0) {

if (a[i] == cur1)

count1--;

else

ount2--;

i++;

}

cur2 = a[j];

count2 = 1;

}

else {

while (count1 > 0) {

if (a[i] == cur1)

                                                                      (3)                //(2 分)

else

                                                                       (4)             //(2 分)

i++;

}

                                        (5)           //(3 分)

count1 = 1;

}

}

if (ans_length < j - i + 1) {

ans_length = j - i + 1;

ans_start = i;

ans_end = j;

}

}

for (i = ans_start; i <= ans_end; i++)

cout<<a[i]<<' ';

return 0;

}

更新时间:2023-08-30 13:47:44 |
【知识点】 信息学NOIP提高组

相似题推荐

填空题

如下图所示,A到B是连通的。假设删除一条细的边的代价是1,删除一条粗的边的代价是2,要让A、B不连通,最小代价是_____(2分),最小代价的不同方案数是_______(3分)。(只要有一条删除的边不同,就是不同的方案)

2023-09-02
单选题

中国计算机学会于( )年创办全国青少年计算机程序设计竞赛。(2018年提高组)

A.

1983

B.

1984

C.

1985

D.

1986

2023-09-02
填空题

如右图所示,共有13个格子。对任何一个格子进行一次操作,会使得它自己以及与它上下左右相邻的格子中的数字改变(由1变0,或由0变1)。现在要使得所有的格子中的数字都变为0,至少需要      次操作。

2023-09-02
简答题

(最长路径)给定一个有向无环图,每条边长度为1,求图中的最长路径长度。(第五空2分,其余3分)

输入:第一行是结点数n(不超过100)和边数m,接下来m行,每行两个整数a,b,表示从结点a到结点b有一条有向边。结点标号从0到(n-1)。

输出:最长路径长度。

提示:先进行拓扑排序,然后按照拓扑序计算最长路径。


#include <iostream>

using namespace std;


int n, m, i, j, a, b, head, tail, ans;

int graph[100][100]; // 用邻接矩阵存储图

int degree[100]; // 记录每个结点的入度

int len[100]; // 记录以各结点为终点的最长路径长度

int queue[100]; // 存放拓扑排序结果


int main() {

cin >> n >> m;

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

graph[i][j] = 0;

for (i = 0; i < n; i++)

degree[i] = 0;

for (i = 0; i < m; i++) {

cin >> a >> b;

graph[a][b] = 1;

                 (1)           ;

}

tail = 0;

for (i = 0; i < n; i++)

if (      (2)      ) {

queue[tail] = i;

tail++;

}

head = 0;

while (tail < n - 1) {

for (i = 0; i < n; i++)

if (graph[queue[head] ][i] == 1) {

                               (3)        ;

if (degree[i] == 0) {

queue[tail] = i;

tail++;

}

}

                  (4)        ;

}

ans = 0;

for (i = 0; i < n; i++) {

a = queue[i];

len[a] = 1;

for (j = 0; j < n; j++)

if (graph[j][a] == 1 && len[j] + 1 > len[a])

len[a] = len[j] + 1;

if (      (5)      )

ans = len[a];

}

cout << ans << endl;

return 0;

}

2023-09-02
单选题

设A和B是两个长为n的有序数组,现在需要将A和B合并成一个排好序的数组,请问任何以元素比较作为基本运算的归并算法最坏情况下至少要做( )次比较。

A.

n2

B.

n logn

C.

2n

D.

2n-1

2023-09-02
公众号
客服 反馈
顶部