Too Rich ICPC 2015 Changchun A dfs+思维

2023-10-04 02:30
文章标签 思维 dfs icpc 2015 changchun rich

本文主要是介绍Too Rich ICPC 2015 Changchun A dfs+思维,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

9255: Too Rich

时间限制: 1 Sec  内存限制: 128 MB
提交: 100  解决: 22
[提交] [状态] [讨论版] [命题人:admin]

题目描述

You are a rich person, and you think your wallet is too heavy and full now. So you want to give me some money by buying a lovely pusheen sticker which costs p dollars from me. To make your wallet lighter, you decide to pay exactly p dollars by as many coins and/or banknotes as possible.
For example, if p = 17 and you have two $10 coins, four $5 coins, and eight $1 coins, you will pay it by two $5 coins and seven $1 coins. But this task is incredibly hard since you are too rich and the sticker is too expensive and pusheen is too lovely, please write a program to calculate the best solution.

 

输入

The first line contains an integer T indicating the total number of test cases. Each test case is a line with 11 integers p, c1 , c5 , c10 , c20 , c50 , c100 , c200 , c500 , c1000 , c2000 , specifying the price of the pusheen sticker, and the number of coins and banknotes in each denomination. The number c i means how many coins/banknotes in denominations of i dollars in your wallet.
1≤T≤20000
0≤p≤109
0≤c i≤100000

 

输出

For each test case, please output the maximum number of coins and/or banknotes he can pay for exactly p dollars in a line. If you cannot pay for exactly p dollars, please simply output ‘-1‘.

 

样例输入

3
17 8 4 2 0 0 0 0 0 0 0
100 99 0 0 0 0 0 0 0 0 0
2015 9 8 7 6 5 4 3 2 1 0

 

样例输出

9
-1
36

 

来源/分类

ICPC 2015 Changchun 

 

[提交] [状态]

遗憾,比赛时想到了正确的解法,但是种种原因,最后没写。。。

题意:有十种面额的硬币的对应的个数,给一个钱数n,求恰好等于n且硬币个数最多的结果

开始直接写了贪心,然后发现贪不过去

样例在这里:250 1 0 0 3 1 0 1 0 0 0  答案是2,贪心是-1

                     190 0 0 0 5 100 0 0 0 0 0  答案是5,贪心应该还是-1

然后想了下dfs,但是细节不太会写

思路:算出所给的所有的钱数,减去要求的n,接着用这个减去的值m求硬币个数最少的情况,最后相减就可以

这个dfs好写一些,但是一直处在dfs能看懂但自己写不出来的状态。。。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50;
const int inf=0x3f3f3f3f;
ll num[20];
ll val[20]={0,1,5,10,20,50,100,200,500,1000,2000};
ll use[20];
ll ans;
void dfs(int x,ll sum,ll cnt){if(!sum){ans=min(ans,cnt);return ;}if(x<1) return ;use[x]=min(sum/val[x],num[x]);dfs(x-1,sum-val[x]*use[x],cnt+use[x]);if(use[x]>=1){use[x]--;dfs(x-1,sum-val[x]*use[x],cnt+use[x]);}
}
int main()
{int t;scanf("%d",&t);while(t--){memset(use,0,sizeof(use));ll p,sum=0;ll cnt=0;scanf("%lld",&p);for(int i=1; i<=10; i++){scanf("%lld",&num[i]);sum+=num[i]*val[i];cnt+=num[i];}sum-=p;if(sum<0){printf("-1\n");continue;}ans=inf;dfs(10,sum,0);if(ans==inf){printf("-1\n");}else printf("%lld\n",cnt-ans);}return 0;
}

 

这篇关于Too Rich ICPC 2015 Changchun A dfs+思维的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1134

相关文章

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访

颠覆你的开发模式:敏捷思维带来的无限可能

敏捷软件开发作为现代软件工程的重要方法论,强调快速响应变化和持续交付价值。通过灵活的开发模式和高效的团队协作,敏捷方法在应对动态变化和不确定性方面表现出色。本文将结合学习和分析,探讨系统变化对敏捷开发的影响、业务与技术的对齐以及敏捷方法如何在产品开发过程中处理持续变化和迭代。 系统变化对敏捷软件开发的影响 在敏捷软件开发中,系统变化的管理至关重要。系统变化可以是需求的改变、技术的升级、

nyoj99(并查集+欧拉路+dfs)

单词拼接 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串。 前一个单词的结尾应该与下一个单词的道字母相同。 如 aloha dog arachnid gopher tiger rat   可以拼接成:aloha.arachnid.dog.gopher.rat.tiger 输入 第一行是一个整

【CF】E. Anya and Cubes(双向DFS)

根据题意的话每次递归分3种情况 一共最多25个数,时间复杂度为3^25,太大了 我们可以分2次求解第一次求一半的结果,也就是25/2 = 12,记录结果 之后利用剩余的一半求结果 s-结果 = 之前记录过的结果 就可以 时间复杂度降低为 3 ^ (n/2+1) 题目链接:http://codeforces.com/contest/525/problem/E #include<set

力扣 797. 所有可能路径【DFS】

1. 题目 2. 代码 DFS , 直接见代码 class Solution {public:vector<int> path;vector<vector<int>> res; // 结果集void dfs(vector<vector<int>>& graph, int cur, int n){// 找出所有从节点 0 到节点 n-1 的路径// 下标从 0 开始的if (

CF Bayan 2015 Contest Warm Up B.(dfs+暴力)

B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/475/probl