• 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个用空格分隔的整数,表示每个猴子获得的香蕉质量之和。按照输入顺序输出对应的猴子。

    输入样例 
    1. 5 3
    2. 1 2 3 4 5
    3. 3 2 1
    输出样例 
    7 5 3

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

    使用模运对数组赋值

    1. #include
    2. using namespace std;
    3. typedef long long int ll;
    4. #define endl "\n"
    5. const int maxLine = 1e5+10;
    6. // #define long long int ll;
    7. // bool cmp(const pair& a,const pair& b){
    8. // return a.second
    9. // }
    10. // auto maxValue=max_element(mymap.begin(),mymap.end(),cmp);
    11. // 猴子的序号和体重过
    12. // prir mypair[maxLine];
    13. struct monky {
    14. int index;
    15. int w;
    16. int value;
    17. };
    18. struct monky ttt[maxLine];
    19. // 体重排序
    20. bool cmp(struct monky a,struct monky b){
    21. return a.w>b.w;
    22. }
    23. bool cmp2(struct monky a,struct monky b){
    24. return a.index
    25. }
    26. int banana[maxLine];
    27. void print(struct monky arr[maxLine],int nums){
    28. for(int i=0;i
    29. cout<" "<" "<
    30. }
    31. cout<
    32. }
    33. void prints(int arr[maxLine],int nums){
    34. for(int i=0;i
    35. cout<
    36. }
    37. cout<
    38. }
    39. int main() {
    40. int m,n;
    41. cin>>m>>n;
    42. for(int i=0;i
    43. cin>>banana[i];
    44. }
    45. for(int i=0;i
    46. int w;
    47. cin>>w;
    48. ttt[i].index=i;
    49. ttt[i].w=w;
    50. }
    51. // 对香蕉降序
    52. sort(banana,banana+m,greater<int>());
    53. // 对猴子体重升序
    54. sort(ttt,ttt+n,cmp);
    55. for(int i=0;i
    56. ttt[i%n].value+=banana[i];
    57. }
    58. // 按照index升序
    59. sort(ttt,ttt+n,cmp2);
    60. for(int i=0;i
    61. cout<" ";
    62. }
    63. return 0;
    64. }

  • 相关阅读:
    macos知名的清理软件 cleanmymac和腾讯柠檬哪个好 cleanmymacx有必要买吗
    小知识:设置archive_lag_target参数强制日志切换
    深度学习系列2——Pytorch 图像分类(AlexNet)
    LeetCode_贪心算法_简单_605.种花问题
    百战c++(数据库1)
    【华为机试真题详解】检查是否存在满足条件的数字组合
    SoC Architecture Design & Verification
    JuiceFS 在多云存储架构中的应用 | 深势科技分享
    【npm如何发布自己的插件包】
    2022/7/26 考试总结
  • 原文地址:https://blog.csdn.net/m0_72678953/article/details/134063812