DP->UVALive 4764

Bing it
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

I guess most of you played cards on the trip to Harbin, but I'm sure you have never played the following card game. This card game has Nrounds and 100000 types of cards numbered from 1 to 100000. A new card will be opened when each round begins. You can ``bing" this new card. And if the card you last ``bing" is the same with this new one, you will get 1 point. You can "bing" only one card, but you can change it into a new one. For example, the order of the 4 cards is 1 3 4 3. You can ``bing" 1 in the first round and change it into 3 in the second round. You get no point in the third round, but get 1 point in the last round. Additionally, there is a special card 999. If you ``bing" it and it is opened again, you will get 3 point.

Given the order of N cards, tell me the maximum points you can get.

Input

The input file will contain multiple test cases. Each test case will consist of two lines. The first line of each test case contains one integerN(2$ \le$N$ \le$100000). The second line of each test case contains a sequence of n integers, indicating the order of cards. A single line with the number ``0" marks the end of input; do not process this case.

Output

For each input test case, print the maximum points you can get.

Sample Input

2 
1 1 
5 
1 999 3 3 999 
0

Sample Output

1 
3
 
   
/*This Code is Submitted by OUC_Fighting for Problem 2883 at 2013-10-06 11:20:28*/
#include <iostream>
#include <string.h>

using namespace std;

#define MAXN 100000 + 10

int temp[MAXN], dp[MAXN];
bool vis[MAXN];

void input()
{
    int n;

    while (cin >> n, n)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> temp[i];
        }

        dp[temp[0]] = 0;

        int per = temp[0], mx = -1;

        memset(dp, 0, sizeof(dp));
        memset(vis, false, sizeof(vis));
        vis[per] = true;

        for (int i = 1; i < n; i++)
        {
            int next = temp[i];

            if (!vis[next])
            {
                dp[next] = dp[per];
                vis[next] = true;
            }
            else
            {
                if (next == 999)
                {
                    dp[next] = max(dp[per], dp[next] + 3);
                }
                else
                {
                    dp[next] = max(dp[per], dp[next] + 1);
                }
            }

            per = next;

            if (dp[next] > mx)
            {
                mx = dp[next];
            }
        }

        cout << mx << endl;
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    input();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值