1052: Holedox Eating
Time Limit: 1 Sec Memory Limit: 128 MB[Submit][Status][Web Board]
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
Sample Output
【分析】
不过感觉这个方法不是出题者希望的解题方法吧..感觉应该要用线段树..不过set复杂度没问题所以直接敲了set
#include <iostream>
#include <algorithm>
#include <set>
#include <cstdio>
#define INF 0x7ffffff
using namespace std;
int main()
{
int t;
long long sum;
int L,n;
int a,b;
long long m,p;
int pp;scanf("%d",&pp);
for(int p=1;p<=pp;p++)
{
scanf("%d%d",&L,&n);
sum=0;
multiset<int>s;
multiset<int>::iterator it,d;
int cur=0,pre=0;
while(n--)
{
scanf("%d",&a);
if(a==0)
{
scanf("%d",&b);
s.insert(b);
}
else
{
m=INF;
if(s.empty()) continue;
for(it=s.begin();it!=s.end();it++)
{
if(abs(*it-cur)<m)
{
m=abs((*it)-cur);
d=it;
}
else
if(abs(*it-cur)==m)
{
if((*it-cur)*(cur-pre)>0)
{
m=abs((*it)-cur);
d=it;
}
}
}
sum+=m;
pre=cur;
cur=*d;
s.erase(d);
}
}
printf("Case %d: %lld\n",p,sum);
}
return 0;
}