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
- Codeforces 555C. Case of Chocolate (SET应用)
- codeforces #310 555C C. Case of Chocolate(set的应用)
- codeforces 555C Case of Chocolate set操作
- codeforces 555 C Case of Chocolate
- codeforces 555C Case of Chocolate(map)
- Codeforces 555C Case of Chocolate
- CodeForces 555C Case of Chocolate(STL:map的使用)
- Codeforces Round #310 (Div. 1) C. Case of Chocolate stl应用
- Codeforces Round #310 (Div. 1) C Case of Chocolate
- 线段树 Case of Chocolate:CodeForces
- Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)
- [Codeforces Round #310 DIV1C (CF555C)] Case of Chocolate
- CF#310-E - Case of Chocolate-(set+贪心)/(线段树+离散化)/(隐式线段树)
- Codeforces #310 div2 C. Case of Matryoshkas
- CodeForces-556C Case of Matryoshkas
- Codeforces 556C Case of Matryoshkas【模拟】
- 【35.37%】【codeforces 556C】Case of Matryoshkas
- Codeforces Round #310 (Div. 1) B. Case of Fugitive(set二分)
- uva11059 Maximum Product
- Java基础(一)妙用枚举类
- 对pair元素进行排序
- Android 使用android-support-multidex解决Dex超出方法数的限制问题
- 五种创建UIImage的类方法
- Codeforces 555C. Case of Chocolate (SET应用)
- Spring MVC学习-----------前言
- 单例模式两种实现方式
- git 简单命令操作
- 01背包问题
- Android 学习笔记 Contacts (一)ContentResolver query 参数详解
- Qt实战之开发软件数据获取助手
- 我自己对指针的理解
- netty4源码分析——pipeline关联