贪心&Shopaholic
2016-01-24 15:03
232 查看
题意:Lindsay是一个购物狂,某商店进行一个促销活动,每买三件东西,其中最便宜的一件免费。很明显的贪心题。
思路:对所有物品的价格进行排序,从大到小三个三个一次的买,累加每次的最便宜的一件,直到剩下的恰好选完或者不足三件(注意不足三件不满足活动要求)。累加的和为answer.
上代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=20005;
//char a[maxn],b[maxn];
int p[maxn];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,mdis=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n-2;i+=3)
{
mdis+=p[i+2];
}
printf("%d\n",mdis);
}
return 0;
}
思路:对所有物品的价格进行排序,从大到小三个三个一次的买,累加每次的最便宜的一件,直到剩下的恰好选完或者不足三件(注意不足三件不满足活动要求)。累加的和为answer.
上代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=20005;
//char a[maxn],b[maxn];
int p[maxn];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,mdis=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n-2;i+=3)
{
mdis+=p[i+2];
}
printf("%d\n",mdis);
}
return 0;
}
相关文章推荐
- linux开机启动脚本、服务等
- linux终端快捷键
- shell script语法
- macosx下apache的默认用户为daemon
- linux的man命令
- *Linux之rm命令
- hadoop WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using b
- Apache Commons 工具类介绍及简单使用
- opencv之haar特征+AdaBoos分类器算法流程(二)
- 中国十大B2C电商网站开发语言调查
- Tomcat各组件关系及server.xml配置文件介绍
- centos6.5修改硬盘分区
- Linux CentOs 7.0 mysql 5.7.10 开机自动启动方法
- linux用户管理笔记
- 怎么修改centos6.5的硬盘分区
- Linux下查看Java进程的运行期间堆栈情况
- hadoop fs -mkdir testdata错误 提示No such file or directory
- 如何把网站及数据库部署到Windows Azure
- linux screen 命令详解
- Linux前台、后台、挂起、退出、查看命令汇总