ZOJ 2883 Shopaholic【贪心】

解题思路:给出n件物品,每买三件,折扣为这三件里面最便宜的那一件
即将n件物品的价值按降序排序,依次选择a[3],a[6],a[9]----a[3*k]

Shopaholic


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[20005];
int cmp(int a,int b)
{return a>b;}

int main()
{
	int ncase,n,i,ans;
	scanf("%d",&ncase);
	while(ncase--)
	{
		ans=0;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
		sort(a+1,a+n+1,cmp);
		for(i=1;i<=n/3;i++)
		ans+=a[3*i];
		printf("%d\n",ans);
	}
}

  

时间: 2024-07-31 14:39:00

ZOJ 2883 Shopaholic【贪心】的相关文章

ZOJ 3829 模拟贪心

2014牡丹江现场赛水题 给出波兰式,判断其是否合法,如果不合法有两种操作: 1:任意位置加一个数字或者操作符 2:任意两个位置的元素对调 贪心模拟即可 先判断数字数是否大于操作符数,若不大于 ans+=sum2-sum1+1:新加入的数字全部放到左端. 然后从左到右遍历一遍,存储到当前位置为止,数字数和sum1,和操作数和sum2 若sum2>=1sum1,优先与队尾的数字对调,若没有则sum1++,表示在最左端加一个数字 #include "stdio.h" #include

ZOJ 3905 Cake(贪心+dp)

动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j个,所以i>=2*j, 并且每次Alice选的时候Bob已经选过了.所以当i>=2 * j的时候Alice一定能选. 所以dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + ary[i].a); dp[i - 1][j]表示Alice不选第i个,dp[i

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J

zoj 3627(贪心)

思路:半夜了思路有点混乱wa了好几发.一开始坑定两个人距离为m才能获得最大的收益,所以我们就可以枚举单个端点,当距离达到m时在一同一个方向走这是我们只需要算一下剩下几秒,左右两边贪心去最大的即可. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2

HDU 1678 Shopaholic(简单数学题 贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1678 Problem 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

zoj 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history rolling forward, our king conquered a new region in a distant continent. There are N towns (numbered from 1 to N) in this region connected by s

zoj 1543 贪心

Stripies Time Limit: 2 Seconds      Memory Limit: 65536 KB Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, but the scientists had to invent an English name

zoj 3778 Talented Chef 贪心

zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完成所有饼至少需要多少时间. 限制: 1 <= n,m,ti <= 40000 思路: 贪心 ans=max(ceil(sigma(1~n,ti)/m),max(ti)) /*zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完