A - Depth of Parentheses

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は、数学の式に含まれる括弧の構造を解析するプログラムを作成しています。

数学の式では、括弧を使って計算の優先順位を表現します。括弧は入れ子にすることができ、括弧の中にさらに括弧を含めることで、複雑な計算順序を指定できます。高橋君は、まず括弧だけを取り出して、その入れ子の深さを調べることにしました。

( と ) のみからなる文字列 S が与えられます。S の長さを N とし、文字列の各文字を先頭から順に 1, 2, \ldots, N 番目と番号づけます。i 番目の文字における深さを、i 番目の文字より前にあるすべての文字(すなわち 1 番目から i-1 番目までの文字)に含まれる ( の個数から ) の個数を引いた値と定義します。i = 1 のときは、それより前に文字が存在しないため、深さは 0 です。

例えば、((())) の各文字の深さは、左から順に 0, 1, 2, 3, 2, 1 です。4 番目の文字 ) では、それより前の 3 文字がすべて ( であるため、深さは 3 - 0 = 3 となります。この文字列における深さの最大値は 3 です。また、(()()) の各文字の深さは、左から順に 0, 1, 2, 1, 2, 1 であり、深さの最大値は 2 です。

文字列 S が与えられるので、S の各文字の深さのうち最大値を求めてください。

制約

  • 2 \leq N \leq 10^6(N は文字列 S の長さ)
  • S は ( と ) のみからなる
  • S は正しく対応のとれた括弧列である。すなわち、以下の条件をともに満たす:
  • 先頭から任意の位置までにおいて、( の個数は ) の個数以上である
  • 文字列全体では ( と ) の個数が等しい

入力

S
  • 1 行目に、括弧の文字列 S が与えられる。入力はこの 1 行のみである。

出力

S の各文字の深さのうち最大値を 1 行で出力してください。


入力例 1

((()))

出力例 1

3

入力例 2

(()(())(()))

出力例 2

3

入力例 3

((((()))))((()))(())()(((((())))))

出力例 3

6

Score : 233 pts

Problem Statement

Takahashi is creating a program to analyze the structure of parentheses contained in mathematical expressions.

In mathematical expressions, parentheses are used to express the priority of calculations. Parentheses can be nested, and by including further parentheses within parentheses, complex calculation orders can be specified. Takahashi decided to first extract only the parentheses and examine the depth of their nesting.

You are given a string S consisting only of ( and ). Let N be the length of S, and number each character of the string from the beginning as the 1st, 2nd, \ldots, Nth character. The depth at the i-th character is defined as the number of ( minus the number of ) among all characters before the i-th character (that is, from the 1st to the (i-1)-th character). When i = 1, there are no characters before it, so the depth is 0.

For example, the depth of each character in ((())) is, from left to right, 0, 1, 2, 3, 2, 1. For the 4th character ), all 3 characters before it are (, so the depth is 3 - 0 = 3. The maximum depth in this string is 3. Also, the depth of each character in (()()) is, from left to right, 0, 1, 2, 1, 2, 1, and the maximum depth is 2.

Given the string S, find the maximum value among the depths of all characters in S.

Constraints

  • 2 \leq N \leq 10^6 (N is the length of string S)
  • S consists only of ( and )
  • S is a correctly matched parenthesis sequence. That is, it satisfies both of the following conditions:
  • At any position from the beginning, the number of ( is at least the number of )
  • In the entire string, the number of ( and ) are equal

Input

S
  • The first line contains the parenthesis string S. The input consists of only this single line.

Output

Print the maximum value among the depths of all characters in S in one line.


Sample Input 1

((()))

Sample Output 1

3

Sample Input 2

(()(())(()))

Sample Output 2

3

Sample Input 3

((((()))))((()))(())()(((((())))))

Sample Output 3

6
B - Sandwich Melody

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は、N 音からなるメロディを文字列 S として記録した。

S の i 文字目 S_i は大文字英字(A–Z)であり、i 番目の音を表す。

文字列 X に対し、X の 圧縮列 を次のように定義する。

X の文字を先頭から順に見ていき、最初の文字は常に残し、2 文字目以降は直前に残した文字と異なる場合のみ残すことで得られる文字列を、X の圧縮列と呼ぶ。

圧縮列では隣接する 2 文字は互いに異なる。

たとえば、AAABBBAA の圧縮列は ABA であり、ABCABC の圧縮列は ABCABC である。

1 \le l \le r \le N を満たす整数の組 (l, r) に対し、S の連続部分文字列 S_l S_{l+1} \dots S_r の圧縮列を考える。

この圧縮列の長さがちょうど 3 であり、かつその 1 文字目と 3 文字目が等しいとき、区間 [l, r] を サンドイッチ区間 と呼ぶ。

たとえば、ある区間に対応する連続部分文字列が AABBA であるとき、その圧縮列は ABA で、長さが 3 かつ 1 文字目と 3 文字目が等しいため、この区間はサンドイッチ区間である。

一方、連続部分文字列が ABC であるとき、圧縮列は ABC で長さは 3 だが 1 文字目と 3 文字目が異なるため、サンドイッチ区間ではない。

また、連続部分文字列が ABAB であるとき、圧縮列は ABAB で長さが 4 であるため、サンドイッチ区間ではない。

サンドイッチ区間の個数を求めてください。

制約

  • 1 \le N \le 10^6
  • S は長さ N の大文字英字(A–Z)からなる文字列である。

入力

N
S
  • 1 行目には、メロディの長さを表す整数 N が与えられる。
  • 2 行目には、長さ N の大文字英字からなる文字列 S が与えられる。

出力

サンドイッチ区間の個数を 1 行で出力せよ。


入力例 1

5
AABBA

出力例 1

2

入力例 2

3
ABC

出力例 2

0

入力例 3

25
AAABBAAAACCCAABBBADDDDAAA

出力例 3

25

入力例 4

75
AAAAABBBBAAAAAACCCDDDDDCCCCEEFFFFFFFEEEEEGGGGGGHHHGGGGIIIIIJJJJIIIIIIIKKKKK

出力例 4

111

入力例 5

1
A

出力例 5

0

Score : 333 pts

Problem Statement

Takahashi recorded a melody consisting of N notes as a string S.

The i-th character S_i of S is an uppercase English letter (A–Z), representing the i-th note.

For a string X, the compressed sequence of X is defined as follows.

Scan the characters of X from the beginning: always keep the first character, and for each subsequent character, keep it only if it differs from the last kept character. The resulting string is called the compressed sequence of X.

In a compressed sequence, any two adjacent characters are distinct.

For example, the compressed sequence of AAABBBAA is ABA, and the compressed sequence of ABCABC is ABCABC.

For a pair of integers (l, r) satisfying 1 \le l \le r \le N, consider the compressed sequence of the contiguous substring S_l S_{l+1} \dots S_r of S.

If the length of this compressed sequence is exactly 3 and its 1st and 3rd characters are equal, then the interval [l, r] is called a sandwich interval.

For example, if the contiguous substring corresponding to some interval is AABBA, its compressed sequence is ABA, which has length 3 and equal 1st and 3rd characters, so this interval is a sandwich interval.

On the other hand, if the contiguous substring is ABC, its compressed sequence is ABC, which has length 3 but the 1st and 3rd characters differ, so it is not a sandwich interval.

Also, if the contiguous substring is ABAB, its compressed sequence is ABAB, which has length 4, so it is not a sandwich interval.

Find the number of sandwich intervals.

Constraints

  • 1 \le N \le 10^6
  • S is a string of length N consisting of uppercase English letters (A–Z).

Input

N
S
  • The first line contains an integer N representing the length of the melody.
  • The second line contains a string S of length N consisting of uppercase English letters.

Output

Print the number of sandwich intervals in one line.


Sample Input 1

5
AABBA

Sample Output 1

2

Sample Input 2

3
ABC

Sample Output 2

0

Sample Input 3

25
AAABBAAAACCCAABBBADDDDAAA

Sample Output 3

25

Sample Input 4

75
AAAAABBBBAAAAAACCCDDDDDCCCCEEFFFFFFFEEEEEGGGGGGHHHGGGGIIIIIJJJJIIIIIIIKKKKK

Sample Output 4

111

Sample Input 5

1
A

Sample Output 5

0
C - Equal Load Distribution

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は引っ越し業者でアルバイトをしています。今日の仕事は、倉庫に一列に並んでいる N 個の荷物をトラックに積み込むことです。左から i 番目の荷物の重さは W_i kg です。

トラックには必要なだけいくつでも荷台を使うことができます。高橋君は、荷物を荷台に分配する際、以下のルールに従います。

ルール:

N 個の荷物の列を、連続する 1 つ以上の荷物からなるグループにすき間なく分割し、各グループをそれぞれ 1 つの荷台に積み込む。すなわち、各荷物はちょうど 1 つの荷台に積まれ、同じ荷台に積まれる荷物は元の列において連続している。さらに、すべての荷台に積まれた荷物の総重量が互いに等しくなければならない。

高橋君は、できるだけ多くの荷台を使いたいと考えています(荷台が多いほど各荷台の重量が軽くなり、積み下ろしが楽になるためです)。

上のルールを満たすように荷物を分配するとき、使用できる荷台の個数の最大値を求めてください。

なお、荷台を 1 つだけ使い、すべての荷物をその荷台に積む分配は常にルールを満たすため、条件を満たす分配は必ず存在します。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9
  • 入力はすべて整数

入力

N
W_1 W_2 \ldots W_N
  • 1 行目には、荷物の個数を表す整数 N が与えられる。
  • 2 行目には、左から i 番目の荷物の重さを表す N 個の整数 W_1, W_2, \ldots, W_N がスペース区切りで与えられる。

出力

使用できる荷台の個数の最大値を 1 行で出力せよ。


入力例 1

6
1 2 3 3 2 1

出力例 1

4

入力例 2

10
2 4 2 2 6 4 2 2 8 8

出力例 2

5

入力例 3

15
1000000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000 1000000000 500000000 500000000 1000000000

出力例 3

12

Score : 366 pts

Problem Statement

Takahashi is working part-time at a moving company. Today's job is to load N packages lined up in a row in a warehouse onto a truck. The weight of the i-th package from the left is W_i kg.

The truck has as many platforms as needed. When distributing the packages onto the platforms, Takahashi follows the following rule.

Rule:

Divide the row of N packages into groups of one or more consecutive packages with no gaps, and load each group onto exactly one platform. That is, each package is loaded onto exactly one platform, and packages loaded onto the same platform are consecutive in the original row. Furthermore, the total weight of packages on every platform must be equal.

Takahashi wants to use as many platforms as possible (because the more platforms there are, the lighter each platform's load becomes, making loading and unloading easier).

Find the maximum number of platforms that can be used when distributing the packages according to the above rule.

Note that using just one platform and loading all packages onto it always satisfies the rule, so a valid distribution always exists.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9
  • All inputs are integers

Input

N
W_1 W_2 \ldots W_N
  • The first line contains an integer N representing the number of packages.
  • The second line contains N integers W_1, W_2, \ldots, W_N separated by spaces, where the i-th integer represents the weight of the i-th package from the left.

Output

Print the maximum number of platforms that can be used, on a single line.


Sample Input 1

6
1 2 3 3 2 1

Sample Output 1

4

Sample Input 2

10
2 4 2 2 6 4 2 2 8 8

Sample Output 2

5

Sample Input 3

15
1000000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000 1000000000 500000000 500000000 1000000000

Sample Output 3

12
D - Delivery

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は町の中を移動して届け物をしようとしています。

町には 1 から N までの番号が付いた N 個の地点と、地点同士を結ぶ M 本の道路があります。i 本目の道路は地点 U_i と地点 V_i を双方向に結んでおり、通るのに C_i の時間がかかります。

高橋君は現在、地点 S にいます。届けたい荷物は地点 G に置いてあり、届け先は地点 T です。

高橋君はまず地点 G に行って荷物を受け取り、その後地点 T まで届ける必要があります。移動の途中で同じ地点や同じ道路を複数回通ることもできます。

高橋君が地点 S から出発し、地点 G を経由して地点 T に到達するまでに必要な時間の最小値を求めてください。ただし、地点 S から地点 G に到達できない場合、または地点 G から地点 T に到達できない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq 1.5 \times 10^5
  • 1 \leq S, G, T \leq N
  • S, G, T はすべて異なる
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • 同じ地点の組を結ぶ道路は高々 1 本である((U_i, V_i) と (U_j, V_j) が順序によらず一致するような異なる i, j は存在しない)
  • 入力はすべて整数である

入力

N M S G T
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • 1 行目には、地点の数を表す N 、道路の数を表す M 、高橋君の初期位置を表す S 、荷物のある地点を表す G 、届け先の地点を表す T が、スペース区切りで与えられる。
  • 続く M 行では、道路の情報が与えられる。
  • 1 + i 行目では、 i 本目の道路が地点 U_i と地点 V_i を結んでおり、その道路を通るのに C_i の時間がかかることを表す。

出力

高橋君が地点 S から出発し、地点 G で荷物を受け取ってから地点 T へ移動するのに必要な最小の時間を 1 行で出力せよ。条件を満たす移動が不可能な場合は -1 を出力せよ。


入力例 1

4 4 1 2 4
1 2 3
2 4 5
1 3 10
3 4 1

出力例 1

8

入力例 2

5 2 1 3 5
1 2 4
3 4 7

出力例 2

-1

入力例 3

8 10 1 5 8
1 2 4
2 3 2
3 5 3
1 4 10
4 5 1
5 6 2
6 8 3
5 7 8
7 8 1
3 6 5

出力例 3

14

入力例 4

12 18 2 9 12
1 2 5
1 3 2
2 4 4
3 4 1
3 5 7
4 6 3
5 6 2
6 7 4
7 8 6
8 9 1
6 9 10
4 10 8
10 11 2
11 12 3
9 12 5
5 10 1
2 5 9
7 11 2

出力例 4

22

入力例 5

3 2 1 2 3
1 2 1000000000
2 3 1000000000

出力例 5

2000000000

Score : 400 pts

Problem Statement

Takahashi is trying to make a delivery by moving through a town.

The town has N locations numbered from 1 to N, and M roads connecting pairs of locations. The i-th road bidirectionally connects location U_i and location V_i, and it takes C_i time to traverse.

Takahashi is currently at location S. The package he wants to deliver is placed at location G, and the destination is location T.

Takahashi must first go to location G to pick up the package, and then deliver it to location T. During his travel, he may pass through the same location or the same road multiple times.

Find the minimum time required for Takahashi to start from location S, pass through location G, and reach location T. If it is impossible to reach location G from location S, or impossible to reach location T from location G, output -1.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq 1.5 \times 10^5
  • 1 \leq S, G, T \leq N
  • S, G, T are all distinct
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • There is at most one road connecting any pair of locations (there do not exist distinct i, j such that (U_i, V_i) and (U_j, V_j) are the same pair regardless of order)
  • All input values are integers

Input

N M S G T
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • The first line contains N representing the number of locations, M representing the number of roads, S representing Takahashi's initial position, G representing the location of the package, and T representing the destination, separated by spaces.
  • The following M lines provide information about the roads.
  • The (1 + i)-th line indicates that the i-th road connects location U_i and location V_i, and it takes C_i time to traverse.

Output

Output in a single line the minimum time required for Takahashi to start from location S, pick up the package at location G, and then move to location T. If such a trip is impossible, output -1.


Sample Input 1

4 4 1 2 4
1 2 3
2 4 5
1 3 10
3 4 1

Sample Output 1

8

Sample Input 2

5 2 1 3 5
1 2 4
3 4 7

Sample Output 2

-1

Sample Input 3

8 10 1 5 8
1 2 4
2 3 2
3 5 3
1 4 10
4 5 1
5 6 2
6 8 3
5 7 8
7 8 1
3 6 5

Sample Output 3

14

Sample Input 4

12 18 2 9 12
1 2 5
1 3 2
2 4 4
3 4 1
3 5 7
4 6 3
5 6 2
6 7 4
7 8 6
8 9 1
6 9 10
4 10 8
10 11 2
11 12 3
9 12 5
5 10 1
2 5 9
7 11 2

Sample Output 4

22

Sample Input 5

3 2 1 2 3
1 2 1000000000
2 3 1000000000

Sample Output 5

2000000000
E - Installation of Wi-Fi Access Points

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君は、大学キャンパスのネットワーク管理者です。キャンパス内には N 個の建物と M 本の連絡通路があり、それぞれの建物には 1 から N までの番号が付けられています。i 番目の連絡通路は建物 u_i と建物 v_i を双方向に結んでいます。

このキャンパスの建物は「教育棟」と「研究棟」の2種類に分類されており、連絡通路はすべて異なる種類の建物の間を結んでいます。すなわち、建物と連絡通路からなるグラフは二部グラフです。ただし、各建物がどちらの種類に属するかは入力では与えられません。また、グラフは連結とは限りません。

大学の方針により、すべての連絡通路で Wi-Fi が利用できるようにしたいと考えています。ある建物に Wi-Fi アクセスポイントを設置すると、その建物を端点とするすべての連絡通路で Wi-Fi が利用可能になります。

高橋君は、すべての連絡通路について、その両端の建物のうち少なくとも一方にアクセスポイントが設置されている状態を実現したいです。この条件を満たすように選ぶ建物の数の最小値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i(自己ループはない)
  • 同じ建物の組を結ぶ連絡通路は高々 1 本である(多重辺はない)
  • 与えられるグラフは二部グラフである
  • グラフは連結とは限らない
  • 入力はすべて整数である

入力

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
  • 1 行目には、建物の数 N と連絡通路の数 M がスペース区切りで与えられる。
  • 続く M 行の i 行目 (1 \leq i \leq M) には、i 番目の連絡通路が結ぶ2つの建物の番号 u_i と v_i がスペース区切りで与えられる。

出力

すべての連絡通路で Wi-Fi を利用可能にするために、アクセスポイントを設置する建物の最小数を 1 行で出力してください。


入力例 1

4 4
1 2
2 3
3 4
4 1

出力例 1

2

入力例 2

5 4
1 2
1 3
1 4
1 5

出力例 2

1

入力例 3

10 8
1 6
1 7
2 7
2 8
3 8
3 9
4 9
5 10

出力例 3

5

入力例 4

8 16
1 5
1 6
1 7
1 8
2 5
2 6
2 7
2 8
3 5
3 6
3 7
3 8
4 5
4 6
4 7
4 8

出力例 4

4

入力例 5

1 0

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is the network administrator of a university campus. The campus has N buildings and M corridors, and each building is numbered from 1 to N. The i-th corridor bidirectionally connects building u_i and building v_i.

The buildings on this campus are classified into two types: "educational buildings" and "research buildings," and every corridor connects buildings of different types. In other words, the graph consisting of buildings and corridors is a bipartite graph. However, which type each building belongs to is not given in the input. Also, the graph is not necessarily connected.

According to university policy, Wi-Fi should be available on all corridors. When a Wi-Fi access point is installed in a building, Wi-Fi becomes available on all corridors that have that building as an endpoint.

Takahashi wants to achieve a state where, for every corridor, at least one of the two buildings at its endpoints has an access point installed. Find the minimum number of buildings that need to be selected to satisfy this condition.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i (no self-loops)
  • There is at most one corridor connecting the same pair of buildings (no multi-edges)
  • The given graph is a bipartite graph
  • The graph is not necessarily connected
  • All input values are integers

Input

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
  • The first line contains the number of buildings N and the number of corridors M, separated by a space.
  • The i-th of the following M lines (1 \leq i \leq M) contains the numbers u_i and v_i of the two buildings connected by the i-th corridor, separated by a space.

Output

Print in one line the minimum number of buildings in which access points need to be installed to make Wi-Fi available on all corridors.


Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

2

Sample Input 2

5 4
1 2
1 3
1 4
1 5

Sample Output 2

1

Sample Input 3

10 8
1 6
1 7
2 7
2 8
3 8
3 9
4 9
5 10

Sample Output 3

5

Sample Input 4

8 16
1 5
1 6
1 7
1 8
2 5
2 6
2 7
2 8
3 5
3 6
3 7
3 8
4 5
4 6
4 7
4 8

Sample Output 4

4

Sample Input 5

1 0

Sample Output 5

0