zoukankan      html  css  js  c++  java
  • 1003D. Coins and Queries

    #include<bits/stdc++.h>
    using namespace std;
    map<int,int> ans;
    int a[50];
    int trans(int num)
    {
        int ans=0;
        while(num>1)
        {
            num/=2;
            ans++;
        }
        return ans;
    }
    int main()
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
        {
           int tmp;
           scanf("%d",&tmp);
           //printf("%d
    ",trans(tmp));
           ans[trans(tmp)]++;
        }
        while(q--)
        {
            int b;
            scanf("%d",&b);
            int tot=0;
            while(b)
            {
                   a[tot++]=(b%2);
                   b/=2;
            }
            int now=0;
            int res=0;
            for(int i=tot-1;i>=0;i--)
            {
                //printf("%d ",a[i]);
                now+=a[i];
                if(ans[i]<now)
                {
                    now-=ans[i];
                    res+=ans[i];
                    now=now*2;
                }
                else
                {
                    res+=now;
                    now=0;
                }
            }
            //printf("
    ");
            if(now) printf("-1
    ");
            else printf("%d
    ",res);
        }
    }
  • 相关阅读:
    10000000000
    vue生命周期
    react基础
    第一个react
    vuex状态管理2
    vue配合UI组件
    vuex
    vue-router配合vue-cli的实例
    vue-router2.0
    父子组件2.0
  • 原文地址:https://www.cnblogs.com/lishengkangshidatiancai/p/10340878.html
Copyright © 2011-2022 走看看