1. 程式人生 > >UPC——7226: Memory Banks(貪心+細節處理)

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

來源/分類

#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;
}