自学内容网 自学内容网

位运算+贪心,CF1995 C - Squaring

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

C - Squaring


二、解题报告

1、思路分析

和CF1883E一样的题目:https://codeforces.com/contest/1883/problem/E

那道题的操作是a[i] * 2

这道题的操作是a[i]^2

对于2 ^ k,其平方相等于 k * 2

也就是说 a[i] ^ 2相当于其2的对数 * 2,所以把那道题的代码搬过来仍然能过,只要把a[i] * 2换成平方即可

那道题的题解:CF1883 E - Look Back-CSDN博客

2、复杂度

时间复杂度: O(N log(logN))空间复杂度:O(N)

3、代码详解

 ​
#include <bits/stdc++.h>
#define sc scanf
using i64 = long long;
using i128 = __int128;
using PII = std::pair<int, int>;
constexpr int inf32 = 1e9 + 7;
constexpr i64 inf64 = 1e18 + 7;
constexpr int P = 998244353;
constexpr double eps = 1e-6;

// #define DEBUG

void solve()
{
    int n;
    std::cin >> n;
    std::vector<i64> a(n);
    for (int i = 0; i < n; ++ i) std::cin >> a[i];
    int i = 0;
    for (; i < n && a[i] == 1; ++ i) ;
    i64 res = 0;    
    ++ i;
    for (i64 s = 0; i < n; ++ i) {
        if (a[i] == 1) {
            res = -1;
            break;
        }
        int c = 0;
        i64 x = a[i];
        while (a[i - 1] > x)
            x *= x, ++ c;
        x = a[i - 1];
        while (s > 0 && a[i] >= x * x)
            x *= x, -- s;

        s += c;
        res += s;
    }

    std::cout << res << '\n';
}

int main()
{
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    int _ = 1;
    std::cin >> _;
    while (_--)
        solve();
    return 0;
}


原文地址:https://blog.csdn.net/EQUINOX1/article/details/140659833

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!