1093: 分香蕉

题目描述

现在有n个香蕉,每个香蕉的质量为ai,m只猴子,每只猴子的体重为bi。

现在将香蕉分给这些猴子,将猴子按照从大到小的顺序依次拿香蕉。当一轮拿完时,还有多的香蕉就会继续一个个拿,直到被拿完。

猴子都是聪明的,每次都会选择一个质量最大的香蕉。

现在请求出每个猴子获得的香蕉质量。

输入格式

第一行输入两个正整数n,m(1<=n,m<=10^5)

第二行n个整数ai表示每个香蕉的质量(1<=ai<=10^4)

第三行m个整数bi表示每个猴子的体重,保证体重互不相同。(1<=bi<=10^9)

输出格式

一行,m个用空格分隔的整数,表示每个猴子获得的香蕉质量之和。按照输入顺序输出对应的猴子。

输入样例
复制代码
5 3
1 2 3 4 5
3 2 1
输出样例
复制代码
7 5 3

这里没想到什么很好的方法,就直接用结构体排序了

使用模运对数组赋值

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define endl "\n"
const int  maxLine = 1e5+10;
// #define long long int ll;

// bool cmp(const pair<char,int>& a,const pair<char,int>& b){
//     return a.second<b.second;
// }
// auto maxValue=max_element(mymap.begin(),mymap.end(),cmp);
// 猴子的序号和体重过
// prir<int,int> mypair[maxLine];
struct monky {
    int index;
    int w;
    int value;
};
struct monky ttt[maxLine];
// 体重排序
bool cmp(struct monky a,struct monky b){
    return a.w>b.w;
}

bool cmp2(struct monky a,struct monky b){
    return a.index<b.index;
}

int banana[maxLine];

void print(struct monky arr[maxLine],int nums){
    for(int i=0;i<nums;i++){
        cout<<ttt[i].index<<" "<<ttt[i].w<<" "<<ttt[i].value<<endl;
    }
    cout<<endl;
}
void prints(int arr[maxLine],int nums){
    for(int i=0;i<nums;i++){
        cout<<arr[i]<<endl;
    }
    cout<<endl;
}
int main() {
    int m,n;
    cin>>m>>n;
    for(int i=0;i<m;i++){
        cin>>banana[i];
    }
    for(int i=0;i<n;i++){
        int w;
        cin>>w;
        ttt[i].index=i;
        ttt[i].w=w;
    }
    // 对香蕉降序
    sort(banana,banana+m,greater<int>());
    // 对猴子体重升序
    sort(ttt,ttt+n,cmp);

    for(int i=0;i<m;i++){
        ttt[i%n].value+=banana[i];
    }
    // 按照index升序
    sort(ttt,ttt+n,cmp2);

    for(int i=0;i<n;i++){
        cout<<ttt[i].value<<" ";
    }
    return 0;
}
相关推荐
88号技师2 分钟前
2025年7月一区SCI优化算法-Logistic-Gauss Circle optimizer-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
一个不知名程序员www1 小时前
算法学习入门---二分查找(C++)
c++·算法
2301_807997381 小时前
代码随想录-day26
数据结构·c++·算法·leetcode
闭着眼睛学算法2 小时前
【双机位A卷】华为OD笔试之【排序】双机位A-银行插队【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
TL滕2 小时前
从0开始学算法——第一天(认识算法)
数据结构·笔记·学习·算法
小欣加油2 小时前
leetcode 3318 计算子数组的x-sum I
c++·算法·leetcode·职场和发展
love is sour2 小时前
聚类(Clustering)详解:让机器自己发现数据结构
算法·支持向量机·聚类
烟袅2 小时前
LeetCode 142:环形链表 II —— 快慢指针定位环的起点(JavaScript)
前端·javascript·算法
CoovallyAIHub2 小时前
OCR战场再起风云:LightOnOCR-1B凭什么比DeepSeekOCR快1.7倍?(附演示开源地址)
深度学习·算法·计算机视觉
海琴烟Sunshine3 小时前
leetcode 190. 颠倒二进制位 python
python·算法·leetcode