Pagini recente » Cod sursa (job #1565012) | Cod sursa (job #1150729) | Cod sursa (job #56705) | Cod sursa (job #1976484) | Cod sursa (job #2532012)
#include <fstream>
using namespace std;
ifstream cin("nivele.in");
ofstream cout("nivele.out");
const int N = 50000 + 7;
int n;
int a[N];
int b[N];
int l[N];
int top;
bool ok()
{
int small = a[1], big = a[1];
for (int i = 2; i <= n; i++)
{
small = min(small, a[i]);
big = max(big, a[i]);
}
for (int i = 1; i <= n; i++)
{
l[i] = b[i] = 0;
if (a[i] != small && a[i] != big)
{
return 0;
}
}
top = 0;
for (int i = 1; i <= n; i++)
{
int x;
if (a[i] == big)
{
x = 1;
}
else
{
x = 0;
}
if (i == 1 || a[i] != a[i - 1])
{
b[++top] = x;
}
l[top]++;
}
int tot = 0;
for (int i = 1; i <= top; i++)
{
if (b[i])
{
if (l[i] % 2)
{
return 0;
}
else
{
tot += l[i] / 2;
}
}
else
{
tot += l[i];
}
}
while (tot % 2 == 0)
{
tot /= 2;
}
return (tot == 1);
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
if (ok())
{
cout << "DA\n";
}
else
{
cout << "NU\n";
}
}
}