HDU 3420 Bus Fair(贪心)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3420

题意:这是一辆神奇的公交,每次只检查你身上带的钱够不,你的钱不够乘车时你就必须下车,你的钱够乘车时你就可以继续乘车,且不用付车钱,走第一个公里的钱是1人1元,第二公里的钱是1人2元,依次类推,问最少花多少钱就能让你的朋友都乘上车

思路:刚开始就全部上车,求出阶段中的最大值即可

AC代码:

暴力每个阶段,比较费时

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 100010;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int a[1010];
int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        int max1 = 0;
        sort(a+1,a+n+1);
        for(int i=1; i<=a[n]; i++)
        {
            int sum = 0;
            for(int j=1; j<=n; j++)
            {
                if(a[j] >= i)
                    sum++;
            }
            int l = sum * i;
            max1 = max(max1,l);
        }
        printf("%d\n",max1);
    }
    return 0;
}
暴力每个人下车的阶段,这一定是上一个人下车之后的最大值

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 100010;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int a[1010];
int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        int max1 = 0;
        sort(a+1,a+n+1);
        for(int i=1; i<=n ;i++)
        {
            max1 = max(max1,a[i] * (n-i+1));
        }
        printf("%d\n",max1);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值