Problem Description
Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is.
 
Input
The input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events. 
The next n lines, each line describes an event. 0 x(0<=x<=L, x is a integer) represents a piece of cake appears in the x position; 1 represent Holedox wants to eat a cake.
In each case, Holedox always starts off at the position 0.
 
Output
Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
 
Sample Input
3
10 8
0 1
0 5
1
0 2
0 0
1
1
1

10 7
0 1
0 5
1
0 2
0 0
1
1

10 8
0 1
0 1
0 5
1
0 2
0 0
1
1

 
Sample Output
Case 1: 9
Case 2: 4
Case 3: 2
 
Author
BUPT
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4301 4303 4304 4308 4305 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<cstring>
using namespace std;
#define MAXN 500010
#define INF 0x3f3f3f3f
typedef long long LL;
/*
模拟 map每次lower_bound
*/
map<int, int> m;
int p, T, L, n, eat, tmp, ans;
int main()
{
scanf("%d", &T);
for(int cas = ;cas<=T;cas++)
{
m.clear();
scanf("%d%d", &L, &n);
ans = , p = ;
int add1 ,add2;
bool dir;
for (int i = ; i < n; i++)
{
scanf("%d", &eat);
if (eat)
{
if (m.empty()) continue;
add1 = add2 = INF;
map<int, int>::iterator it = m.lower_bound(p);
if (it != m.end())
add1 = min(add1, abs(it->first - p));
if (it != m.begin())
it--, add2 = min(add2, abs(it->first - p));
if (add1 != INF&&add1 == add2)
{
if (dir) p = p + add1;
else p = p - add1;
ans += add1;
}
else if (add1 < add2)
{
if (add1) dir = true;
p = p + add1, ans += add1;
}
else
{
if (add2) dir = false;
p = p - add2, ans += add2;
}
if (--m[p] == )
m.erase(p);
// cout << ':' << ans <<"at" << p << endl;
}
else
scanf("%d", &tmp), m[tmp]++;
}
printf("Case %d: %d\n", cas, ans);
}
}

最新文章

  1. git: 修改commiter 信息
  2. 10年微软MVP路(如何成为一个MVP?)
  3. SQL Server 2014 Backup Encryption
  4. Fragment实现兼容手机和平板
  5. nodejs的mysql模块学习(四)断开数据库连接
  6. Android开发了解——AIDL
  7. LogBoy 之Android Studio控制台输出日志太多清空
  8. HDOJ Sudoku Killer(dfs)
  9. D - 小晴天老师系列——晴天的后花园
  10. 使用Jmeter进行http接口测试 ---------成都杀手
  11. GridView中添加行单击事件.md
  12. Dart 中dynamic 的使用
  13. jquery考试成绩统计系统
  14. linux之tail和head的使用
  15. LeetCode: Palindrome Partitioning II 解题报告
  16. jmxtrans
  17. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
  18. Android之开源中国客户端源码分析(一)
  19. Spark构成
  20. WPF: RenderTransform特效

热门文章

  1. [SHOI2013]超级跳马
  2. pyinstaller打包报错:AttributeError: &#39;str&#39; object has no attribute &#39;items&#39;
  3. Snipaste强大离线/在线截屏软件的下载、安装和使用
  4. 19 C#循环语句的跳过和中断 continue和break
  5. 配置JDK、tomcat及Java Web项目部署
  6. const学习(续)
  7. Python中*args和**kwargs的使用
  8. Java集合框架源码(一)——hashMap
  9. Greenplum开发
  10. CPU位数、操作系统位数、机器字长、C/C++基本数据类型长度