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

对于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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值