Cod sursa(job #2532010)

Utilizator rapunzelMihnea Andreescu rapunzel Data 27 ianuarie 2020 09:27:16
Problema Nivele Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin("nivele.in");
ofstream cout("nivele.out");

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        int s = a[0], b = a[0], cnt = 0;
        for (int i = 1; i < n; i++)
        {
            s = min(s, a[i]);
            b = max(b, a[i]);
        }
        for (int i = 0; i < n && !cnt; i++)
        {
            cnt += (a[i] != s && a[i] != b);
        }
        if (cnt)
        {
            cout << "NU\n";
            continue;
        }
        int levels = 0;
        int i = 0;
        while (i < n)
        {
            int j = i;
            while (j + 1 < n && a[j + 1] == a[j])
            {
                j++;
            }
            int length = j - i + 1;
            if (a[i] == b && length % 2)
            {
                levels = -1;
                break;
            }
            if (a[i] == b)
            {
                levels += length / 2;
            }
            else
            {
                levels += length;
            }
            i = j + 1;
        }
        while (levels % 2 == 0)
        {
            levels /= 2;
        }
        if (levels == 1)
        {
            cout << "DA\n";
        }
        else
        {
            cout << "NU\n";
        }
    }
}