HDU 1678 Shopaholic(贪心)
2016-01-24 00:18
162 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1678
简单的贪心题,排序即可,每三个加上那个最小值。
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,a[20005];
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
int ans=0;
for(int i=2; i<n; i+=3)
ans+=a[i];
printf("%d\n",ans);
}
return 0;
}
简单的贪心题,排序即可,每三个加上那个最小值。
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,a[20005];
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
int ans=0;
for(int i=2; i<n; i+=3)
ans+=a[i];
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 在命令行用 sort 进行排序
- 文件遍历排序函数
- C#选择排序法实例分析
- C#插入法排序算法实例分析
- C#实现Datatable排序的方法
- SQLSERVER的排序问题结果不是想要的
- Windows Powershell排序和分组管道结果
- C#通过IComparable实现ListT.sort()排序
- C#选择法排序实例分析
- SQL学习笔记四 聚合函数、排序方法
- C#对list列表进行随机排序的方法
- jQuery拖动元素并对元素进行重新排序
- 将MySQL查询结果按值排序的简要教程
- 一根网线内的8根线哪4根是传输数据的,哪四根是防干扰的
- C#折半插入排序算法实现方法
- SQL进行排序、分组、统计的10个新技巧分享
- C++实现位图排序实例
- C语言中快速排序和插入排序优化的实现
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析