Skip to content

Instantly share code, notes, and snippets.

@0x2f0713
Last active May 31, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x2f0713/98010d01453e891e5f51d32b43fecbf2 to your computer and use it in GitHub Desktop.
Save 0x2f0713/98010d01453e891e5f51d32b43fecbf2 to your computer and use it in GitHub Desktop.

Bai 3: Doan cong dai nhat (7 diem)

Cho day A gom n so nguyen a[[1], a[2],..., an voi (1 <= n <= 10 ^5, |a[i]| <= 10^9)

Hay tim doan con gom cac phan tu lien tiep dai nhat trong day sao cho khong co hai phan tu nao co gia tri bang nhau dung canh nhau (a[i] khac a[i+1], 1 <= i < n)

Du lieu vao tu tep van ban DCDN.INP co cau truc:

  • Dong dau tien chua so nguyen duong n;
  • Dong tiep theo gom n so nguyen a[1], a[2],.., a[n]

Du lieu ra ghi vao tep van ban DCDN.OUT ghi mot so nguyen duy nhat la chieu dai cua doan cong dai nhat tim duoc

Cac so tren cung dong ghi cach nhau it nhat 1 dau cach.

Vi du:

DCDN.INP DCDN.OUT
7
3 3 3 4 5 6 6 4
8
2 4 5 5 4 3 4 5 5
#include <iostream>
using namespace std;
int main()
{
int SoPhanTuCuaMang = 8;
int arr[SoPhanTuCuaMang] = {2,4,5,5,4,3,4,5};
int mang[1000][1000] = {1};
int max = 1;
for (int i = 0; i < SoPhanTuCuaMang; i++)
{
for (int j = i + 1; j < SoPhanTuCuaMang; j++)
{
if (arr[j] != arr[j + 1])
{
mang[i][j] = mang[i][j - 1] + 1;
cout << mang[i][j] << " (" << arr[j] << ") ";
if (mang[i][j] > max)
{
max = mang[i][j];
}
}
else
{
mang[i][j] = mang[i][j - 1] + 1;
cout << mang[i][j] << " (" << arr[j] << ") ";
if (mang[i][j] > max)
{
max = mang[i][j];
}
break;
}
}
cout << endl;
}
cout << max << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment