LA 3971 二分

直接二分搜品质因子

AC代码如下:

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define MAX 0x3f3f3f3f

int N, B;
map<string,int> id;
vector<int> price[1100], para[1100];
int cnt, L, R;

bool judge( int M ){
    int sum = 0;
    for( int i = 1; i <= cnt; i++ ){
        int temp = MAX;
        for( int j = 0; j < price[i].size(); j++ ){
            if( para[i][j] >= M ){
                temp = min( temp, price[i][j] );
            }
        }
        sum += temp;
        if( sum > B ){
            return false;
        }
    }
    return true;
}

int main(){
    int T;
    scanf( "%d", &T );
    while( T-- ){
        scanf( "%d%d", &N, &B );
        id.clear();
        for( int i = 0; i < 1100; i++ ){
            price[i].clear();
            para[i].clear();
        }
        cnt = 0;
        R = 0;
        for( int i = 0; i < N; i++ ){
            string temp1, temp2;
            int temp3, temp4;
            cin >> temp1 >> temp2 >> temp3 >> temp4;
            R = max( R, temp4 );
            if( id.find( temp1 ) != id.end() ){
                price[id[temp1]].push_back( temp3 );
                para[id[temp1]].push_back( temp4 );
            }else{
                id[temp1] = ++cnt;
                price[id[temp1]].push_back( temp3 );
                para[id[temp1]].push_back( temp4 );
            }
        }
        L = 0;
        while( L < R ){
            int M = L + ( R - L + 1 ) / 2;
            if( judge( M ) ){
                L = M;
            }else{
                R = M - 1;
            }
        }
        cout << L << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值