题解 CF1379C Choosing flowers

· · 题解

感觉难度虚高

首先很显然的,我们把每一种物品拆成 2 个,一个贡献是 a_i,一个贡献是 b_i。然后按贡献排序(显然尽量取贡献大的)。

我们再设 solve(t,nn) 表示从下标为 t 的物品往后取,还能取 nn 个。在遍历的时候分 3 种情况。

具体第一次是否买过了可以用数组标记。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
struct node{
    ll x;
    int y,id;
}p[N*2];
int T,m,tot;
ll n,a[N][2];
bool flag[N];
bool cmp(node a,node b){return a.x>b.x;}
ll solve(int t,int nn){
    ll res=0;
    for(int i=t;i<=tot&&nn;i++){
        if(p[i].id==0){
            res+=p[i].x;nn--;
            flag[p[i].y]=1;
        }
        else{
            if(flag[p[i].y]){
                res+=nn*p[i].x;
                break;
            }
            else{
                ll val=a[p[i].y][0]+(nn-1)*p[i].x;
                res+=max(val,solve(i+1,nn));
                break;
            }
        }
    }
    return res;
}
int main(){
    scanf("%d",&T);
    while(T--){
        tot=0;
        scanf("%lld%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%lld%lld",&a[i][0],&a[i][1]);
            flag[i]=0;
            p[++tot]=(node){a[i][0],i,0};
            p[++tot]=(node){a[i][1],i,1};
        }
        sort(p+1,p+tot+1,cmp);
        printf("%lld\n",solve(1,n));
    }
    return 0;
}