Cod sursa(job #2532012)

Utilizator rapunzelMihnea Andreescu rapunzel Data 27 ianuarie 2020 09:34:12
Problema Nivele Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#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";
        }
    }
}