题解:P13486 [GCJ 2008 Finals] Juice

· · 题解

思路:

具体思路如下:

第一想法:

枚举在要求中出现过的 A,B值,并算出当前的C,最后统计满足要求的个数。

但是还是会超时。

考虑优化。

优化

可以使用优先队列维护C的值,因为C是越来越小的。

只需要在每次循环内部去掉没用的C值(pop掉) 。

当前优先队列的大小就是满足要求的人的个数 。

把所有答案取最大值输出即可 。

时间复杂度:

O(n\times n\times logn)

不卡常数,勉强能过.

代码:

#include <bits/stdc++.h>
using namespace std;
using p=pair<int,int>;
struct node{
    int a,b,c;
    bool operator < (node fa)const{
        return a<fa.a; 
    }
}drink[5005];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    for(int t=1;t<=T;t++){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>drink[i].a>>drink[i].b>>drink[i].c;
        }
        sort(drink+1,drink+1+n);
        priority_queue<p,vector<p>,greater<p> > qBC;
        priority_queue<int> qC;
        int ans=0;
        for(int i=1;i<=n;i++){
            int nowA=drink[i].a;
            for(int j=1;j<=i;j++){
                qBC.push({drink[j].b,drink[j].c});
            } 
            while(!qBC.empty()){
                int nowB=qBC.top().first;
                while(!qBC.empty()&&qBC.top().first==nowB){
                    qC.push(qBC.top().second);
                    qBC.pop();
                } 
                int nowC=10000-nowA-nowB;
                while(!qC.empty()&&qC.top()>nowC){
                    qC.pop();
                } 
                int st=qC.size();
                ans=max(ans,st);
            }
            while(!qC.empty()){
                qC.pop();
            }
        } 
        cout<<"Case #"<<t<<": "<<ans<<endl;
    }
    return 0;
}