nowcoder contest#115 江西财经大学第一届程序设计竞赛 G 小Q的口袋校园 记忆化搜索 DP

时间:2021-04-12 19:50:14

题解:

1、先把活动按照开始时间进行排序

2、然后直接判断每一个活动是否参加,参加的话要满足当前活动开始时间大于或等于前一个参加活动的结束时间

3、直到搜完所有活动为止然后维护happy值和point值就行了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 25;
struct point{int s,e,h,p;};
struct point p[maxn];
bool cmp(struct point a,struct point b){return a.s<b.s;}
int n = 0,maxhappy = 0,maxpoint = 0;
void dfs(int right,int now,int sumhappy,int sumpoint){
    if(sumhappy > maxhappy){
        maxhappy = sumhappy;
        maxpoint = sumpoint;
    }
    else if(sumhappy == maxhappy){
        maxpoint = max(maxpoint,sumpoint);
    }
    if(now == n) return;
    if(p[now].s >= right)
        dfs(p[now].e,now+1,sumhappy+p[now].h,sumpoint+p[now].p);
    dfs(right,now+1,sumhappy,sumpoint);
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        maxhappy = maxpoint = 0;
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d%d%d%d",&p[i].s,&p[i].e,&p[i].h,&p[i].p);
        sort(p,p+n,cmp);
        dfs(0,0,0,0);
        printf("%d %d\n",maxhappy,maxpoint);
    }
    return 0;
}