UPC——7226: Memory Banks(贪心+细节处理)

7226: Memory Banks

时间限制: 1 Sec  内存限制: 256 MB
提交: 224  解决: 47
[提交] [状态] [讨论版] [命题人:admin]

题目描述

We have purchased 60 different types of memory banks, typed as 0 to 59, A bank of type i has 2i memory capacity. We have Xi memory banks of type i.
We also have n workstations numbered from 1 to n. The ith workstation needs exactly (no more and no less) Wi memory capacity to work.
Each workstation can use unlimited amount of memory banks, Total memory capacity of this workstation is the sum of capacity of all memory banks it used.
We need to make all workstations work and calculate the total capacity of unused memory banks, can you help us?

输入

Input is given from Standard Input in the following format:
X0 ... X59
n
W1 ... Wn
Constraints
0 ≤ Xi ≤ 260
1 ≤ n ≤ 100000
0 ≤ Wi ≤ 260
All of them are integers.

 

输出

Print one line denotes the answer.
If it is possible to make all workstations work, output a single number represents the total capacity of unused memory banks. It maybe very large, you should modulo 1000000007(109 + 7)
Otherwise output -1.

样例输入

4 2 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0
3
10 8 16

 

样例输出

6

 

来源/分类

2018东北四省赛 

 

#include<bits/stdc++.h>
#define mod 1000000007
#define rep(i,j,k) for(int i=j;i<k;i++)
using namespace std;
const int maxn = 1e6+5;
typedef long long ll;
ll w[maxn];
ll val[61];
bool cmp(ll a,ll b)
{
	return a>b;
}
int main(void)
{
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll num[61];
    memset(num,0,sizeof(num));
    rep(i,0,60)
    {
        ll t;
        cin>>t;
        num[i]=t;
        val[i]=1ll<<i;
    }
    int n;
    cin>>n;
    rep(i,0,n)
        cin>>w[i];
    //sort(w,w+n,cmp);
    int cnt=0;
    for(int i=59;i>=0;i--)
    {
        if(cnt==n)
            break;
        if(num[i]==0)
            continue;
        rep(j,0,n)
        {
          if(w[j]==0) continue;  
          ll des= w[j]/val[i];
          if(des==0) continue;
          if(num[i]<des) des=num[i]; 
          w[j]-=des*val[i];
          num[i]-=des;
          if(w[j]==0) 
                cnt++;
          if(num[i]==0) break;
              
        }
        
    }
    if(cnt!=n)
        cout<<"-1"<<endl;
    else
    {
        ll res=0;
        rep(i,0,60)
        {
            if(num[i]==0)
                continue;
            res +=(num[i]%mod) * (val[i]%mod)%mod;
            res%=mod;
        }
        cout<<res<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值