codeforces 878C 及部分set的用法


参考的题解:http://blog.csdn.net/weixin_37517391/article/details/78374840

非常清楚与详尽,点赞


#include <bits/stdc++.h>
using namespace std ;
const int maxn = 15 ;


struct node{
    int cap ;
    int num ;
    int max_vector[maxn] ;
    int min_vector[maxn] ;

    node(int k){
        cap = k ;
        for(int i = 0 ; i < cap ; i ++ ){
            scanf("%d" , &max_vector[i]) ;
            min_vector[i] = max_vector[i] ;
        }
        num = 1 ;
    }

    bool operator < ( const node & k ) const {
        for(int i = 0 ; i < cap ; i ++ ){
            if(max_vector[i] > k.min_vector[i])
                return false ;
        }
        return true ;
    }

    void unite( const node & k ) {
        for(int i = 0 ; i < cap ; i ++ ){
            min_vector[i] = min(min_vector[i] , k.min_vector[i]) ;
            max_vector[i] = max(max_vector[i] , k.max_vector[i]) ;
        }
        num += k.num ;
    }
};
set<node> save ;
int main(){

    int n , k ;
    scanf("%d %d" , &n , &k) ;
    save.clear() ;

    for(int i = 0 ; i < n ; i ++ ){

        node temp = node(k) ;

        auto l = save.lower_bound(temp) ;
        auto r = save.upper_bound(temp) ;
        //r -- ;
        while(l != r){
            temp.unite(*l) ;

            l = save.erase(l) ;
        }
        save.insert(temp) ;
        printf("%d\n" , (save.rbegin())->num) ;
        
    }
    return 0 ;
}



一下为g++ 6.4版本对set的部分尝试,

注意迭代器如果要从末尾遍历的化要用 rstart()方法,没有end()-1

以及auto可以存储地址,但是不能输出,

erase()的返回下一个值

注意* 与->的使用


#include <bits/stdc++.h>
using namespace std ;
const int maxn = 15 ;


struct node{
    int a ;
    int b ;
    node(int a1 , int b1){
        a = a1 , b = b1 ;
    }

    bool operator < ( const node & k ) const {
        return a < k.a ;
    }
};
set<node> s2 ;
set<int> s ;
int main(){
    s.insert(1) ;
    cout << *s.lower_bound(2) << " " << *s.upper_bound(2) << endl ;

    s.insert(2) ;
    cout << *s.lower_bound(2) << " " << *s.upper_bound(2) << endl ;

    s.insert(3) ;
    cout << *s.lower_bound(2) << " " << *s.upper_bound(2) << endl ;

    auto l = s.lower_bound(2) ;

    l = s.erase(l) ;

    auto l1 = s.rbegin() ; /// meaningfully has a s.rend()
    ///auto l2 = s.end() - 1 ; is not possible to use

    set<int> :: iterator it ;
    for(it = s.begin() ; it != s.end() ; it ++ ){
        cout << *it << endl ;
    }
    /// if set<node>  should use it->element
    
    return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值