Codeforces 555C. Case of Chocolate (SET应用)

来源:互联网 发布:珠宝设计软件培训 编辑:程序博客网 时间:2024/06/02 05:23

对于U的操作,能到达的y的上界和x值大于它最接近的操作的y的上界是一样的:

1.如果是L的操作,上界就是那个L操作的y值。

2.如果是U的操作,上界就是这次U操作所能到达的上界。


对于L的操作,则是找y值大于它最近的操作。

所以建立两个map U,L,一个key值是x,一个是y,因为map的lower_bound是按照key值查找的。


对于U操作,结束后U要插入pair(x,能到达的y值),在L中插入(y,x)

对于L操作 ,结束后L要插入pair(y,能到达的x值),在U中插入(x,y)


这个可以画一下图方便理解。


map知识:

map的元素是pair,用迭代器找到后使用first,second来调用元素的key和value

使用count来判断key存不存在

如果没有大于等于某元素的,lower_bound返回end()


代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#include <set>#include <map>int N,Q;map<int,int> U;map<int,int> L;int main(){    scanf("%d%d",&N,&Q);    map<int,int>::iterator p;    for(int i=0;i<Q;i++){        int x,y;        char cm[2];        scanf("%d%d%s",&x,&y,cm);        if(cm[0]=='U'){            if(U.count(x)){                printf("0\n");                continue;            }            p=U.lower_bound(x);            int res;            if(p==U.end()){                res=y;            }            else {                res=y-p->second;            }            printf("%d\n",res);            U[x]=y-res;            L[y]=x;        }        else {            if(L.count(y)){                printf("0\n");                continue;            }            int res;            p=L.lower_bound(y);            if(p==L.end()){                res=x;            }            else {                res=x-p->second;            }            printf("%d\n",res);            L[y]=x-res;            U[x]=y;        }    }    return 0;}


0 0
原创粉丝点击