boolcheck(int low, int high){ for (int i = low + 1; i <= high; ++i) { for (int j = low; j < i; ++j) { if (a[i] == a[j]) returnfalse; } } returntrue; }
intmain(){ int res = 0; cin >> n; for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { if (check(j, i)) res = max(res, i - j + 1); } } cout << res << endl; return0; }
boolcheck(int low, int high){ for (int i = low + 1; i <= high; ++i) { for (int j = low; j < i && mark[a[i]] > 0; ++j) { if (a[i] == a[j]) returnfalse; } } returntrue; }
intmain(){ int res = 0; cin >> n; for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 0; i < n; ++i) { while (j <= i) { if (!check(j, i)) j++; else { res = max(res, i - j + 1); break; } } } cout << res << endl; return0; }
对于每一个i如何确定j的位置:由于[j, i - 1]是前一步得到的最长连续不重复子序列,所以如果[j, i]中有重复元素,一定是a[i]重复,因此右移j直到a[i]不重复为止(由于[j, i - 1]已经是前一步的最优解,此时j只可能右移以剔除重复元素a[i],不可能左移增加元素,因此,j具有单调性、本题可用双指针降低复杂度)。
intmain(){ int res = 0; cin >> n; for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 0, j = 0; i < n; ++i) { count[a[i]]++; while (count[a[i]] > 1) {//某个数字的数量大于1该数字重复, 将j指针移动到与i指针相同的位置 count[a[j]]--; j++; } res = max(res, i - j + 1); } cout << res << endl; return0; }
intmain(){ scanf("%d %d %d", &n, &m, &x); for (int i = 0; i < n; ++i) scanf("%d", &A[i]); for (int i = 0; i < m; ++i) scanf("%d", &B[i]); for (int i = 0, j = m - 1; i < n; ++i) { //int j = m - 1;不可写在for循环中 while (A[i] + B[j] > x && j >= 0) j--; if (A[i] + B[j] == x) { printf("%d %d\n", i, j); break; } } return0; }
intmain(){ scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d", &arr1[i]); for (int i = 0; i < m; ++i) scanf("%d", &arr2[i]); int i = 0, j = 0; while (i < n && j < m) { if (arr1[i] == arr2[j]) i++; j++; } if (i == n) puts("Yes"); elseputs("No"); return0; }