• hlg1551Assemble--暴力求解


    Assemble
    Time Limit: 2000 MS Memory Limit: 65535 K
    Total Submit: 17(10 users) Total Accepted: 11(10 users) Rating:  Special Judge: No
    Description
        Recently your team noticed that the computer you use to practice for programming contests
    is not good enough anymore. Therefore, you decide to buy a new computer.
        To make the ideal computer for your needs, you decide to buy separate components and
    assemble the computer yourself. You need to buy exactly one of each type of component.
        The problem is which components to buy. As you all know, the quality of a computer is
    equal to the quality of its weakest component. Therefore, you want to maximize the quality
    of the component with the lowest quality, while not exceeding your budget.
    Input

    On the first line one positive number: the number of testcases, at most 100. After that per
    testcase:
    • One line with two integers:1≤n≤1 000, the number of available components and1≤b≤1 000 000 000, your budget.
    • n lines in the following format: “type name price quality”, wheretypeis a string with the type of the component,nameis a string with the unique name of the com-ponent,priceis an integer (0≤price≤1 000 000) which represents the price of the
    component andqualityis an integer (0≤quality≤1 000 000 000) which represents
    the quality of the component (higher is better). The strings contain only letters, digits

    and underscores and have a maximal length of20characters.
    It will always possible to construct a computer with your budget.
    Output

    Per testcase:
    • One line with one integer: the maximal possible quality.

    Sample Input
    1
    18 800
    processor 3500_MHz 66 5
    processor 4200_MHz 103 7
    processor 5000_MHz 156 9
    processor 6000_MHz 219 12
    memory 1_GB 35 3
    memory 2_GB 88 6
    memory 4_GB 170 12
    mainbord all_onboard 52 10
    harddisk 250_GB 54 10
    harddisk 500_FB 99 12
    casing midi 36 10
    monitor 17_inch 157 5
    monitor 19_inch 175 7
    monitor 20_inch 210 9
    monitor 22_inch 293 12
    mouse cordless_optical 18 12
    mouse microsoft 30 9
    keyboard office 4 10
    Sample Output
    9
    Source
    NWERC2007

    大意:给你一部分电脑零件

    每种零件有一定的价格和性能值

    每种零件需要一个

    电脑的整体性能为所选的所有零件中性能最差的

    给你一笔预算和一些零件问你不超过预算的求情况下电脑的最大性能

    分析:

    这是黄老师家上的一道题

    比赛的时候硬是半天没有做出来

    其实直接暴力就可以了

    枚举每种可能出现的答案  然后判断预算是否超支   对于每个枚举的答案  同一类型如果没有一件物品的性能值大于等于该值说明不符合条件

    对于多件符合的物品   选择价格最便宜的即可

    代码: 

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <map>
     6 using namespace std;
     7 
     8 const int maxn = 1005;
     9 
    10 struct point
    11 {
    12     string s;
    13     int a, b;
    14 }poin[maxn];
    15 
    16 int n, sum_b;
    17 bool check(int x)
    18 {
    19     int sum = 0;
    20     string s1 = poin[0].s;
    21     int st = 0, en = 0;//每一类的最开始的下标和结束的下标
    22     for(int i = 0; i < n; i++)//需要注意的是最后一类的判断
    23     {
    24         if(poin[i].s != s1)//不属于上一类
    25         {
    26             en = i - 1;
    27             bool flag = false;
    28             int max_num = 0xffffff;
    29             for(int i = st; i <= en; i++)//判断
    30             {
    31                 if(poin[i].b >= x)
    32                 {
    33                     flag = true;
    34                     if(max_num > poin[i].a)//在该类寻找最便宜的即可
    35                     {
    36                         max_num = poin[i].a;
    37                     }
    38                 }
    39             }
    40             if(!flag)//该类中没有一件物品的品质大于等于x
    41                 return false;
    42             sum += max_num;
    43             st = i;
    44             s1 = poin[i].s;
    45         }
    46     }
    47     //printf("*%d
    ",sum);
    48     if(sum <= sum_b)
    49         return true;
    50     else
    51         return false;
    52 }
    53 
    54 int main()
    55 {
    56     int t;
    57     //freopen("1511.txt","r",stdin);
    58     scanf("%d",&t);
    59     while(t--){
    60         scanf("%d %d",&n, &sum_b);
    61         string str;
    62         int num[maxn] = { 0 };//num数组用于存储需要枚举的值
    63         int k = 0;
    64         for(int i = 0; i < n; i++)
    65         {
    66             cin>>poin[i].s>>str>>poin[i].a>>poin[i].b;
    67             num[k++] = poin[i].b;//将需要枚举的值加入数组中
    68         }
    69         poin[n].s = "asdf";//这一步非常重要  wrong一次
    70         n++;
    71         sort(num, num + k);
    72         num[k] = 0;
    73         int ans = 0;
    74         for(int i = k - 1; i >= 0; i--)//从大到小
    75         {
    76              if(num[i] == num[i+1])
    77                 continue;//枚举过的话就直接忽略
    78              if(check(num[i]))//check 用来检测预算是否超支
    79              {
    80                  ans = num[i];
    81                  break;//如果满足条件的话直接跳出,因为要求的就是求得最大值
    82              }
    83             // printf("%d %d
    ",num[i],check(num[i]));
    84         }
    85         printf("%d
    ",ans);
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    javamail.providers not found
    vue.js实现购物车功能2.0版本
    vue.js实现购物车功能
    iframe高度自适应
    C语言 自定义字符串拼接函数
    php安装
    Apache安装
    python:爬虫初体验
    c:forEach 显示下拉框并回显
    数据结构 --- 线性表学习(php模拟)
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/3716855.html
Copyright © 2020-2023  润新知