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 --三维凸包,求多面体重心
题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...
随机推荐
- 【bzoj4641】基因改造 特殊匹配条件的KMP
题目描述 如果两个长度相等的字符串,如果存在一种字符的一一映射,使得第一个字符串的所有字符经过映射后与第二个字符串相同,那么就称它们“匹配”.现在给出两个串,求第一个字符串所有长度等于第二个字符串的长 ...
- 在@Async注解下RequestContextHolder.getRequestAttributes() 获得null的情况
我们有的时候会在service层获取request填充一些诸如用户名和IP地址等信息,这个时候如果不想从Controller层传request,可以在service直接使用 HttpServletRe ...
- 【题解】CF#1012 C-Hill
感觉这题的状态还是比较明显的.设置状态 \(f[i][j][0/1]\) 表示dp到第 \(i\) 个位置,前面(包括这里)已经出现了 \(j\) 个山峰,当前位置是不是山峰即可 dp.这样的状态有一 ...
- [JXOI2017]颜色 线段树扫描线 + 单调栈
---题面--- 题解: 首先题目要求删除一些颜色,换个说法就是要求保留一些颜色,那么观察到,如果我们设ll[i]和rr[i]分别表示颜色i出现的最左边的那个点和最右边的那个点,那么题目就是在要求我们 ...
- BZOJ5340 & 洛谷4564 & LOJ2552:[CTSC2018]假面——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5340 https://www.luogu.org/problemnew/show/P4564 ht ...
- BZOJ3675 & 洛谷3648 & UOJ104:[Apio2014]序列分割——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3675 https://www.luogu.org/problemnew/show/P3648 ht ...
- PHP 数据库防止攻击
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...
- Linux试题
1.编写脚本,统计/etc./usr./var目录中有多少个一级子目录和文件 #!/bin/bash # danran # time is Mon Jun 5 13:09:12 CST 2017 li ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- simpleDateFormat的 学习
http://blog.csdn.net/qq_27093465/article/details/53034427