BestCoder Round #59 HDOJ5499 5500 5501题解

题目链接: 点击打开链接


A: 模拟题, 读入数据的时候记录有没有女选手以及round1 & round2最大值, 排序后分类讨论, 如果全为男选手直接输出m个, 如果有女选

手, 再分类讨论女选手在不在前m个中.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 150;
struct node
{
    /* data */
    char name[25], sex[10];
    double score1, score2, total;
}a[MAXN];
int n, m;
bool cmp(node a, node b)
{
    return a.total > b.total;
}
int main(int argc, char const *argv[])
{
    int t;
    scanf("%d", &t);
    while(t--) {
        getchar();
        bool flag = false;
        double m1 = 0, m2 = 0;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; ++i) {
            scanf("%s%s%lf%lf", a[i].name, a[i].sex, &a[i].score1, &a[i].score2);
            if(a[i].sex[0] == 'f') flag = true;
            m1 = max(m1, a[i].score1);
            m2 = max(m2, a[i].score2);
        }
        for(int i = 0; i < n; ++i) {
            a[i].score1 *= 1.0 * 300 / m1;
            a[i].score2 *= 1.0 * 300 / m2;
            a[i].total = a[i].score1 * 0.3 + a[i].score2 * 0.7;
        }
        sort(a, a + n, cmp);
        printf("The member list of Shandong team is as follows:\n");
        if(!flag) {
            for(int i = 0; i < m; ++i)
                printf("%s\n", a[i].name);
        }
        else {
            int x = 0, num = 0;
            bool flag2 = false;
            for(int i = 0; i < n; ++i)
                if(a[i].sex[0] == 'f') {
                    x = i;
                    break;
                }
            if(x < m) 
                for(int i = 0; num < m; ++i) 
                    if(a[i].sex[0] == 'f' && !flag2) {
                        printf("%s\n", a[i].name);
                        flag2 = true;
                        num++;
                    }
                    else {
                        printf("%s\n", a[i].name);
                        num++;
                    }
            else {
                for(int i = 0; i < m - 1; ++i) 
                    printf("%s\n", a[i].name);
                printf("%s\n", a[x].name);
            }
        }
    }
    return 0;
}


B: yy题, 首先找到最大的n的位置, 向前寻找n - 1, 如果找到则n - 1不需要调整, 未找到则需要调整1次, 再向前寻找n - 2, 直到找完.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 20;
int n, a[MAXN];
int main(int argc, char const *argv[])
{
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(int i = 0; i < n; ++i) 
            scanf("%d", &a[i]);
        int x = n - 1, ans = n;
        while(x >= 0) {
            if(a[x] == ans) ans--;
            x--;
        }
        printf("%d\n", ans);
    }    
    return 0;
}


C: 贪心+01背包. 考虑挨着的两题需不需要交换, 根据分数的损失可以得到不等式: b[i + 1] / c[i + 1] > b[i] / c[i], 可以看出做题的最优顺序

与这个比值有关, 由大到小排序后, 01背包选题目使得得分最高即可.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 3010;
struct node
{
    /* data */
    int a, b, c;
    double x;
}pro[MAXN];
int n, t, dp[MAXN];
bool cmp(node a, node b)
{
    return a.x > b.x;
}
int main(int argc, char const *argv[])
{
    int cas;
    scanf("%d", &cas);
    while(cas--) {
        memset(dp, -1, sizeof(dp));
        scanf("%d%d", &n, &t);
        for(int i = 0; i < n; ++i) {
            scanf("%d%d%d", &pro[i].a, &pro[i].b, &pro[i].c);
            pro[i].x = 1.0 * pro[i].b / pro[i].c;
        }
        sort(pro, pro + n, cmp);
        int ans = 0;
        dp[0] = 0;
        for(int i = 0; i < n; ++i)
            for(int j = t; j >= pro[i].c; --j) {
                if(dp[j - pro[i].c] == -1) continue;
                dp[j] = max(dp[j], dp[j - pro[i].c] + pro[i].a - j * pro[i].b);
            }
        for(int i = 0; i <= t; ++i)
            ans = max(ans, dp[i]);
        printf("%d\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值