题解 | #信封嵌套# 运算符重定义

信封嵌套

https://www.nowcoder.com/practice/25fe1fc89c4c4e82bbc63df04bc6ca30

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
struct envelope{
    int height;
    int width;
    envelope(int w,int h):width(w),height(h){}
    bool operator<(envelope& other){
        if(width==other.width)return height<other.height;
        return width<other.width;
    };
    bool canHold(envelope& other){
        //cout<<*this<<' '<<other<< ((width>other.width) && (height>other.height))<<endl;
         return (width>other.width) && (height>other.height);
    }
    friend std::ostream& operator<<(ostream& os,envelope& ev){
        cout<<'('<<ev.width<<' '<<ev.height<<')';
        return os;
    };

};

int main() {
    int n;
    int t1,t2,i,j,result=1;
    //坑,result初值为1,与dp元素初值相同而不是随手写int_min,为了异常输入(n=1)时输出仍然为1
   cin>>n;
   vector<envelope> envelopes;envelopes.reserve(n);
   while(n-- && cin>>t1>>t2)envelopes.push_back({t1,t2});
   sort(envelopes.begin(),envelopes.end());
   vector<int> dp(envelopes.size(),1);
   //printa(envelopes);
   for(i=1;i<envelopes.size();++i){
    for(j=0;j<i;++j){
        if(envelopes[i].canHold(envelopes[j]))dp[i]=max(dp[i],dp[j]+1);
    }
    result=max(result,dp[i]);
   }
   cout<<result<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 20:15
还能挽救吗?找同学帮忙看了一下&nbsp;字节怎么能如此对我
牛客26396789...:你这是严重红线,被发现你自己永远进不去,你那个同学直接走人,你还敢宣扬
点赞 评论 收藏
分享
喜欢飞来飞去的雪碧在...:可以试一试字节
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务