UVA11384 Help is needed for Dexter (law, thinking)

First, the data range reaches 1e9, so even if O (n) O(n)O ( n ) can’t be done, so we play with examples to find the rules

If you subtract 1 each time, it looks like the number of subtractions is large, but it actually takes n times. Each time you choose the largest one to subtract all, it also takes n times. The closer we get to the middle, the less the total number of times. This is The first impression of this question.

If we draw a picture

     1    
    11
   111
  1111
 11111
111111

We found that the square in the middle is bigger than the sides, so we divide the two parts by half each time, and subtract the minimum of the two parts respectively. This will become two identical two parts, which will be divided and halved after sorting. , The final answer is log2n+1, we can directly use the log2 function in the cmath library to output the answer.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>

using namespace std;
typedef long long ll;

const int N = 500007, M = 5000007, INF = 0x3f3f3f3f;

int main()
{
    
    
    int n;
    while(~scanf("%d", &n))
        printf("%d\n", (int)log2(n) + 1);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45697774/article/details/108696977
law