zoukankan      html  css  js  c++  java
  • UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大。

    析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <unordered_map>
    #include <unordered_set>
    #define debug() puts("++++");
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e3 + 5;
    const int mod = 2000;
    const int dr[] = {-1, 1, 0, 0};
    const int dc[] = {0, 0, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    struct Node{
        int p;
        int val;
        bool operator < (const Node &pp) const{
            return p < pp.p;
        }
        Node(int pp, int v) : p(pp), val(v) { }
    };
    vector<Node> v[maxn];
    map<string, int> mp;
    int cnt;
    
    int getId(const string &s){
        if(mp.count(s))  return mp[s];
        return mp[s] = cnt++;
    }
    
    bool judge(int mid){
        int ans = 0;
        for(int i = 0; i < cnt; ++i){
            bool ok = false;
            for(int j = 0; j < v[i].size(); ++j)
                if(v[i][j].val >= mid){ ans += v[i][j].p;  ok = true; break; }
            if(!ok || ans > m)  return false;
        }
        return true;
    }
    
    int solve(){
        int l = 1, r = (int)1e9;
        for(int i = 0; i < cnt; ++i)  sort(v[i].begin(), v[i].end());
        while(l < r){
            int mid = (l + r) >> 1;
            if(judge(mid)) l = mid + 1;
            else r = mid;
        }
        return judge(l) ? l : l-1;
    }
    
    int main(){
        int T;  cin >> T;
        while(T--){
            scanf("%d %d", &n, &m);
            mp.clear();
            cnt = 0;
            for(int i = 0; i < n; ++i)  v[i].clear();
            char s[25], t[25];
            int p, x;
            for(int i = 0; i < n; ++i){
                scanf("%s %s %d %d", s, t, &p, &x);
                v[getId(s)].push_back(Node(p, x));
            }
            printf("%d
    ", solve());
        }
        return 0;
    }
    
  • 相关阅读:
    PhpStorm函数注释的设置
    thinkphp5 返回数组提示variable type error: array
    js获取json对象中的key和value,并组成新数组
    PHP生成随机字符串与唯一字符串
    yii2-admin扩展自定义目录
    PHP7.3发布啦
    服务器环境从PHP5升级到PHP7
    亲测能用的mysqli类,挺好用的
    PHP必用代码片段
    git flow的使用
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6407864.html
Copyright © 2011-2022 走看看