首页 > ACM题库 > HDU-杭电 > hdu 2333 Assemble-分治-[解题报告]C++
2014
01-05

hdu 2333 Assemble-分治-[解题报告]C++

Assemble

问题描述 :

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.

输入:

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 and 1 ≤ b ≤ 1 000 000 000, your budget.

n lines in the following format: “type name price quality”, where type is a string with the type of the component, name is a string with the unique name of the component, price is an integer (0 ≤ price ≤ 1 000 000) which represents the price of the component and quality is 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 of 20 characters.

输出:

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 and 1 ≤ b ≤ 1 000 000 000, your budget.

n lines in the following format: “type name price quality”, where type is a string with the type of the component, name is a string with the unique name of the component, price is an integer (0 ≤ price ≤ 1 000 000) which represents the price of the component and quality is 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 of 20 characters.

样例输入:

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

样例输出:

9

一开始用深搜+string+map+set,ZOJ和HDU都过了,POJ死活TLE。

后来只好转为用二分吧。

主要是对能够支持的quality进行二分,求出符合题意要求的最大quality。

选取部件的时候使用贪心,找到不小于给定quality的最便宜的部件。


#ifdef _MSC_VER
#define DEBUG
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
#define MAX 1010
using namespace std;

struct NODE
{
  char name[40];
  int price,quality;
};

NODE node[MAX];

int quality[MAX];
int n,m;

int main(void)
{
#ifdef DEBUG  
  freopen("../stdin.txt","r",stdin);
  freopen("../stdout.txt","w",stdout); 
#endif  

  int ncase=1;
  char str[40];
  scanf("%d",&ncase);

  while(ncase--)
  {
    scanf("%d%d",&n,&m);
    int cnt=0;
    for(int i=0;i<n;++i)
    {
      getchar();
      scanf("%s%s%d%d",node[i].name,str,&node[i].price,&node[i].quality);
      quality[cnt++]=node[i].quality;
    }
    sort(quality,quality+cnt);
    cnt=unique(quality,quality+cnt)-quality;
    int low=0,high=cnt-1;
    int ans=0;
    while(high>=low)
    {
      int mid=(low+high)>>1;
      int qua=quality[mid];
      int now=0,pre=0;
      int sum=0;
      while(pre<n)
      {
        int tmp=INT_MAX;
        while(!strcmp(node[now].name,node[pre].name))
        {
          if(node[now].quality>=qua)
            tmp=min(tmp,node[now++].price);
          else
            now++;
        }
        if(tmp==INT_MAX || sum+tmp>m)
          break;
        else
        {
          sum+=tmp;
          pre=now;
        }
      }
      if(pre<n || sum>m)
        high=mid-1;
      else
      {
        ans=mid;
        low=mid+1;
      }
    }
    printf("%d\n",quality[ans]);
  }

  return 0;
}

解题转自:http://blog.csdn.net/neofung/article/details/7485133