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 --三维凸包,求多面体重心
题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...
随机推荐
- 【bzoj1704】[Usaco2007 Mar]Face The Right Way 自动转身机 贪心
题目描述 农夫约翰有N(1≤N≤5000)只牛站成一排,有一些很乖的牛朝前站着.但是有些不乖的牛却朝后站着.农夫约翰需要让所有的牛都朝前站着.幸运的是约翰最近买了一个自动转身机.这个神奇的机器能使K( ...
- BZOJ1500:[NOI2005]维修数列——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1500 https://www.luogu.org/problemnew/show/P2042#su ...
- BZOJ2553 [BeiJing2011]禁忌 【AC自动机 + dp + 矩乘优化】
题目链接 BZOJ2553 题解 话说在前,此题卡精度,最好开long double 先建\(AC\)自动机 求期望,逆着求,设\(f[i][j]\)为长度为\(i\)的串,当前匹配AC自动机\(j\ ...
- 卡特兰数(Catalan Number) 学习笔记
一.三个简单的问题 1.给定一串长为2n的01序列,其中0和1的数量相等,满足任意前缀中0的个数不少于1的个数,求序列的个数 2.给出一串长为n的序列,按顺序将他们进栈,随意出栈,求最后进出栈的方案 ...
- finetune on caffe
官方例程:http://caffe.berkeleyvision.org/gathered/examples/finetune_flickr_style.html 相应的中文说明:http://blo ...
- 在局域网中基于Windows文件共享的git环境搭建
本文的思想是在局域网中用一台电脑作为服务器,在其中建立一个文件夹,作为总的公开版本库.然后将这个文件夹共享,使其他客户机都可以访问,从而进行代码的管理. 一.下载安装文件 1.git核心: git-f ...
- Uva-oj Palindromes 暴力
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- Wand FZU - 2282 全错位重排
N wizards are attending a meeting. Everyone has his own magic wand. N magic wands was put in a line, ...
- TextToast -- 自定义Toast源码
import android.content.Context;import android.graphics.Color;import android.graphics.PixelFormat;imp ...
- 001 Python中的变量和字符串
1.Python“变量”更像“名字” 变量名就像我们现实社会的名字,把一个值赋值给一个名字时,Ta会存储在内存中,称之为变量(variable). 在大多数语言中,都把这种行为称为“给变量赋值”或“把 ...