P15256
Gaochenxi103_QAQ · · 题解
做法
考虑贪心的选择,二进制下从低位到高位依次统计性价比最高的折扣;如果当前的折扣价格比之前累计的价格低,那么就选择这次折扣。显然二进制低位全为
最后如果存在一种折扣可以直接满足所有的,选择最小价格的一个即可。(注意折扣的价格单调不递减)
代码
::::success[赛时代码]
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
int n,T,a[N];
void solve()
{
int x;cin>>x;
int lst=1,res=0,i;
for(i=0;x;i++,x>>=1)
{
if(a[lst]*(1LL<<(i+1-lst))>a[i+1] && i+1<=n)
{
lst=i+1;
}
if(res>a[i+1] && i+1<=n)res=a[i+1];
if(x%2==1)
{
res+=a[lst]*(1<<(i+1-lst));
}
}
if(i+1<=n && res>a[i+1])res=a[i+1];
cout<<res<<"\n";
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>T;
for(int i=1;i<=n;i++)cin>>a[i];
while(T--)solve();
return 0;
}
::::