2018.2.4【 HDU - 5999 】解题报告(贪心)

4 篇文章 0 订阅

The Third Cup is Free

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1833    Accepted Submission(s): 857


Problem Description
Panda and his friends were hiking in the forest. They came across a coffee bar inside a giant tree trunk.
Panda decided to treat everyone a cup of coffee and have some rest. Mr. Buck, the bartender greeted Panda and his animal friends with his antler. He proudly told them that his coffee is the best in the forest and this bar is a Michelin-starred bar, thats why the bar is called Starred Bucks.
There was a campaign running at the coffee bar: for every 3 cups of coffee, the cheapest one is FREE. After asking all his friends for their flavors, Panda wondered how much he need to pay.
 

Input
The first line of the input gives the number of test cases, T.
T test cases follow. Each test case consists of two lines. The first line contains one integer N, the number of cups to be bought.
The second line contains N integers  p1,p2,,pN  representing the prices of each cup of coffee.
 

Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the least amount of money Panda need to pay.

limits


 1 ≤ T ≤ 100.
 1 ≤ N ≤  105 .
 1 ≤ pi ≤ 1000.

 

Sample Input
  
  
2 3 1 2 3 5 10 20 30 20 20
 

Sample Output
  
  
Case #1: 5 Case #2: 80
 

Source
 

Recommend
jiangzijing2015

【题目大意】

三杯里价钱最低的一杯免费,问最少花多少钱。

【解题思路】

贪心。尽量每次便宜的杯价钱都高一些,所以从大到小排序后非3的倍数杯免费即可。

【解题代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
using namespace std;
const int maxn=1e5+10;
int cup[maxn];
bool cmp(int a,int b)
{
	return a>b;
 } 
 
int main()
{
	int t;
	int kase=0;
	scanf("%d",&t); 
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for( int i=0;i<n;i++)
			scanf("%d",&cup[i]);
		sort(cup,cup+n,cmp);
		int sum=0;
		for(int i=0;i<n;i++)
		{
			if((i+1)%3)
				sum+=cup[i];
		}
		printf("Case #%d: %d\n",++kase,sum);
		
	} 
	return 0;
} 

【收获与反思】

贪心策略,让每次免费的杯价钱尽量高些。入门题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值