1438. Shopaholic
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Lindsay is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this disease, but try to limit its effect on her wallet. You have realized that the stores coming with these offers are quite selective when it comes to which items you get for free; it is always the cheapest ones. As an example, when your friend comes to the counter with seven items, costing 400, 350, 300, 250, 200, 150, and 100 dollars, she will have to pay 1500 dollars. In this case she got a discount of 250 dollars. You realize that if she goes to the counter three times, she might get a bigger discount. E.g. if she goes with the items that costs 400, 300 and 250, she will get a discount of 250 the first round. The next round she brings the item that costs 150 giving no extra discount, but the third round she takes the last items that costs 350, 200 and 100 giving a discount of an additional 100 dollars, adding up to a total discount of 350. Your job is to find the maximum discount Lindsay can get.
Input
The first line of input gives the number of test scenarios, 1 ≤ t ≤ 20. Each scenario consists of two lines of input. The first gives the number of items Lindsay is buying, 1 ≤ n ≤ 20000. The next line gives the prices of these items, 1 ≤ pi ≤ 20000.
Output
For each scenario, output one line giving the maximum discount Lindsay can get by selectively choosing which items she brings to the counter at the same time.
Sample Input
1
6
400 100 200 350 300 250
Sample Output
400
分析:
排序,隔三求和,要注意的是从大到小而不是从小到大
#include <iostream>
#include <vector>
#include <algorithm> using namespace std; bool cmp(int a, int b) {
return a >= b;
} int main(int argc, char const *argv[])
{
int testNum;
int itemNum;
int itemValue;
vector<int> items;
cin >> testNum;
while (testNum--) {
cin >> itemNum;
items.resize(itemNum);
for (int i = ; i != itemNum; ++i) {
cin >> itemValue;
items[i] = itemValue;
}
sort(items.begin(), items.end(), cmp);
int maxDiscount = ;
itemNum /= ;
for (int i = ; i < itemNum; ++i) {
maxDiscount += items[ * i + ];
}
cout << maxDiscount << endl;
}
return ;
}
1438. Shopaholic的更多相关文章
- 【HDOJ】1438 钥匙计数之一
状态压缩.分最后一个槽的值以及当前的配置方案是否可以进行DP. /* 1438 */ #include <cstdio> #include <cstring> #include ...
- HDOJ(HDU) 1678 Shopaholic
Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you can ...
- P - Shopaholic
P - Shopaholic Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- poj 1515+poj 1438(边双连通)
题目链接:http://poj.org/problem?id=1515 思路:题目的意思是说将一个无向图改成有向图,使其成为强连通,输出所有的边.我们可以求无向图的边双连通分量,对于同一个双连通分量, ...
- 九度oj 题目1438:最小公倍数
题目1438:最小公倍数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2451 解决:2057 题目描述: 给定两个正整数,计算这两个数的最小公倍数. 输入: 输入包含多组测试数据,每 ...
- ZOJ 2883 Shopaholic【贪心】
解题思路:给出n件物品,每买三件,折扣为这三件里面最便宜的那一件即将n件物品的价值按降序排序,依次选择a[3],a[6],a[9]----a[3*k] Shopaholic Time Limit: 2 ...
- 51nod 1438:方阵与完全平方数
1438 方阵与完全平方数 题目来源: mostleg 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 如果一个由正整数组成的n*n的方阵,满足以下 ...
- 【九度OJ】题目1438:最小公倍数 解题报告
[九度OJ]题目1438:最小公倍数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1438 题目描述: 给定两个正整数,计 ...
- POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心
题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...
随机推荐
- Chrome查看html样式基本操作-div
1. div 标签(白板,块级标签),想学会前端,只需要学会div+css就可以了. span 标签 (白板,行内标签) 2. Chrome审查元素的使用.右击浏览器--检查---可以看标签是块级还是 ...
- 生成模型(Generative Model)Vs 判别模型(Discriminative Model)
概率图分为有向图(bayesian network)与无向图(markov random filed).在概率图上可以建立生成模型或判别模型.有向图多为生成模型,无向图多为判别模型. 判别模型(D ...
- BZOJ2588 Count on a tree 【树上主席树】
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Submit: 7577 Solved: 185 ...
- POJ1201:Intervals(差分约束)
差分约束经典题.设s[i]为前缀和,则有 s[i]-s[i-1]<=1 (i往i-1连-1的边) s[i]>=s[i-1] (i-1往i连0的边) s[b]-s[a-1]>=c (a ...
- [zhuan]Android程序的真正入口Application
http://blog.csdn.net/coding_or_coded/article/details/6602560 接触android已经有一段时间了,一直以为android程序的入口是配置文件 ...
- JavaScript中字符串与16进制之间的转换
一.字符串转换为16进制 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- HDU2647 topsort
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- HDU1083 :Courses(二分图匹配)
Cources Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- c++模板类编译错误
最近想写一个c++模板类,实现一个有向图.依据惯例,类在头文件中声明,类的实现写在源文件中.可是编译的时候出现了如下错误: undefined reference to 通过谷歌发现,这是一个很常见的 ...
- stout代码分析之四:Try类
stout的在异常捕获上遵循于谷歌类似的原则,不适用try...catch...,而是从函数返回值判断异常.Try类正是实现了这样的一个功能. 同Option一样,Try是一个模板类,每个类对象都有两 ...