51Nod-1467-旋转绳
来源:互联网 发布:ubuntu安装pdf阅读器 编辑:程序博客网 时间:2024/04/30 15:06
ACM模版
描述
题解
这个题倒是不难,主要就是二分,并不像平时做的那种海量查询的问题那样,需要预处理优化、线段树优化之类的。
一开始看到数据这么大,访问次数这么多,就感觉是需要预处理的,可是想半天也没有想到怎么处理,后来发现是二分,感觉很出乎意料,二分竟然能过,写好后提交了两次超时了,因为这个题卡 I/O
了,加上输入和输出外挂后,顺利 AC,只用了
这个题远没有我一开始想的那么复杂,但是 AC 人数着实有些少,让我感觉这个题很神秘,所以一开始想岔了。
代码
#include <iostream>#include <stdio.h>#include <cmath>#include <algorithm>using namespace std;const int MAXN = 2e5 + 10;int n, m;int pos[MAXN];struct snag{ int x, pos; bool operator < (const snag &b) const { return x < b.x; }} sg[MAXN];int bs(int x){ int l = 1, r = n, res = n + 1; while (l <= r) { int m = (l + r) >> 1; if (sg[m].x > x) { res = m; r = m - 1; } else { l = m + 1; } } return res;}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;}template <class T>inline void out_d(T a){ // 输出外挂 if (a < 0) { putchar('-'); a = -a; } if (a >= 10) { out_d(a / 10); } putchar(a % 10 + '0');}int main(){ scan_d(n), scan_d(m); for (int i = 1; i <= n; i++) { scan_d(sg[i].x); sg[i].pos = i; } sort(sg + 1, sg + n + 1); for (int i = 1; i <= n; i++) { pos[sg[i].pos] = i; } int x, flag, now, len; for (int i = 1; i <= m; i++) { scan_d(x), scan_d(len); x = pos[x]; flag = 1; now = bs(len + sg[x].x) - 1; len -= sg[now].x - sg[x].x; flag = -1; x = now; while (len) { if (flag == 1) { now = bs(len + sg[x].x) - 1; } else { now = bs(sg[x].x - len); } if (now && sg[now - 1].x == sg[x].x - len) { now--; } if (now == x) { break; } int y = abs(sg[now].x - sg[x].x); if ((len / y) & 1) { x = now; flag = -flag; } len %= y; } out_d(sg[x].pos); putchar(10); } return 0;}
阅读全文
0 0
- 51Nod-1467-旋转绳
- 51NOD 1467 旋转绳 二分查找 模拟
- 51Nod 旋转字符串
- 51nod 1347 旋转字符串
- 51nod 1347 旋转字符串
- 51nod 1347 旋转字符串
- 1347 旋转字符串【51NOD】
- 51nod 1347 旋转字符串
- 51nod-【1347 旋转字符串】
- 51nod 1347 旋转字符串
- 51Nod 1347 旋转字符串
- 51NOD 1347 旋转字符串
- 【51Nod】1347 旋转字符串
- 51NOD 1347 旋转字符
- 51nod 1347 旋转字符串
- 51nod 1347 旋转字符串
- 51Nod 1347 旋转字符串
- 51NOD-1437-旋转字符串
- flash AS3.0实现进度条与播放暂停按钮的同步
- 别人的 App 不好用?自己改了便是。嘿嘿嘿 篇
- Windows环境下搭建SVN服务器
- Marlin-1.1.3固件Configuration.h文件解析
- 浙江省第八届微型党课大赛圆满落幕
- 51Nod-1467-旋转绳
- 史上最全的架构师图谱
- cmd命令登录mysql
- MVC与单元测试实践之健身网站(六)-计划的添加与重置
- OpenStack构建企业私有云-环境准备
- redis-[2]-redis中key操作
- Android 自定义HorizontalListview
- Linux 移动或重命名文件/目录---mv命令
- java中主函数解析