UVALive 4764 简单dp水题(也可以暴力求解)
2015-08-03 20:12
399 查看
B - Bing it
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVALive 4764
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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(2
![](http://7xjob4.com1.z0.glb.clouddn.com/80c682eaab977b8d00c3b9ca2fca02db)
N
![](http://7xjob4.com1.z0.glb.clouddn.com/80c682eaab977b8d00c3b9ca2fca02db)
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
Sample Output
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVALive 4764
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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(2
N
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
#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; }
相关文章推荐
- Anim动画
- HDU 1509:Windows Message Queue【优先队列】
- Applicability Statement 2 (AS2) Module
- jdbc在web中的应用
- Linux 虚拟机 快捷键
- 手术Robot能取代医生吗?
- setup factory 9制作VB程序安装包
- node在windows下node-gyp编译可能会出现的问题
- 自我介绍
- QString、string相互转换
- C++11 标准新特性: 右值引用与转移语义
- SharedPreferences
- iOS开发中常用的颜色及其对应的颜色值
- 钢条切割:递归+记忆化
- 二维数组作为参数传递的问题
- HDOJ Square Coins 1398【母函数】
- iOS应用内置付费In-App Purchases入门
- iOS UI04_Touch
- 深入理解DIP、IoC、DI以及IoC容器
- 运用JS设置cookie、读取cookie、删除cookie