Codeforces534E:Berland Local Positioning System

In Berland a bus travels along the main street of the capital. The street begins from the main square and looks like a very long segment. There are n bus
stops located along the street, the i-th of them is located at the distance ai from
the central square, all distances are distinct, the stops are numbered in the order of increasing distance from the square, that is, ai?<?ai?+?1 for
all i from 1 to n?-?1. The bus starts its journey
from the first stop, it passes stops 2, 3 and so on. It reaches the
stop number n, turns around and goes in the opposite direction to stop 1,
passing all the intermediate stops in the reverse order. After that, it again starts to move towards stop n. During the day, the bus runs non-stop on this
route.

The bus is equipped with the Berland local positioning system. When the bus passes a stop, the system notes down its number.

One of the key features of the system is that it can respond to the queries about the distance covered by the bus for the parts of its path between some pair of stops. A special module of the system takes the input with the information about a set of stops
on a segment of the path, a stop number occurs in the set as many times as the bus drove past it. This module returns the length of the traveled segment of the path (or -1 if it is impossible to determine the length uniquely). The operation of the module is
complicated by the fact that stop numbers occur in the request not in the order they were visited but in the non-decreasing order.

For example, if the number of stops is 6, and the part of the bus path starts at the bus stop number 5,
ends at the stop number 3 and passes the stops as follows: ,
then the request about this segment of the path will have form: 3,?4,?5,?5,?6. If the bus on the segment of the path from stop 5 to
stop 3 has time to drive past the 1-th stop (i.e., if we consider
a segment that ends with the second visit to stop 3 on the way from 5),
then the request will have form: 1,?2,?2,?3,?3,?4,?5,?5,?6.

You will have to repeat the Berland programmers achievement and implement this function.

Input

The first line contains integer n (2?≤?n?≤?2·105)
— the number of stops.

The second line contains n integers (1?≤?ai?≤?109)
— the distance from the i-th stop to the central square. The numbers in the second line go in the increasing order.

The third line contains integer m (1?≤?m?≤?4·105)
— the number of stops the bus visited on some segment of the path.

The fourth line contains m integers (1?≤?bi?≤?n)
— the sorted list of numbers of the stops visited by the bus on the segment of the path. The number of a stop occurs as many times as it was visited by a bus.

It is guaranteed that the query corresponds to some segment of the path.

Output

In the single line please print the distance covered by a bus. If it is impossible to determine it unambiguously, print ?-?1.

Sample test(s)

input

6
2 3 5 7 11 13
5
3 4 5 5 6

output

10

input

6
2 3 5 7 11 13
9
1 2 2 3 3 4 5 5 6

output

16

input

3
10 200 300
4
1 2 2 3

output

-1

input

3
1 2 3
4
1 2 2 3

output

3

Note

The first test from the statement demonstrates the first example shown in the statement of the problem.

The second test from the statement demonstrates the second example shown in the statement of the problem.

In the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn‘t defined uniquely.

In the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely.

题意:给出n个车站离中央广场的距离,然后给出公交车对于各个车站的访问情况,问公交车走过的路程是否唯一,唯一输出路程,不唯一输出-1

思路:对于输出-1的情况,只有在对于每个车站的访问次数都一样,而且两个车站的距离有差异的情况下才成立

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 200005
#define mod 19999997
const int INF = 0x3f3f3f3f;

LL n,m,a[Len],b[2*Len],hsh[Len];

int main()
{
    LL i,j,k,minn,maxn,ans;
    bool flag;
    mem(hsh,0);
    scanf("%I64d",&n);
    up(i,1,n)
    scanf("%I64d",&a[i]);
    scanf("%I64d",&m);
    minn = INF,maxn = 0;
    up(i,1,m)
    {
        scanf("%I64d",&b[i]);
        hsh[b[i]]++;
        if(b[i]==1 || b[i]==n)
            hsh[b[i]]++;
    }
    up(i,1,n)
    {
        minn = min(minn,hsh[i]);
        maxn = max(maxn,hsh[i]);
    }
    if(minn == maxn)
    {
        flag = true;
        up(i,1,n-2)
        {
            if(a[i+1]-a[i]!=a[i+2]-a[i+1])
            {
                flag = false;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
        else
            printf("%I64d\n",(a[n]-a[1])*minn-(a[2]-a[1]));
    }
    else
    {
        LL ans = 0;
        up(i,1,n-1)
        {
            ans+=(a[i+1]-a[i])*min(hsh[i+1],hsh[i]);
        }
        printf("%I64d\n",ans);
    }

    return 0;
}
时间: 2024-10-12 13:42:46

Codeforces534E:Berland Local Positioning System的相关文章

The ‘Microsoft.ACE.OLEDB.12.0′ provider is not registered on the local machine. (System.Data)

When you try to import Excel 2007 or later “.xlsx” files into an SQL Server 2008 database you may get the following error mesage : The ‘Microsoft.ACE.OLEDB.12.0′ provider is not registered on the local machine. (System.Data) To solve this problem ins

Manhattan Positioning System --- Gym - 101490G

题目 https://vjudge.net/problem/Gym-101490G 题意 给出 n 个信标的二维坐标 ( X,Y ) 以及唯一接收器距离每个信标的曼哈顿距离 D ( ( x1, y1 ) 和 ( x2, y2 ) 的曼哈顿距离为 | x1 - x2 | + | y1 - y2 | ),问是否能确定接收器的位置. 题解 每个信标用曼哈顿距离可以构建出一个旋转 45° 的正方形,取随意一个正方形,用 ( x1, y1 ),( x2, y2 ) 来表示每条边构成边集,将该边集与每个正方

HDFS F ile System Shell Guide

Overview appendToFile cat checksum chgrp chmod chown copyFromLocal copyToLocal count cp createSnapshot deleteSnapshot df du dus expunge find get getfacl getfattr getmerge help ls lsr mkdir moveFromLocal moveToLocal mv put renameSnapshot rm rmdir rmr

HDFS分布式文件系统(The Hadoop Distributed File System)

The Hadoop Distributed File System (HDFS) is designed to store very large data sets reliably, and to stream those data sets at high bandwidth to user applications. In a large cluster, thousands of servers both host directly attached storage and execu

DFS(distributed file system)

A clustered file system is a file system which is shared by being simultaneously mounted on multiple servers. There are several approaches to clustering, most of which do not employ a clustered file system (only direct attached storage for each node)

PatentTips - Safe general purpose virtual machine computing system

BACKGROUND OF THE INVENTION The present invention relates to virtual machine implementations, and in particular to a safe general purpose virtual machine that generates optimized virtual machine computer programs that are executable in a general purp

File System Shell

Overview appendToFile cat chgrp chmod chown copyFromLocal copyToLocal count cp du dus expunge get getfacl getmerge ls lsr mkdir moveFromLocal moveToLocal mv put rm rmr setfacl setrep stat tail test text touchz Overview The File System (FS) shell incl

Modeling of Indoor Positioning Systems Based on Location Fingerprinting

Kamol Kaemarungsi and Prashant Krishnamurthy Telecommunications Program School of Information Science. University of Pittsburgh 135 N. Bellefield Avenue, Pittsburgh. Pennsylvani Given a set of system parameters and radio propagation characteristics.

Global and Local Coordinate Systems

ansys 中的坐标系 整体和局部坐标系(主要在建模中涉及) 整体坐标系是以你建模的整个建筑为一体,来确定坐标系的.比如你建一个矩形平面的建筑,整体坐标系一般默认水平方向为X轴,竖直方向为Y轴,以垂直图面的方向为Z轴.局部坐标系是以单个构件为概念的设置的.比如说对一个工字形的钢柱,局部坐标系的X轴通常为这个截面的强轴方向,对于工字形柱来说一般就是与腹板垂直的方向:其Y轴为截面的弱轴方向,对于工字形柱来说一般就是与腹板平行的方向:Z轴对于梁柱等杆系构件来说一般是沿杆件的长度方向,对于板构件来说一般