VK Cup 2015
来源:互联网 发布:mac 草图大师 卡 编辑:程序博客网 时间:2024/04/27 12:40
题意:给n种面值的钱币,然后有q个询问,每次询问给出一个x,问说能不能在不超过k张钱币,不超过2种面值的情况下,凑出x块钱,如果能输出最少的钱币数,如果不能输出-1;
第一眼不知道为什么我居然想用背包做,然后T了,然后又暴力了一下,又T了,然后又写了一个姿势很优雅的二分查找,还是T了。最后用map瞎搞搞出来,先n*k把一个面值的钱凑算出来,map[]存这个钱数所对应的最少钱币张数,然后对于每个金额y我们查询一下y - x ,然后找钱币数最少的那个就行。
#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<set>#include<map>#include<time.h>#include<cstdio>#include<vector>#include<stack>#include<queue>#include<iostream>using namespace std;#define LONG long longconst int INF=0x3f3f3f3f;const int MOD=1e9+7;const double PI=acos(-1.0);#define clrI(x) memset(x,-1,sizeof(x))#define clr0(x) memset(x,0,sizeof x)#define clr1(x) memset(x,INF,sizeof x)#define clr2(x) memset(x,-INF,sizeof x)#define EPS 1e-10int a[6000];int n ;int K ;int main(){ cin>>n>>K; for(int i =1; i<= n ;++i) scanf("%d",&a[i]); sort(a + 1 , a + n + 1); int q; map <int,int > mp; mp.clear() ; for(int i =1; i <=n ;++ i) for(int j = 0 ; j <= K ;++ j) if(mp[a[i]*j] == 0) mp[a[i] * j] = j; else mp[a[i] * j] = min(mp[a[i] * j] , j); cin>>q; while(q--) { int x ; cin>>x; int ans = INF; for(int i =1; i<=n ;++ i) for(int j = 0; j <= K; ++ j) if(mp[x - a[i] * j ]!= 0 || x == a[i] * j ) ans = min(ans , j + mp[x - a[i] * j]); if(ans > K) cout<<-1<<endl; else cout<<ans<<endl; }}
1 0
- VK Cup 2015
- Codeforces VK Cup 2015
- VK Cup 2015
- VK Cup 2015
- [VK Cup 2015 - Round 1]简要题解
- VK Cup 2015 - Finals, online mirror
- 51Nod 1524 | Codeforces VK Cup 2015
- 【VK Cup 2012】final
- Codeforences #351 VK CUP
- VK Cup 2017
- Codeforces VK Cup 2017
- [Codeforces VK Cup 2016
- VK Cup 2017
- VK Cup 2017
- VK Cup 2017
- VK Cup 2017
- VK Cup 2015 - Qualification Round 2 C. Name Quest
- VK Cup 2015 - Qualification Round 1 B. Photo to Remember
- 文章标题
- Lambda表达式
- 作为客户端调用WebService服务java代码详解。
- h5 form表单新增属性
- spring 事物配置整理加工
- VK Cup 2015
- Android studio 添加 android.permission.ACCESS_MOCK_LOCATION 权限无法打包
- $.ajax()方法详解
- 利用递归下降分析法求解简单正整数四则运算
- 安卓 JNI fork 监听程序被卸载
- ApiDemo-AnimationSeeking
- JDBC,ORM,JPA,Hibernate
- ORA-32004 obsolete or deprecated parameter(s) specified for RDBMS instance
- Thinkphp3.2.3 ----后台----图片上传相应处理