UVALive 4764 简单dp水题(也可以暴力求解)

B - Bing it

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

Submit Status Practice UVALive 4764

Description

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 N rounds 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 integer N(2N100000). 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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

int dp[100010];
bool vis[100010];
int a[100010];

int main(){
    int n;

    while(scanf("%d",&n)!=EOF){
            if(n==0)
            break; 

        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        memset(vis,false,sizeof(vis));

       for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
       }

         int sum=-1;

           int point=a[0];
           vis[point]=true;

         for(int i=1;i<n;i++){
             int next=a[i];
             if(!vis[next]){

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

             point=next;

             if(dp[next]>sum)
                sum=dp[next];

         }
         printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-12-28 06:40:56

UVALive 4764 简单dp水题(也可以暴力求解)的相关文章

URAL 1039 Anniversary Party 树形DP 水题

1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor rela

ACM :漫漫上学路 -DP -水题

CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit Status Description 对于csuxushu来说,能够在CSU(California State University)上学是他一生的荣幸.CSU校园内的道路设计的十分精巧,由n+1条水平道路和n+1条竖直道路等距交错而成,充分体现了校园深厚的文化底蕴.然而不幸的是CS市每到夏季,天降大雨,

HDU 1520 Anniversary party 树DP水题

非常水的树DP,状态为当前为i,上级来没来 然后跑一遍记忆化搜索即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃圾,大哥拿来了一袋老鼠,其中有w只白老鼠和b只黑老鼠.胡小兔先抓,先抓到白老鼠的人赢. 每次学姐抓完老鼠之后,总会有另外一只老鼠从袋子里自己跑出来(这只老鼠不算任何人抓的),而胡小兔抓老鼠时则不会发生这样的事. 每次袋子里的每只老鼠被抓到的概率相等,当有一只老鼠跑出来的时候,每只老鼠跑出来的几率也相

洛谷P1352 没有上司的舞会(树形DP水题)

题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri,但是呢,如果某个职员的上司来参加舞会了,那么这个职员就无论如何也不肯来参加舞会了.所以,请你编程计算,邀请哪些职员可以使快乐指数最大,求最大的快乐指数. 输入输出格式 输入格式: 第一行一个整数N.(1<=N<=6000) 接下来N行,第i+1行表示i号职员的快乐指数Ri.(-128<=Ri

POJ 3250 Bad Hair Day 简单DP 好题

Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a sp

13年山东省赛 The number of steps(概率dp水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Memory Limit: 128 M Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two ro

HDU 2089 不要62 (数位dp水题)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 32358    Accepted Submission(s): 11514 题目链接 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样

openjudge dp水题记录

当发现自己竟然不会打dp的时候内心是崩溃的,然后按照一年前的刷题记录刷openjudge,然后发现自己准确率比一年前(刚学信竞两个月时)的准确率低得多,已经没救. 列一下最近打的几道sb题 2985:数字组合 描述有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: 那么可能的组合有5=1+4和5=2+3和5=5三种组合方式.输入输入的第一行是两个正整数n和t,用空格隔开,其中1<=n<=20,表示正整数的个数,t为要求的和(1&