跳到正文

题解

P15256

Gaochenxi103_QAQ约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年2月11日
收录
2026年2月11日
更新
2026年11月7日
篇幅
约 1 分钟
题目
P15256

版本

  1. v1当前

做法

考虑贪心的选择,二进制下从低位到高位依次统计性价比最高的折扣;如果当前的折扣价格比之前累计的价格低,那么就选择这次折扣。显然二进制低位全为 11 低情况代表的数量之和都小于当前的。

最后如果存在一种折扣可以直接满足所有的,选择最小价格的一个即可。(注意折扣的价格单调不递减)

代码

赛时代码
C++
#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;
}
原文
存档评论1 条抓取于