set用法

set的用法一直拖着,今天才算真正会了,小小总结一下(我好菜啊啊啊)

#include<bits/stdc++.h>

using namespace std;
set<int> s;int n,x,a[15000];

int main(){
    freopen("7.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),s.insert(a[i]);
    
    set<int>::iterator it;
    //遍历 
    for(it=s.begin();it!=s.end();it++) 
    //s.end()实际上没有值,s.end()迭代器指向的东西已经超出数组本身内容,
    //若要返回的值实际上是set的size 
    cout<<*it<<" ";cout<<endl;
    //查找值所对应的迭代器 
    it=s.lower_bound(3);
    //对于无重复元素的set lower_bound和upper_bound无区别 
    //输出迭代器所指向的值 
    cout<<*it<<endl; return 0;
}
//5
//8 4 2 6 10
 

例题:

巧克力

【问题背景】

平面直角坐标系中有一块长方形的巧克力。左下角坐标是(0,0),右上角坐标是(n,m)现在要在这块巧克力上切p求每次切完后最大的一块完整的巧克力的面积

【输入格式】

     第一行三个正整数n,m,p。表示巧克力的大小和要切割的次数

     以下p行每行一个大写字母ci和一个整数ai

ci=V’则表示从x=ai处切割;若ci=H’则表示从y=ai处切割。

【输出格式】

     输出一共p行,第i行为第i次切割后的答案。

【输入样例】

7 6 5

H 4

V 3

V 5

H 2

V 1

【输出样例】

 28

16

12

6

4

样例说明

 

【数据范围】

     50%的数据:p<=1000

     100%的数据:pm,n<=100000。

思路:naive的我开始真的啥都不会啊,暴力写了个sort(因为真的不知道set能自动排序....辣鸡如我)

code 1   50分

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=100050;
int n,m,p,k,top1,top2,FG,x[N],y[N];char opt[5];
LL mx;

void find(){mx=0;
    if(FG==1) sort(x+1,x+1+top1);
    if(FG==2) sort(y+1,y+1+top2);
    for(int i=1;i<=top1;i++) 
    for(int j=1;j<=top2;j++)
    mx=max(mx,(LL)(x[i]-x[i-1])*(y[j]-y[j-1]));
}

int main(){
    freopen("chocolate.in","r",stdin);
    freopen("chocolate.out","w",stdout);
    scanf("%d%d%d",&n,&m,&p);
    x[++top1]=n;y[++top2]=m;
    for(register int i=1;i<=p;i++){
        scanf("%s%d",opt,&k);
        if(opt[0]=='V') x[++top1]=k,FG=1;
        if(opt[0]=='H') y[++top2]=k,FG=2;
        find();printf("%lld\n",mx);
    }
    return 0;
} 

后来胡乱瞄了一眼std发现要用set,所以写了如下的set代码,(真的巨麻烦)

set维护位置,priority_queue维护大小,但是优先队列不能遍历,所以就打标记代表它已经被用掉了,虽然麻烦点但是不知道为甚恶魔有些WA掉了,40分(还不如暴力sort,嘤嘤嘤)

code2 40分

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N=1e6+50;
struct nodex{
    int l,r,siz,id;
    bool operator<(const nodex& a)const{
    return siz<a.siz;}
};priority_queue<nodex> qx,qy;

set<int> sx,sy;
char opt[10];
int n,m,p,k,cntx[N],cnty[N];

void changex(int k2){
    set<int>::iterator it1,it2;
    it2=sx.lower_bound(k2); 
    it1=--it2;++it2;
    
    int k1=*it1,k3=*it2;  sx.insert(k2);
    qx.push((nodex){k1,k2,k2-k1,++cntx[k1]});
    qx.push((nodex){k2,k3,k3-k2,++cntx[k2]});
}
void changey(int k2){
    set<int>::iterator it1,it2;
    it2=sy.lower_bound(k2); 
    it1=--it2;++it2;
    
    int k1=*it1,k3=*it2;sy.insert(k2);
    qy.push((nodex){k1,k2,k2-k1,++cnty[k1]});
    qy.push((nodex){k2,k3,k3-k2,++cnty[k2]});
}
int main(){
    freopen("chocolate.in","r",stdin);
    freopen("chocolate.out","w",stdout);
    scanf("%d%d%d",&n,&m,&p);
    sx.insert(0);sx.insert(n);qx.push((nodex){0,n,n,++cntx[0]});
    sy.insert(0);sy.insert(m);qy.push((nodex){0,m,m,++cnty[0]});
    for(int i=1;i<=p;i++){
        scanf("%s%d",opt,&k);
        if(opt[0]=='V')  changex(k);
        if(opt[0]=='H')  changey(k);
        int mxx=0,mxy=0;
        while(!qx.empty()){
            nodex u=qx.top();
            if(u.id!=cntx[u.l]) qx.pop();
            else{mxx=u.siz;break;}
        }
        while(!qy.empty()){
            nodex u=qy.top();
            if(u.id!=cnty[u.l]) qy.pop();
            else{mxy=u.siz;break;}
        }
        printf("%lld\n",(LL)mxx*mxy);
    }
    return 0;
}

最后认真看了题解,学了set发现,这题不就是个大水题么,multiset轻松搞过,,,(辣鸡如我*2)

code 3 AC

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N=1e6+50;

set<int> sx,sy; 
multiset<int> mx,my;
char opt[10];int n,m,p,k;

int main(){
    freopen("chocolate.in","r",stdin);
    freopen("chocolate.out","w",stdout);
    scanf("%d%d%d",&n,&m,&p);
    sx.insert(0);sx.insert(n);
    sy.insert(0);sy.insert(m);
    mx.insert(n);my.insert(m);
    for(int i=1;i<=p;i++){
        scanf("%s%d",opt,&k);
        int mxx=0,myy=0;
        if(opt[0]=='V'){
            set<int>::iterator it;
            it=sx.lower_bound(k);
            int v=*it,u=*(--it);
            sx.insert(k);
            
            mx.erase(mx.lower_bound(v-u));
            mx.insert(v-k);
            mx.insert(k-u);
        }if(opt[0]=='H'){
            set<int>::iterator it;
            it=sy.lower_bound(k);
            int v=*it,u=*(--it);
            sy.insert(k);
            
            my.erase(my.lower_bound(v-u));
            my.insert(v-k);
            my.insert(k-u);
        }mxx=*(mx.rbegin());myy=*(my.rbegin());
        printf("%lld\n",(LL)mxx*myy);
    }
    return 0;
}

完结撒花!!

 

posted @ 2018-09-30 09:38  ASDIC减除  阅读(208)  评论(0编辑  收藏  举报