HackerRank Algorithms-Search-Flowers 贪心
来源:互联网 发布:js设置div宽度和高度 编辑:程序博客网 时间:2024/06/06 12:26
题目点此
题意:K个小伙伴买N个多花,老板很奇葩,不想买太多花,如果有人已经买过x朵花了,那么买下一朵花就要付x+1倍的价格。给出每朵花的价格,求最少花多少钱都把花买下。
分析:每个小伙伴买花时肯定是先买原价贵的,不然就吃亏了是不是。所以贪心策略就是把价格排序下,先让每个小伙伴把一血献给最贵的那几朵花,每次都买剩下的花最贵的那几朵,这样就是最优的了。
一遍遍历即可,复杂度为O(n)。
代码:
/* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.net/hcbbt * File: Flowers.cpp * Lauguage: C/C++ * Create Date: 2013-09-06 15:46:01 * Descripton: flowers */#include <cstdio>#include <algorithm>using namespace std;#define rep(i, n) for (int i = 0; i < (n); i++)typedef long long LL;typedef unsigned long long ULL;const int MAXN = 110;int a[MAXN];int n, k;LL sum = 0;bool cmp(int a, int b) {return a > b;}int main() {scanf("%d%d", &n, &k);rep(i, n) scanf("%d", &a[i]);sort(a, a + n, cmp);rep(i, n) sum += a[i] * (i / k + 1);printf("%lld\n", sum);return 0;}
- HackerRank Algorithms-Search-Flowers 贪心
- HackerRank Algorithms-Search-Pairs 数组的固定差对数
- interviewstreet - Flowers 类别Search
- Flowers hdu1587 贪心算法
- Protecting the Flowers (贪心)
- Lesson 3 Search algorithms
- 贪心算法(Greedy Algorithms)
- sdibt 1995 Flowers (贪心)
- poj3262 Protecting the Flowers(贪心)
- POJ3262 Protecting the Flowers 【贪心】
- POJ3262 Protecting the Flowers(贪心)
- [贪心] poj3262 Protecting the Flowers
- 【POJ3262】Protecting the Flowers【贪心】
- POJ3262 Protecting the Flowers (贪心)
- HackerRank Breadth First Search: Shortest Reach
- [Hackerrank题目选做] Team Formation 贪心
- Algorithms—79.Word Search
- hackerRank
- 获取登陆的ip
- Servlet是如何实现MVC的
- 关于Tomcat的URIEncoding以及GET乱码
- ANDRIOD GLOSSARY
- 浅谈HTTP中get与Post的区别
- HackerRank Algorithms-Search-Flowers 贪心
- 关于IIS的IWAM_和IUSR_用户的密码
- 【.R】Android R.java文件丢失的找回
- 美国面试之旅——面试经验分享(ZZ)
- Android 百度地图开发(一)---申请API Key和在项目中显示百度地图
- Android开发:检测耳机的插入状态
- 【前无古人后无来者的反调试方法】
- 【layout】Binary XML file line #23: You must supply a layout_width attribute.
- Hibernate Entity to mysql( long text)