Codeforces Round #310 (Div. 1) C Case of Chocolate
2015-06-28 16:04
337 查看
思路:对于每个点而言、只与它相邻的两个点有关系、所以可以用stl或者线段树来找到它的相邻点、
代码:187ms(开挂之后貌似是最快的- -)
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int N = 200000 + 1;
int x
, y
, t
;
//适用于正负整数
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if(c = getchar(),c == EOF) return 0; //EOF
while(c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline void out(int x) {
if(x > 9) out(x / 10);
putchar(x % 10 + '0');
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
x[q] = y[q] = 0;
map <int, int> mp;
mp[0] = q;
mp[n + 1] = q;
for (int i = 0; i < q; ++ i) {
char buffer[2];
scan_d(x[i]);
scan_d(y[i]);
scanf("%s",buffer);
t[i] = *buffer == 'U';
map <int, int>::iterator iterator;
iterator = mp.lower_bound(x[i]);
if (x[i] == iterator->first) {
puts("0"); continue;
}
if (!t[i]) iterator --;
int j = iterator->second;
mp[x[i]] = i;
if (t[i]) {
out(y[i] - y[j]);
putchar('\n');
y[i] = y[j];
} else {
out(x[i] - x[j]);
putchar('\n');
x[i] = x[j];
}
}
return 0;
}
代码:187ms(开挂之后貌似是最快的- -)
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int N = 200000 + 1;
int x
, y
, t
;
//适用于正负整数
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if(c = getchar(),c == EOF) return 0; //EOF
while(c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline void out(int x) {
if(x > 9) out(x / 10);
putchar(x % 10 + '0');
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
x[q] = y[q] = 0;
map <int, int> mp;
mp[0] = q;
mp[n + 1] = q;
for (int i = 0; i < q; ++ i) {
char buffer[2];
scan_d(x[i]);
scan_d(y[i]);
scanf("%s",buffer);
t[i] = *buffer == 'U';
map <int, int>::iterator iterator;
iterator = mp.lower_bound(x[i]);
if (x[i] == iterator->first) {
puts("0"); continue;
}
if (!t[i]) iterator --;
int j = iterator->second;
mp[x[i]] = i;
if (t[i]) {
out(y[i] - y[j]);
putchar('\n');
y[i] = y[j];
} else {
out(x[i] - x[j]);
putchar('\n');
x[i] = x[j];
}
}
return 0;
}
相关文章推荐
- semaphore信号灯
- php查询mysql并缓存到redis
- python实现简单ftp客户端的方法
- atitit.企业管理----商业间谍策略的使用与防务
- atitit.企业管理----商业间谍策略的使用与防务
- unity, remove a scene from build settings
- vnc的使用
- 简单分页存储过程
- javascript获取动态加载图片的宽度和高度?
- 数据表的约束
- Codeforces 556B Case of Fake Numbers 数字转盘
- LDA 线性判别分析
- Oracle Parallel 多线程
- kernal linear regression
- 启动oracle数据库
- SQL存储过程、触发器和游标
- python生成uuid,并去掉中间的'-'
- (原创)《Android编程权威指南》学习笔记01-- Android应用初体验--007
- 内部类 匿名内部类与接口
- 纸上谈兵: 图 (graph)