1285D - Dr Evil Underscores - CodeForces Solution


bitmasks brute force dfs and similar divide and conquer dp greedy strings trees *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a, trie[10000005][2], k = 1, ans = 0, cnt;
ll b[50], poww[50];
void insertt(ll x)
{
    memset(b, 0, sizeof(b));
    cnt = 0;
    while (x)
    {
        b[cnt] = x % 2;
        x = x / 2;
        cnt++;
    }
    ll p = 0;
    for (ll i = 29; i >= 0; i--)
    {
        if (trie[p][b[i]] == 0)
        {
            trie[p][b[i]] = k;
            k++;
        }
        p = trie[p][b[i]];
    }
}
ll dfs(ll now, ll deep)
{
    if (trie[now][1] == 0 && trie[now][0] == 0)
        return 0; //该节点已经没有子节点
    if (trie[now][1] == 0)
        return dfs(trie[now][0], deep - 1); //该节点只有0子节点
    if (trie[now][0] == 0)
        return dfs(trie[now][1], deep - 1); //该节点只有1子节点
    //只有0子节点或只有1子节点,则在异或完以后最大的数中 这一位是0,所以不需要加上poww[deep]
    return poww[deep] + min(dfs(trie[now][0], deep - 1), dfs(trie[now][1], deep - 1));
    //该节点同时有1和0子节点,则在异或完以后最后最大数中 这一位一定为1,所以需要加上的poww[deep]
}
int main()
{
    scanf("%lld", &n);
    for (ll i = 1; i <= n; i++)
    {
        scanf("%lld", &a);
        insertt(a);
    }
    poww[0] = 1;
    for (ll i = 1; i <= 29; i++)
        poww[i] = poww[i - 1] * 2;
    printf("%lld\n", dfs((ll)0, (ll)29));
}


Comments

Submit
0 Comments
More Questions

960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression