C语言问题: Andryusha and Socks

版权声明:本人原创文章若需转载请标明出处和作者!沙沙 https://blog.csdn.net/weixin_44143702/article/details/86375607

Andryusha is an orderly boy and likes to keep things in their place.

Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.

Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?

Input

The first line contains the single integer n (1 ≤ n ≤ 10^5) — the number of sock pairs.

The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.

It is guaranteed that Andryusha took exactly two socks of each pair.

Output

Print single integer — the maximum number of socks that were on the table at the same time.

Examples

Input

1
1 1

Output

1

Input

3
2 1 1 3 2 3

Output

2

Note

In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.

In the second example Andryusha behaved as follows:

  • Initially the table was empty, he took out a sock from pair 2 and put it on the table.
  • Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
  • Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
  • Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
  • Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
  • Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.

Thus, at most two socks were on the table at the same time.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
    int n, x[200010], i, j, t, max, flag;
    scanf("%d", &n);
    t = max = 0;
    for(i = 0; i < (2 * n); i++)
    {
        flag = 0;
        scanf("%d", &x[i]);
        for(j = 0; j < i; j++)
        {
            if(x[j] != 0)
            {//这里的查找我已经改进的十分快捷了,还是time limit exceeded !
                if(x[i] == x[j])
                {
                    flag = 1;
                    break;
                    x[i] = x[j] = 0;
                }
            }
        }
        if(flag == 0)  t++;
        else t--;
        if(max < t)  max = t;
    }
    printf("%d\n", max);
    return 0;
}

 这个代码 time limit exceeded 

改正点:

1. 该方法思路清晰,但是效率低,可以改为用一个数组记录输入的编号,未输入时令其值为0,输入后令其值为1,并用打擂法记录table上面袜子数量的最大值

改正代码如下:

猜你喜欢

转载自blog.csdn.net/weixin_44143702/article/details/86375607