Блог пользователя GDivyanshu3012

Автор GDivyanshu3012, история, 5 часов назад, По-английски

I am trying to solve this problem. PROBLEM LINK

But I can't figure out why I am getting WA in test 2. I cannot exactly find out the exact test case on which my code fails because its the 485th TC and CF isn't showing it.

My logic is this:

My logic:

We want to make the current element to be greater than or equal to prev element To do this, let's say we raise the curr element to some power. Mathematically,

=> curr * 2^pow >= prev = 2^pow >= prev/curr

Take log base 2 on both sides.

pow >= log2(prev) — log2(curr);

this means, pow = ceil(log2(prev) — log2(curr))

My code:

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>


using namespace std;


typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define int long long
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define endl "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())



void solve(){
    int n;cin>>n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin>>a[i];

    int ans = 0;
    int prevpow = 0;
    for(int i = 1; i < n; i++){
        int curr = a[i];
        int prev = pow(2, prevpow) * a[i-1];
        if(curr >= prev){
            prevpow = 0;
            continue;
        }
        // y < x
        prevpow = ceil(log2(prev) - log2(curr));
        ans += prevpow;
    }
    cout<<ans<<endl;
}


int32_t main(){
    fast_cin();
    ll t;
    cin >> t;
    for(int it=1;it<=t;it++) {
        solve();
    }
    return 0;
}


[submission:272673906]
  • Проголосовать: нравится
  • -4
  • Проголосовать: не нравится

»
5 часов назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

since the elements are ranging to 10^9, it will be a matter of few indices that this line int prev = pow(2, prevpow) * a[i-1]; will be out of integer range

  • »
    »
    3 часа назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I am using macro : #define int long long. Even then it gives WA.

»
3 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Your code will not work, as it will overflow on a test like 1e9, 5e8-1, 2.5e8-3, .... One wokring solution is similar to yours, with some justification. Let the $$$p$$$ be array, where $$$p[i]$$$ is the number of operations you make on the $$$i$$$th element. Then, $$$a[i-1] \cdot 2^{p[i-1]} <= a[i] \cdot 2^{p[i]}$$$, and your task is to minimize $$$p[i]$$$, note that $$$p[i]$$$ can't be negative. First, set $$$p[i] = p[i-1]$$$, then if $$$a[i-1] <= a[i]$$$, divide $$$a[i]$$$ by 2 as many times as you can until either $$$p[i] = 0$$$ or $$$a[i-1] <= a[i]$$$. Otherwise, multiply $$$a[i]$$$ by 2 until $$$a[i-1] <= a[i]$$$. The answer is sum of $$$p$$$.