{"id":5038,"date":"2024-10-01T16:01:01","date_gmt":"2024-10-01T08:01:01","guid":{"rendered":""},"modified":"2024-10-01T16:01:01","modified_gmt":"2024-10-01T08:01:01","slug":"\u8d2a\u5fc301\u80cc\u5305_wand","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5038.html","title":{"rendered":"\u8d2a\u5fc301\u80cc\u5305_wand"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

The cities of Byteland and Berland are located on the axis \ud835\udc42\ud835\udc65. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line \ud835\udc42\ud835\udc65 there are three types of cities:<\/p>\n

the cities of Byteland,
the cities of Berland,
disputed cities.
Recently, the project BNET has been launched \u2014 a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.<\/p>\n

The countries agreed to connect the pairs of cities with BNET cables in such a way that:<\/p>\n

If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables,
If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables.
Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.<\/p>\n

The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.<\/p>\n

Each city is a point on the line \ud835\udc42\ud835\udc65. It is technically possible to connect the cities \ud835\udc4e and \ud835\udc4f with a cable so that the city \ud835\udc50 (\ud835\udc4e<\ud835\udc50<\ud835\udc4f) is not connected to this cable, where \ud835\udc4e, \ud835\udc4f and \ud835\udc50 are simultaneously coordinates of the cities \ud835\udc4e, \ud835\udc4f and \ud835\udc50.<\/p>\n

Input
The first line contains a single integer \ud835\udc5b (2\u2264\ud835\udc5b\u22642\u22c5105) \u2014 the number of cities.<\/p>\n

The following \ud835\udc5b lines contains an integer \ud835\udc65\ud835\udc56 and the letter \ud835\udc50\ud835\udc56 (\u2212109\u2264\ud835\udc65\ud835\udc56\u2264109) \u2014 the coordinate of the city and its type. If the city belongs to Byteland, \ud835\udc50\ud835\udc56 equals to \u2018B\u2019. If the city belongs to Berland, \ud835\udc50\ud835\udc56 equals to \u00abR\u00bb. If the city is disputed, \ud835\udc50\ud835\udc56 equals to \u2018P\u2019.<\/p>\n

All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.<\/p>\n

Output
Print the minimal total length of such set of cables, that if we delete all Berland cities (\ud835\udc50\ud835\udc56=\u2018R\u2019), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities (\ud835\udc50\ud835\udc56=\u2018B\u2019), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.<\/p>\n

Examples
inputCopy
4
-5 R
0 P
3 P
7 B
outputCopy
12
inputCopy
5
10 R
14 B
16 B
21 R
32 R
outputCopy
24
Note
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be 5+3+4=12.<\/p>\n

In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates 10,21,32, so to connect them you need two cables of length 11 and 11. The cities of Byteland have coordinates 14 and 16, so to connect them you need one cable of length 2. Thus, the total length of all cables is 11+11+2=24.<\/p>\n

\"\u8d2a\u5fc301\u80cc\u5305_wand
\u4ece\u6628\u665a\u4e00\u76f4\u6539\u5230\u73b0\u5728\uff0cA\u4e86\uff0c\u8981\u54ed\u51fa\u6765\u4e86o(\u2565\ufe4f\u2565)o<\/p>\n

\u9898\u610f\uff1a<\/strong>
B,R,P\u4e09\u79cd\u7c7b\u578b\u57ce\u5e02\uff0c\u6bcf\u4e2a\u57ce\u5e02\u4f4d\u7f6e\u5404\u4e0d\u540c\u3002
\u8981\u6c42\u5728\u57ce\u5e02\u4e4b\u95f4\u5efa\u8fb9\uff0c\u957f\u5ea6\u4e3a\u8ddd\u79bb\u957f\u5ea6\u3002\u4f7f\u5f97\u5220\u9664\u6240\u6709B\u57ce\u5e02\u540e\uff0cR\u3001P\u76f8\u4e92\u8fde\u901a\uff1b\u5220\u9664R\u57ce\u5e02\u540e\uff0cB\u3001P\u76f8\u4e92\u8fde\u901a\u3002
\u6c42\u5efa\u8fb9\u7684\u6700\u5c0f\u957f\u5ea6\u548c\u3002<\/p>\n

\u601d\u8def\uff1a<\/strong>
\u9996\u5148\u4ece\u7b80\u5355\u60c5\u51b5\u7740\u624b\uff1a
\u5047\u8bbe\u6ca1\u6709P\u57ce\u5e02\u7684\u60c5\u51b5\uff0c\u90a3\u4e48\u7ed3\u679c\u5c31\u662fB\u57ce\u5e02\u8fde\u63a5\u548c\u52a0\u4e0aR\u57ce\u5e02\u8fde\u63a5\u548c\u3002
\u5047\u8bbe\u6ca1\u6709R\u57ce\u5e02\u7684\u60c5\u51b5\uff0c\u90a3\u4e48\u7ed3\u679c\u5c31\u662fB\u57ce\u5e02\u4e0eP\u57ce\u5e02\u8fde\u5728\u4e00\u8d77\u7684\u8fde\u63a5\u548c\u3002
\u5047\u8bbe\u6ca1\u6709B\u57ce\u5e02\u7684\u60c5\u51b5\uff0c\u90a3\u4e48\u7ed3\u679c\u5c31\u662fR\u57ce\u5e02\u4e0eP\u57ce\u5e02\u8fde\u5728\u4e00\u8d77\u7684\u8fde\u63a5\u548c\u3002<\/p>\n

\u4e8e\u662f\u53ef\u4ee5\u60f3\u5230\uff0c\u53ea\u8981\u5c06B\u548cP\u4f9d\u6b21\u8fde\u63a5\u8d77\u6765\uff0c\u518d\u628aR\u548cP\u4f9d\u6b21\u8fde\u63a5\u8d77\u6765\uff0c\u7ed3\u679c\u4e00\u5b9a\u662f\u6ee1\u8db3\u9898\u610f\u7684\uff0c\u82b1\u8d39\u4e5f\u5c31\u662f M X ( P , B ) \u2212 M I ( P , B ) + M X ( P , R ) \u2212 M I ( P , R ) MX(P,B)-MI(P,B)+MX(P,R)-MI(P,R) <\/span><\/span>M<\/span>X<\/span>(<\/span>P<\/span>,<\/span><\/span>B<\/span>)<\/span><\/span>\u2212<\/span><\/span><\/span><\/span>M<\/span>I<\/span>(<\/span>P<\/span>,<\/span><\/span>B<\/span>)<\/span><\/span>+<\/span><\/span><\/span><\/span>M<\/span>X<\/span>(<\/span>P<\/span>,<\/span><\/span>R<\/span>)<\/span><\/span>\u2212<\/span><\/span><\/span><\/span>M<\/span>I<\/span>(<\/span>P<\/span>,<\/span><\/span>R<\/span>)<\/span><\/span><\/span><\/span><\/span>\u3002\u8003\u8651\u4ece\u8fd9\u91cc\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n

B\u4e4b\u95f4\u7684\u8fde\u63a5\u548cR\u4e4b\u95f4\u7684\u8fde\u63a5\u4e00\u5b9a\u662f\u72ec\u7acb\u7684\uff0c\u5173\u952e\u5c31\u662f\u52a0\u5165\u4e86P\u4ee5\u540e\uff0c\u600e\u4e48\u5229\u7528P\u4e4b\u95f4\u8fde\u7684\u8fb9\u4f18\u5316\u7ed3\u679c\u3002<\/p>\n

\u4e8e\u662f\u6211\u4eec\u4eceP\u7740\u624b\uff0c\u7528P\u5c06\u57ce\u5e02\u5212\u5206\u4e3a X X X P Y Y Y P Z Z Z P N N N\u2026
X\u90e8\u5206\u57ce\u5e02\u4e0e\u7b2c\u4e00\u4e2aP\u76f8\u8fde\uff0cN\u90e8\u5206\u57ce\u5e02\u4e0e\u6700\u540e\u4e00\u4e2aP\u76f8\u8fde\uff0c\u8fd9\u4e2a\u7ed3\u679c\u552f\u4e00\uff0c\u597d\u8003\u8651\u3002<\/p>\n

\u5173\u952e\u5c31\u662f\u76f8\u90bbP\u4e4b\u95f4\u57ce\u5e02\u8981\u600e\u4e48\u8fde\u63a5\u3002<\/p>\n

\u8ba8\u8bba\u4e4b\u540e\u53d1\u73b0\u53ea\u6709\u4e24\u79cd\u6392\u5217\u65b9\u6848\uff0c
\u65b9\u68481\u82b1\u8d39\u4e3aP\u95f4\u8ddd\u79bb\u4e58\u4ee53\u51cf\u53bbR\u95f4\u6700\u5927\u95f4\u8ddd\u548cB\u95f4\u6700\u5927\u95f4\u8ddd
\u65b9\u68482\u82b1\u8d39\u4e3aP\u95f4\u8ddd\u79bb\u4e58\u4ee52
\u5c06\u65b9\u68481\u548c\u65b9\u68482\u82b1\u8d39\u53d6min\u5c31\u662f\u8fde\u63a5\u76f8\u90bb\u4e24\u4e2aP\u7684\u6700\u5c0f\u82b1\u8d39\u4e86\u3002
\"\u8d2a\u5fc301\u80cc\u5305_wand<\/p>\n

\u7531\u6b64\u95ee\u9898\u5f97\u5230\u4e86\u89e3\u51b3\u3002<\/p>\n

#include<\/span><cstdio><\/span><\/span> #include<\/span><cstring><\/span><\/span> #include<\/span><algorithm><\/span><\/span> #include<\/span><vector><\/span><\/span> using<\/span> namespace<\/span> std;<\/span> typedef<\/span> long<\/span> long<\/span> ll;<\/span> const<\/span> int<\/span> maxn =<\/span> 2e5<\/span> +<\/span> 7<\/span>;<\/span> ll a[<\/span>maxn]<\/span>;<\/span> int<\/span> b[<\/span>maxn]<\/span>;<\/span> vector<<\/span>int<\/span>><\/span>pos;<\/span> int<\/span> main<\/span>(<\/span>)<\/span> { \n   <\/span> int<\/span> n;<\/span>scanf<\/span>(<\/span>\"%d\"<\/span>,<\/span>&<\/span>n)<\/span>;<\/span> ll MI1 =<\/span> 2e9<\/span>,<\/span>MX1 =<\/span> -<\/span>2e9<\/span>;<\/span> ll MI2 =<\/span> 2e9<\/span>,<\/span>MX2 =<\/span> -<\/span>2e9<\/span>;<\/span> for<\/span>(<\/span>int<\/span> i =<\/span> 1<\/span>;<\/span>i <=<\/span> n;<\/span>i++<\/span>)<\/span> { \n   <\/span> char<\/span> op[<\/span>10<\/span>]<\/span>;<\/span> scanf<\/span>(<\/span>\"%lld%s\"<\/span>,<\/span>&<\/span>a[<\/span>i]<\/span>,<\/span>op)<\/span>;<\/span> if<\/span>(<\/span>op[<\/span>0<\/span>]<\/span> ==<\/span> 'B'<\/span>)<\/span> { \n   <\/span> b[<\/span>i]<\/span> =<\/span> 1<\/span>;<\/span> MI1 =<\/span> min<\/span>(<\/span>MI1,<\/span>a[<\/span>i]<\/span>)<\/span>;<\/span> MX1 =<\/span> max<\/span>(<\/span>MX1,<\/span>a[<\/span>i]<\/span>)<\/span>;<\/span> }<\/span> else<\/span> if<\/span>(<\/span>op[<\/span>0<\/span>]<\/span> ==<\/span> 'R'<\/span>)<\/span> { \n   <\/span> b[<\/span>i]<\/span> =<\/span> 2<\/span>;<\/span> MI2 =<\/span> min<\/span>(<\/span>MI2,<\/span>a[<\/span>i]<\/span>)<\/span>;<\/span> MX2 =<\/span> max<\/span>(<\/span>MX2,<\/span>a[<\/span>i]<\/span>)<\/span>;<\/span> }<\/span> else<\/span> if<\/span>(<\/span>op[<\/span>0<\/span>]<\/span> ==<\/span> 'P'<\/span>)<\/span> { \n   <\/span> pos.<\/span>push_back<\/span>(<\/span>i)<\/span>;<\/span> b[<\/span>i]<\/span> =<\/span> 3<\/span>;<\/span> }<\/span> }<\/span> ll ans =<\/span> 0<\/span>;<\/span> if<\/span>(<\/span>pos.<\/span>size<\/span>(<\/span>)<\/span> ==<\/span> 0<\/span>)<\/span> { \n   <\/span> if<\/span>(<\/span>MI1 !=<\/span> 2e9<\/span>)<\/span> ans +<\/span>=<\/span> MX1 -<\/span> MI1;<\/span> if<\/span>(<\/span>MI2 !=<\/span> 2e9<\/span>)<\/span> ans +<\/span>=<\/span> MX2 -<\/span> MI2;<\/span> printf<\/span>(<\/span>\"%lld\\n\"<\/span>,<\/span>ans)<\/span>;<\/span> return<\/span> 0<\/span>;<\/span> }<\/span> int<\/span> flag1 =<\/span> 0<\/span>,<\/span>flag2 =<\/span> 0<\/span>;<\/span> for<\/span>(<\/span>int<\/span> i =<\/span> 1<\/span>;<\/span>i <=<\/span> pos[<\/span>0<\/span>]<\/span>;<\/span>i++<\/span>)<\/span> { \n   <\/span> if<\/span>(<\/span>b[<\/span>i]<\/span> ==<\/span> 1<\/span> &&<\/span> !<\/span>flag1)<\/span> { \n   <\/span> flag1 =<\/span> 1<\/span>;<\/span> ans +<\/span>=<\/span> a[<\/span>pos[<\/span>0<\/span>]<\/span>]<\/span> -<\/span> a[<\/span>i]<\/span>;<\/span> }<\/span> if<\/span>(<\/span>b[<\/span>i]<\/span> ==<\/span> 2<\/span> &&<\/span> !<\/span>flag2)<\/span> { \n   <\/span> flag2 =<\/span> 1<\/span>;<\/span> ans +<\/span>=<\/span> a[<\/span>pos[<\/span>0<\/span>]<\/span>]<\/span> -<\/span> a[<\/span>i]<\/span>;<\/span> }<\/span> }<\/span> flag1 =<\/span> 0<\/span>,<\/span>flag2 =<\/span> 0<\/span>;<\/span> for<\/span>(<\/span>int<\/span> i =<\/span> n;<\/span>i >=<\/span> pos[<\/span>pos.<\/span>size<\/span>(<\/span>)<\/span> -<\/span> 1<\/span>]<\/span>;<\/span>i--<\/span>)<\/span> { \n   <\/span> if<\/span>(<\/span>b[<\/span>i]<\/span> ==<\/span> 1<\/span> &&<\/span> !<\/span>flag1)<\/span> { \n   <\/span> flag1 =<\/span> 1<\/span>;<\/span> ans +<\/span>=<\/span> a[<\/span>i]<\/span> -<\/span> a[<\/span>pos[<\/span>pos.<\/span>size<\/span>(<\/span>)<\/span> -<\/span> 1<\/span>]<\/span>]<\/span>;<\/span> }<\/span> if<\/span>(<\/span>b[<\/span>i]<\/span> ==<\/span> 2<\/span> &&<\/span> !<\/span>flag2)<\/span> { \n   <\/span> flag2 =<\/span> 1<\/span>;<\/span> ans +<\/span>=<\/span> a[<\/span>i]<\/span> -<\/span> a[<\/span>pos[<\/span>pos.<\/span>size<\/span>(<\/span>)<\/span> -<\/span> 1<\/span>]<\/span>]<\/span>;<\/span> }<\/span> }<\/span> for<\/span>(<\/span>int<\/span> i =<\/span> 1<\/span>;<\/span>i <<\/span> pos.<\/span>size<\/span>(<\/span>)<\/span>;<\/span>i++<\/span>)<\/span> { \n   <\/span> ll pre1 =<\/span> a[<\/span>pos[<\/span>i -<\/span> 1<\/span>]<\/span>]<\/span>,<\/span>pre2 =<\/span> a[<\/span>pos[<\/span>i -<\/span> 1<\/span>]<\/span>]<\/span>;<\/span> ll mx1 =<\/span> 0<\/span>,<\/span>mx2 =<\/span> 0<\/span>;<\/span> for<\/span>(<\/span>int<\/span> j =<\/span> pos[<\/span>i -<\/span> 1<\/span>]<\/span>;<\/span>j <=<\/span> pos[<\/span>i]<\/span>;<\/span>j++<\/span>)<\/span> { \n   <\/span> if<\/span>(<\/span>b[<\/span>j]<\/span> ==<\/span> 1<\/span>)<\/span> { \n   <\/span> mx1 =<\/span> max<\/span>(<\/span>mx1,<\/span>a[<\/span>j]<\/span> -<\/span> pre1)<\/span>;<\/span> pre1 =<\/span> a[<\/span>j]<\/span>;<\/span> }<\/span> if<\/span>(<\/span>b[<\/span>j]<\/span> ==<\/span> 2<\/span>)<\/span> { \n   <\/span> mx2 =<\/span> max<\/span>(<\/span>mx2,<\/span>a[<\/span>j]<\/span> -<\/span> pre2)<\/span>;<\/span> pre2 =<\/span> a[<\/span>j]<\/span>;<\/span> }<\/span> }<\/span> mx1 =<\/span> max<\/span>(<\/span>mx1,<\/span>a[<\/span>pos[<\/span>i]<\/span>]<\/span> -<\/span> pre1)<\/span>;<\/span> mx2 =<\/span> max<\/span>(<\/span>mx2,<\/span>a[<\/span>pos[<\/span>i]<\/span>]<\/span> -<\/span> pre2)<\/span>;<\/span> ll num1 =<\/span> 2<\/span> *<\/span> a[<\/span>pos[<\/span>i]<\/span>]<\/span> -<\/span> 2<\/span> *<\/span> a[<\/span>pos[<\/span>i -<\/span> 1<\/span>]<\/span>]<\/span>;<\/span> ll num2 =<\/span> 1ll<\/span> *<\/span> 3<\/span> *<\/span> (<\/span>a[<\/span>pos[<\/span>i]<\/span>]<\/span> -<\/span> a[<\/span>pos[<\/span>i -<\/span> 1<\/span>]<\/span>]<\/span>)<\/span> -<\/span> mx1 -<\/span> mx2;<\/span> ans +<\/span>=<\/span> min<\/span>(<\/span>num1,<\/span>num2)<\/span>;<\/span> }<\/span> printf<\/span>(<\/span>\"%lld\\n\"<\/span>,<\/span>ans)<\/span>;<\/span> return<\/span> 0<\/span>;<\/span> }<\/span> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u8d2a\u5fc301\u80cc\u5305_wandThecitiesofBytelandandBerlandarelocatedontheaxis????????.Inaddition,onthisaxistherearealsodisputedciti...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5038"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5038"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5038\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}