Codeforces 774B: Significant Cups prefix and + bisection

Portal

Title description

Give you n1 first-class items and n2 second-class items, and give you the importance and weight of each item. You must choose one item for each type. If you select an item, the importance is greater than the similar items of the selected item Must be selected, the total weight of the selected items shall not exceed the given weight, and the sum of the maximum importance is required

analysis

The idea is relatively simple, and it is not difficult to write.
First of all, it is easier to think that each type of item must be taken from the most important to the least important, so that we can enumerate the first type of items and select the i-th one, and then Go to the maximum number of items that can be selected in the second category, and then update the answer.
Just use the prefix and optimization to preprocess it in advance.

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
int n1,n2,m;
struct Node{
    
    
    int d,w;
}a[N],b[N];
ll da[N],db[N];
ll wa[N],wb[N];

bool cmp(Node A,Node B){
    
    
    if(A.w != B.w)
        return A.w > B.w;
    return A.d < B.d;
}

int main(){
    
    
    scanf("%d%d%d",&n1,&n2,&m);
    for(int i = 1;i <= n1;i++) scanf("%d%d",&a[i].w,&a[i].d);
    for(int i = 1;i <= n2;i++) scanf("%d%d",&b[i].w,&b[i].d);
    sort(a + 1,a + 1 + n1,cmp);
    sort(b + 1,b + 1 + n2,cmp);
    for(int i = 1;i <= n1;i++) {
    
    
        da[i] = da[i - 1] + a[i].d;
        wa[i] = wa[i - 1] + a[i].w;
    }
    for(int i = 1;i <= n2;i++) {
    
    
        db[i] = db[i - 1] + b[i].d;
        wb[i] = wb[i - 1] + b[i].w;
    }
    ll ans = 0;
    for(int i = 1;i <= n1;i++){
    
    
        int l = 1,r = n2;
        while(r  - l > 1){
    
    
            int mid = l + r >> 1;
            if(da[i] + db[mid] <= m) l = mid;
            else r = mid - 1;
        }
        if(da[i] + db[r] <= m){
    
    
            ans = max(wa[i] + wb[r],ans);
        }
        if(da[i] + db[l] <= m){
    
    
            ans = max(wa[i] + wb[l],ans);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

Guess you like

Origin blog.csdn.net/tlyzxc/article/details/112465594