#P3657. [USACO17FEB] Why Did the Cow Cross the Road II P

    ID: 1059 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2017USACO树状数组离散化枚举,暴力

[USACO17FEB] Why Did the Cow Cross the Road II P

Description

Farmer John raises NN breeds of cows, numbered from 11 to NN. Some breeds get along well with others; it turns out that if two breeds are numbered a,ba, b, then when ab4|a - b| \leq 4, these two breeds can get along, otherwise they cannot.

A long road runs through the farm. There are NN pastures on the left side of the road (each occupied by exactly one breed), and NN pastures on the right side of the road (each occupied by exactly one breed). To help the cows safely cross the road, Farmer John wants to paint some crosswalks (cow-walks?) on the road, subject to the following conditions:

  1. Each crosswalk connects a pasture on the left to a pasture on the right.
  2. The breeds of the two connected pastures must get along.
  3. No two crosswalks may intersect in the middle of the road.
  4. Each pasture can be connected to at most one crosswalk.

You need to help FJ determine the maximum possible number of crosswalks.

Input Format

The first line contains an integer NN (1N1051 \leq N \leq 10^5).

The next NN lines each contain an integer aia_i, representing the breed in the ii-th pasture on the left side of the road.

The next NN lines each contain an integer bib_i, representing the breed in the ii-th pasture on the right side of the road.

Output Format

Output the maximum number of crosswalks.

6
1
2
3
4
5
6
6
5
4
3
2
1
5

Hint

It is guaranteed that both aia_i and bib_i are permutations of 11 to NN.

Translated by ChatGPT 5