贪心 + DFS
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.
Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci roubles. The number of bottles of each type in the store can be considered infinite.
You want to buy at least L liters of lemonade. How many roubles do you have to spend?
Input
The first line contains two integers n and L (1 ≤ n ≤ 30; 1 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.
The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.
Output
Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.
Example
4 12
20 30 70 90
150
4 3
10000 1000 100 10
10
4 3
10 100 1000 10000
30
5 787787787
123456789 234567890 345678901 456789012 987654321
44981600785557577
Note
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.
In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.
In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.
题意 : 给你 n 个物品,以及一个容器的体积 l , n 个物品的体积是 2^i-1 , 求在超过容器体积的前提下,最小的花费是多少。
思路分析 : 想了一个贪心策略,优先去贪性价比最高的物品,当恰好装下的时候,此时可以记录一下答案,若不能时,此时可以让他们多装一个,再次记录一下答案,深搜就行了
代码示例:
/*
* Author: parasol
* Created Time: 2018/3/7 18:18:10
* File Name: 2.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
#define ll long long
const ll maxn = 1e6+5;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f; struct node
{
ll l, c;
double p;
bool operator< (const node &v)const{
return p < v.p;
}
}pre[35];
ll n, l;
ll ans = __LONG_LONG_MAX__; void dfs(ll x, ll cost, ll sum){
if (cost >= ans) return;
if (sum <= 0) {ans = min(ans, cost); return;}
if (x == n+1) return;
ll f = sum / pre[x].l;
int pt = 0;
if (sum%pre[x].l == 0){
ans = min(ans, cost+f*pre[x].c);
return;
}
else {
dfs(x+1, cost+(f+1)*pre[x].c, sum-(f+1)*pre[x].l);
pt = 1;
}
if (pt) {
dfs(x+1, cost+f*pre[x].c, sum-f*pre[x].l);
}
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> l;
ll an = 1;
for(ll i = 1; i <= n; i++){
scanf("%lld", &pre[i].c);
pre[i].l = an;
pre[i].p = 1.0*pre[i].c/an;
an *= 2;
}
sort(pre+1, pre+1+n);
dfs(1, 0, l);
printf("%lld\n", ans);
return 0;
}
贪心 + DFS的更多相关文章
- 小黑的镇魂曲(HDU2155:贪心+dfs+奇葩解法)
题目:点这里 题目的意思跟所谓的是英雄就下100层一个意思……在T秒内能够下到地面,就可以了(还有一个板与板之间不能超过H高). 接触这题目是在昨晚的训练赛,当时拍拍地打了个贪心+dfs,果断跟我想的 ...
- 【bzoj3252】攻略 贪心+DFS序+线段树
题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某 ...
- hdu6060[贪心+dfs] 2017多校3
/* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...
- UVALive3902 Network[贪心 DFS&&BFS]
UVALive - 3902 Network Consider a tree network with n nodes where the internal nodes correspond to s ...
- 【NOIP2003】传染病控制(-贪心/dfs)
我自己yy了个贪心算法,在某oj 0msAC~.然后去wikioi提交,呵呵,原来是之前oj的数据太弱给我水过了,我晕. 我之前的想法是在这棵树上维护sum,然后按时间来割边,每一时刻割已经感染的人所 ...
- HDU 5802 Windows 10 (贪心+dfs)
Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...
- Cut 'em all! CodeForces - 982C(贪心dfs)
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...
- Too Rich(贪心+DFS)
Too Rich http://acm.hdu.edu.cn/showproblem.php?pid=5527 Time Limit: 6000/3000 MS (Java/Others) Me ...
- BZOJ3252 攻略(贪心+dfs序+线段树)
考虑贪心,每次选价值最大的链.选完之后对于链上点dfs序暴力修改子树.因为每个点最多被选一次,复杂度非常正确. #include<iostream> #include<cstdio& ...
- UVA 10123 No Tipping (物理+贪心+DFS剪枝)
Problem A - No Tipping As Archimedes famously observed, if you put an object on a lever arm, it will ...
随机推荐
- Codeforces Round #181 (Div. 2)
A. Array 模拟. B. Coach 模拟. C. Beautiful Numbers good number的位和最大不超过\(10^7\),那么只要枚举a或b的个数,然后最多循环7次判断位和 ...
- chrome浏览器频繁卡死
输入chrome:flag 把对应的GPU选项关掉 或者重装 ,从360软件管理器上装
- 【t093】外星密码
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有了防护伞,并不能完全避免2012的灾难.地球防卫小队决定去求助外星种族的帮助.经过很长时间的努力,小 ...
- SpringSide 3 中的安全框架
在SpringSide 3的官方文档中,说安全框架使用的是Spring Security 2.0.乍一看,吓了我一跳,以为Acegi这么快就被淘汰了呢.上搜索引擎一搜,发现原来Spring Secur ...
- 21个项目玩转深度学习:基于TensorFlow的实践详解03—打造自己的图像识别模型
书籍源码:https://github.com/hzy46/Deep-Learning-21-Examples CNN的发展已经很多了,ImageNet引发的一系列方法,LeNet,GoogLeNet ...
- 2019-8-31-git-通过-SublimeMerge-处理冲突
title author date CreateTime categories git 通过 SublimeMerge 处理冲突 lindexi 2019-08-31 16:55:59 +0800 2 ...
- 浏览器的内核分别是什么?经常遇到的浏览器的兼容性有哪些?原因,解决方法是什么,常用hack的技巧 ?
* IE浏览器的内核Trident. Mozilla的Gecko.google的WebKit.Opera内核Presto: * png24为的图片在iE6浏览器上出现背景,解决方案是做成PNG8. * ...
- egg-socket在egg中的使用
WebSocket 的产生源于 Web 开发中日益增长的实时通信需求,对比基于 http 的轮询方式,它大大节省了网络带宽,同时也降低了服务器的性能消耗: socket.io 支持 websocket ...
- 两种方法,轻松上手ConfigMap!
属性配置文件在任何应用程序中都非常重要.它们不仅可以让应用程序具备灵活性,还能够根据文件中配置的值产生不同的功能.实际上,在staging.开发.测试.UAT或生产环境中,我们都使用属性配置文件来驱动 ...
- js 处理json
json 分为两种结构 数组 对象 对象 { } 对象里的键值对 1.键值对之间用冒号链接 2.键必须用“”包裹 3.值如果是字符串 就用“”包裹 如果是数字 则不需要 4.键值对 ...