codeforces 1003d Coins and Queries

本文介绍了一个使用贪心策略解决如何用最少数量的2的幂次方金币来凑成特定数值的问题。通过深度优先搜索(DFS)算法实现,并提供了完整的C++代码实现。

http://www.elijahqi.win/archives/3922
题意 让你用最少的2^x的金币凑出想要的数字那么我直接贪心即可 就尽量用大的去满足我的要求 简单dfs即可

#include<bits/stdc++.h>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) {x=x*10+ch-'0',ch=gc();}
    return x*f;
}
const int N=2e5+10;
int a[N],n,q,cnt[33],cnt1[33];
inline void gao(int x){
    int tot=0;while(x!=1) ++tot,x>>=1;
    ++cnt[tot];
}
inline int dfs(int x,int nm){
    if (!x) return 0;
    if (nm<0) return -1;int res=0;
    if((1<<nm)<=x) {
        int t=x/(1<<nm);
        t=min(t,cnt[nm]);res=dfs(x-t*(1<<nm),nm-1);
        if (res==-1) return -1;
        else return t+res;
    }
    else return dfs(x,nm-1);
}
int main(){
//  freopen("d.in","r",stdin);
    n=read();q=read();
    for (int i=1;i<=n;++i) a[i]=read();
    for (int i=1;i<=n;++i) gao(a[i]);
    for (int i=1;i<=q;++i){
        int x=read();memcpy(cnt1,cnt,sizeof(cnt));
        printf("%d\n",dfs(x,31));
    }
    return 0;
}
### 关于Codeforces Round 1003的信息 #### 比赛详情 Codeforces Round 1003是一场面向不同编程水平参赛者的在线算法竞赛。该轮比赛由多个题目组成,旨在测试选手们的逻辑思维能力和编码技巧。 #### 题目概览 虽然具体到每道题目的描述未直接提及[^1],通常情况下,这类赛事会包含若干个难度递增的问题,覆盖从简单实现到复杂数据结构与算法的应用等多个方面。对于特定编号的比赛如Round 1003而言,其题目设置也会遵循这一模式。 #### 解题报告 针对Codeforces Round 1003的具体解题思路和方法,在现有资料中并未找到确切记录[^2]。然而,基于以往的经验和其他相似轮次的比赛分析可以推测,官方或社区成员可能会发布详细的解答指南,这些资源往往包含了对各个问题深入浅出的解析以及高效的解决方案示例。 为了获取最准确完整的关于此场比赛的信息,建议访问Codeforces官方网站并查阅对应的比赛页面及其评论区,那里不仅有正式发布的题目说明,还有来自全球程序员分享的心得体会和个人见解。 ```python # 示例:如何通过Python脚本自动抓取Codeforces某一轮比赛信息(仅作为技术演示用途) import requests from bs4 import BeautifulSoup def fetch_contest_info(contest_id): url = f"https://codeforces.com/contest/{contest_id}" response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # 假设我们想要提取所有的题目名称 problem_names = [] problems_section = soup.find('div', class_='problems') # 这里假设有一个class名为'problems' if problems_section: for item in problems_section.findAll('a'): title = item.string.strip() if title and not title.isdigit(): problem_names.append(title) return problem_names print(fetch_contest_info(1003)) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值