interviewstreet - Flowers 类别Search
来源:互联网 发布:社交数据 编辑:程序博客网 时间:2024/05/21 19:07
题目来源:https://www.interviewstreet.com/challenges/dashboard/#problem/4fd05444acc45、
解题报告:
把花的价钱进行排序,那么K个人一定最先买最贵的K朵花,然后依次往便宜的去买。
#include <iostream>#include <algorithm>using namespace std;int c[100];int main(){ int N, K; cin >> N >> K; for (int i = 0; i < N; i++) { cin >> c[i]; } sort(c, c+N); long long sum = 0; int x = 1; for (int i = N-1; i >=0; i -= K) { for (int j = i; j >= 0 && j > i-K; j--) sum += x * c[j]; x++; } cout << sum << endl;}
附录:
You and your K-1 friends want to buy N flowers. Flower number i has host ci. Unfortunately the seller does not like a customer to buy a lot of flowers, so he tries to change the price of flowers for customer who had bought flowers before. More precisely if a customer has already bought x flowers, he should pay (x+1)*ci dollars to buy flower number i.
You and your K-1 firends want to buy all N flowers in such a way that you spend the as few money as possible.
Input:
The first line of input contains two integers N and K.
next line contains N positive integers c1,c2,...,cN respectively.
Output:
Print the minimum amount of money you (and your friends) have to pay in order to buy all n flowers.
Sample onput :
3 3
2 5 6
Sample output :
13
Explanation :
In the example each of you and your friends should buy one flower. in this case you have to pay 13 dollars.
Constraint :
1 <= N, K <= 100
Each ci is not more than 1000,000
- interviewstreet - Flowers 类别Search
- interviewstreet-pairs --类别search
- interviewstreet-median -类别search
- interviewstreet-string similarity - 类别-string process
- HackerRank Algorithms-Search-Flowers 贪心
- InterviewStreet --Candies
- interviewstreet pair
- flowers
- Flowers
- Flowers
- interviewstreet - even tree
- InterviewStreet / Challenges / Stock Trading
- InterviewStreet题目(1)
- InterviewStreet题目(2)
- InterviewStreet题目(3)
- 96. Unique Binary Search Trees 类别:动态规划 难度:medium
- 类别
- 类别
- VC++调试技巧
- OHAS无法启动的处理过程
- android 自定义核心服务
- PL/SQL 配置
- 在 Visual Studio 2012 中创建 ASP.Net Web Service
- interviewstreet - Flowers 类别Search
- 11GR2单实例安装(ASM)
- Linux 创建SWAP分区
- 图论——拓扑排序
- 二叉树/二叉搜索树的操作汇集(c++)
- 2012.9.16
- linux常用+好用指令笔记(持续更新)
- JVM中的Hello World是如何运行的?
- C和C++的区别