贪心—商人小鑫

think:这道题目开始时候调用快排函数,但一直显示超内存,后来在博客上搜了一下,发现超内存的原因极可能是因为使用了memset函数对结构体初始化了,后来将其注释后ac了,但不明白为什么,再就是发现虽然都是用快排,但是很不一样。希望自己可以将快排查资料优化并尽可能简化步骤。

商人小鑫
Time Limit: 1000MS Memory Limit: 65536KB

Problem Description
小鑫是个商人,当然商人最希望的就是多赚钱,小鑫也一样。
这天,他来到了一个遥远的国度。那里有着n件商品,对于第i件商品需要付出ci的价钱才能得到。当然,对于第i件商品,小鑫在自己心中有一个估价pi:代表着当他买下这件商品后带回他的国家可以卖出的价格。小鑫只能带回m件商品,你能帮他计算一下他最多能赚多少钱么?

Input
输入有多组,到文件结束。(注:数据有很多组,请用高效率算法)
对于每一组数据。第一行是n,m。m≤n≤10000000。
紧接着有n行,每一行有两个数 c ,p。第i行代表着ci,pi。ci≤pi
数据都在int范围内 。

Output
对于每组输入数据只输出一行一个数,代表小鑫能赚多少钱。

Example Input
4 2
1 2
1 3
2 2
3 4

Example Output
3

Hint

Author
lin

以下为accepted代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 10000004
typedef struct node
{
    int pc;
    int flag;
}st;
st a[MAXN];
int cmp(const void *a, const void *b)
{
    if(((st *)a)->pc != ((st *)b)->pc)
    {
        return (((st *)b)->pc - ((st *)a)->pc);//降序
    }
    else
    {
        return (((st *)b)->flag - ((st *)a)->flag);//降序
    }
}
int main()
{
    int n, m, i, c, p, sum;
    while(scanf("%d %d", &n, &m) != EOF)
    {
        sum = 0;//累加变量sum初始化

        ///memset(a, 0, sizeof(a));//结构体a[MAXN]初始化
        for(i = 0; i < n; i++)
        {
            scanf("%d %d", &c, &p);
            a[i].pc = p-c;
            a[i].flag = i;
        }
        qsort(&a[0], n, sizeof(a[0]), cmp);//调用快排函数
        for(i = 0; i < m; i++)
        {
            if(a[i].pc >= 0)//通过实际环境考虑进而结合题意进行判断
            {
                sum += a[i].pc;
            }
            else if(a[i].pc < 0)
            {
                break;
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


/***************************************************
User name: jk160630
Result: Accepted
Take time: 56ms
Take Memory: 1684KB
Submit time: 2017-01-22 23:24:28
****************************************************/

以下为memory limit exceeded代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 10000004
typedef struct node
{
    int pc;
    int flag;
}st;
int cmp(const void *a, const void *b)
{
    if(((st *)a)->pc != ((st *)b)->pc)
    {
        return (((st *)b)->pc - ((st *)a)->pc);//降序
    }
    else if(((st *)a)->pc == ((st *)b)->pc)
    {
        return (((st *)b)->flag - ((st *)a)->flag);//降序
    }
}
int main()
{
    int n, m, i, c, p, sum;
    st a[MAXN];
    while(scanf("%d %d", &n, &m) != EOF)
    {
        sum = 0;//累加变量sum初始化
        memset(a, 0, sizeof(a));//结构体a[MAXN]初始化
        for(i = 0; i < n; i++)
        {
            scanf("%d %d", &c, &p);
            a[i].pc = p-c;
            a[i].flag = i;
        }
        qsort(&a[0], n, sizeof(a[0]), cmp);//调用快排函数
        for(i = 0; i < m; i++)
        {
            if(a[i].pc >= 0)//通过实际环境考虑进而结合题意进行判断
            {
                sum += a[i].pc;
            }
            else if(a[i].pc < 0)
            {
                break;
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


/***************************************************
User name: jk160630
Result: Memory Limit Exceeded
Take time: 0ms
Take Memory: 65536KB
Submit time: 2017-01-22 23:06:57
****************************************************/
<think>好的,我现在需要帮助用户解决关于SDUT C语言实验中“商人小鑫”题目的问题。首先,用户提到的是查找相关的题目解析或解决方案。根据用户提供的引用内容,看起来小鑫的任务是写一个自动排名的程序,可能类似于根据分数进行排序。不过题目名称是“商人小鑫”,可能需要确认是否同一题目。 首先,我需要回忆SDUT的C语言实验题中是否有名为“商人小鑫”的题目。通常,这类题目可能涉及排序算法,例如贪心算法,比如商人如何安排买卖以获得最大利润。比如,可能有题目描述商人小鑫在买卖商品时需要选择最优的买入和卖出时机,或者处理货物排序问题。 用户提供的引用[^1]是关于ACM集训队选拔的自动排名程序,这可能涉及到快速排序或类似算法。但用户现在的问题是关于“商人小鑫”的题目,可能两者不同,需要区分。可能用户混淆了两个不同的题目,或者题目内容相关但名称不同。 接下来,我需要确认是否存在“商人小鑫”的题目。假设存在,常见的类似题目可能类似于“买卖股票的最佳时机”问题,即给定一系列价格,计算最大利润。例如,每次可以选择买入和卖出,但必须在再次买入前卖出。或者可能涉及货物装运,按利润排序选择最优组合。 假设题目是类似于贪心的利润最大化问题,解决方案通常需要对数据进行排序。例如,将利润高的先处理,或者按某种条件排序后累加。例如,可能需要对物品按单价排序,或者按单位重量价值排序,类似背包问题。 例如,可能的题目描述是:商人小鑫有n个货物,每个货物有重量和利润,他的车能装一定重量的货物,如何选择货物使总利润最大。此时,解决方案是按单位重量的利润从高到低排序,依次选取直到载重满。 因此,解题步骤可能包括:输入货物信息,计算单位利润,排序,累加。这需要结构体存储货物信息,排序函数,然后计算结果。 现在需要确保提供的解决方案符合题目要求。比如,可能需要使用结构体排序,在C语言中可以用qsort函数,自定义比较函数。 例如,代码可能如下: ```c #include <stdio.h> #include <stdlib.h> struct Goods { int weight; int profit; double rate; }; int compare(const void *a, const void *b) { struct Goods *g1 = (struct Goods *)a; struct Goods *g2 = (struct Goods *)b; if (g2->rate > g1->rate) return 1; else if (g2->rate < g1->rate) return -1; else return 0; } int main() { int n, c; scanf("%d %d", &n, &c); struct Goods goods[n]; for (int i = 0; i < n; i++) { scanf("%d %d", &goods[i].weight, &goods[i].profit); goods[i].rate = (double)goods[i].profit / goods[i].weight; } qsort(goods, n, sizeof(struct Goods), compare); double total = 0; for (int i = 0; i < n; i++) { if (c >= goods[i].weight) { total += goods[i].profit; c -= goods[i].weight; } else { total += c * goods[i].rate; break; } } printf("%.2f\n", total); return 0; } ``` 这个代码处理的是典型的背包问题,按单位利润排序,优先装单位利润高的货物。这可能符合“商人小鑫”题目的需求。 需要验证用户提到的引用是否相关。引用中的小鑫需要写自动排名的程序,可能使用快速排序,但当前问题可能属于贪心算法。不过用户可能混淆了不同的题目,因此需要进一步确认。 如果用户需要的是商人小鑫的排序问题,比如按某种条件对商品排序,那么快速排序的实现可能相关。比如,根据利润或价格进行排序,可能需要自定义比较函数。 总结:用户的问题可能涉及结构体排序和贪心算法,需要根据题目具体要求选择排序方式。提供的解决方案应包含结构体定义、比较函数和排序过程。</think>关于SDUT C语言实验中的“商人小鑫”题目,其核心是**贪心算法的应用**,通常涉及对货物利润的排序与选择。以下是一个典型的问题解析和解决方案: --- ### 问题描述(推测) 商人小鑫需要从一批货物中选择部分装入货车,每件货物有重量$w_i$和利润$p_i$。货车载重为$C$,如何选择货物使得总利润最大? --- ### 解决思路 1. **计算单位重量利润**:对每件货物,计算$\text{单位利润} = \frac{p_i}{w_i}$。 2. **降序排序**:按单位利润从高到低排序。 3. **贪心选择**:依次装入单位利润高的货物,直到载重耗尽。 --- ### 代码实现 ```c #include <stdio.h> #include <stdlib.h> typedef struct { int weight; int profit; double unit_profit; // 单位重量利润 } Goods; // 比较函数:按单位利润降序排序 int compare(const void *a, const void *b) { Goods *g1 = (Goods *)a; Goods *g2 = (Goods *)b; if (g1->unit_profit < g2->unit_profit) return 1; // 降序 else return -1; } int main() { int n, capacity; scanf("%d %d", &n, &capacity); Goods goods[n]; // 输入数据并计算单位利润 for (int i = 0; i < n; i++) { scanf("%d %d", &goods[i].weight, &goods[i].profit); goods[i].unit_profit = (double)goods[i].profit / goods[i].weight; } // 排序 qsort(goods, n, sizeof(Goods), compare); // 贪心选择 double total_profit = 0.0; for (int i = 0; i < n && capacity > 0; i++) { if (goods[i].weight <= capacity) { total_profit += goods[i].profit; capacity -= goods[i].weight; } else { total_profit += capacity * goods[i].unit_profit; // 部分装入 capacity = 0; } } printf("%.2f\n", total_profit); return 0; } ``` --- ### 关键点说明 1. **结构体定义**:用`Goods`结构存储货物信息,包括单位利润。 2. **快速排序**:通过`qsort`实现降序排列,时间复杂度为$O(n \log n)$。 3. **贪心选择**:优先选择高单位利润货物,直至载重耗尽。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值