• Gym 100553B Burrito King 无脑背包


    题意就是你有n和m两个上限 吃一个东西会同时增加两个东西 m的值不能超过给定的m 问最后的n m值和每个东西吃了多少

    贪心一下就好了 算一下性价比 从最大的开始吃 直到吃满了m n也一定是最大了

    只是想借这道题说一下经常卡题的小bug 因为这道题一开始卡精度 后来又卡除零 很诡异……

    粗心bug小总结~

    1.double除零是有返回值的 int除零才是RE 同时注意数据范围有没有0

    2.精度可能要比题意中小一点

    3.两组数据之间也有换行

    4.两层for里i j用混了

    5.define的用法

    6.三目运算符可能会RE也可能超时

    7.预处理数组时越界

    8.case记得打

    9.有的地方爆了int要用long long

    (10.想起来再补充……)

    贴代码

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<math.h>
     5 #include<string.h>
     6 #include<map>
     7 #include<vector>
     8 #include<queue>
     9 #define M(a,b) memset(a,b,sizeof(a))
    10 using namespace std;
    11 const double eps=1e-10;
    12 struct thing {
    13     double z,m,ha,uh,val,sum,sum2,need;
    14 } th[100005];
    15 bool cmp(thing a,thing b) {
    16     return a.ha*b.uh>a.uh*b.ha;
    17 }
    18 bool cmp1(thing a,thing b) {
    19     return a.z<b.z;
    20 }
    21 int main() {
    22 //    printf("%f
    ",1.0/0);
    23     freopen("burrito.in","r",stdin);
    24     freopen("burrito.out","w",stdout);
    25     int n,a,b;
    26     while(~scanf("%d%d%d",&n,&a,&b)) {
    27         for(int i=0; i<n; i++) {
    28             th[i].z=i;
    29             scanf("%lf%lf%lf",&th[i].m,&th[i].ha,&th[i].uh);
    30             th[i].sum=th[i].m*th[i].ha;
    31             th[i].sum2=th[i].m*th[i].uh;
    32             th[i].need=0;
    33         }
    34         sort(th,th+n,cmp);
    35         double nowa=0,nowb=0;
    36         for(int i=0; i<n; i++) {
    37             if(th[i].uh==0) {
    38                 nowa+=th[i].sum;
    39                 th[i].need=th[i].m;
    40             } else if(nowb+th[i].sum2>=b) {
    41                 if(nowb+th[i].sum2==b) {
    42                     th[i].need=th[i].m;
    43                     nowb+=th[i].sum2;
    44                     nowa+=th[i].sum;
    45                 } else {
    46                     th[i].need=(b-nowb)*1.0/th[i].uh*1.0;
    47                     nowa+=th[i].need*th[i].ha;
    48                     nowb=b;
    49                 }
    50                 break;
    51             } else {
    52                 nowa+=th[i].sum;
    53                 nowb+=th[i].sum2;
    54                 th[i].need=th[i].m;
    55             }
    56         }
    57         if(nowa<a-eps) printf("-1 -1
    ");
    58         else {
    59             sort(th,th+n,cmp1);
    60             printf("%.10lf %.10lf
    ",nowa,nowb);
    61             for(int i=0; i<n; i++)
    62                 printf("%.10lf%c",th[i].need,i==n-1?'
    ':' ');
    63         }
    64     }
    65     return 0;
    66 }
    67 /*
    68 
    69 2 5 5
    70 2 2 1
    71 2 2 4
    72 2 5 5
    73 2 2 2
    74 2 2 4
    75 
    76 */
  • 相关阅读:
    2017-2018-1 20179215《Linux内核原理与分析》第九周作业
    2017-2018-1 20179215 速读《构建之法》
    2017-2018-1 20179215 速读《从问题到程序》
    2017-2018-1 20179215《Linux内核原理与分析》第八周作业
    2017-2018-1 20179215《Linux内核原理与分析》第七周作业
    2017-2018-1 20179215 课堂测试
    2017-2018-1 20179215《Linux内核原理与分析》第六周作业
    2017-2018-1 20179215《Linux内核原理与分析》第五周作业
    20179215 第二周课堂测试
    2017-2018-1 20179215《Linux内核原理与分析》第三周作业
  • 原文地址:https://www.cnblogs.com/general10/p/5731395.html
Copyright © 2020-2023  润新知