zoukankan      html  css  js  c++  java
  • 训练赛-Building Numbers

    题意:首先告诉你,一个数字从1开始有两种变换方式:1.当前数字的值加1

    2.当前的数字值乘2;

    思路:首先把数组里的数字需要的变换次数算出来,然后用前缀和解决;

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    #define ll long long
    using namespace std;
    ll a[100005];ll sum[100005];ll ans[100005];
    int main()
    {
        int t;
        int n;
        int q;
        scanf("%d",&t);
        int l,r;
        while(t--)
        {
            memset(sum,0,sizeof(sum));
            memset(ans,0,sizeof(ans));
            scanf("%d%d",&n,&q);
            for(int i=1;i<=n;i++)
            {
                scanf("%lld",&a[i]);
                ll x=a[i];
                if(x==1)
                {
                    ans[i]=0;continue;
                }
                while(x>1)
                {
                   if((x&1)==1)
                   {
                        x--;ans[i]++;
                   }
                   else
                   {
                       x=x/2;ans[i]++;
                   }
                }
            }
            for(int i=1;i<=n;i++)
                sum[i]=sum[i-1]+ans[i];
            while(q--)
            {
                scanf("%d%d",&l,&r);
                if(l==r)
                    printf("%lld
    ",ans[l]);
                else
                    printf("%lld
    ",sum[r]-sum[l-1]);
            }
        }
        return 0;
    }
  • 相关阅读:
    chapter 12_1 数据文件
    chapter11_3 字符串缓冲
    chapter11_2 Lua链表与队列
    chapter11_1 Lua数组、列表
    chapter9_4 非抢占式的多线程
    Java设计模式
    java内存回收机制
    javaIO流概述
    java集合概述
    java多线程
  • 原文地址:https://www.cnblogs.com/huangdao/p/8343129.html
Copyright © 2011-2022 走看看