UVALive - 4764 bing it——动态规划
2017-09-10 15:35
393 查看
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
题意:给定一串数字序列 从前到后依次选择bing或者不bing 如果这张牌和bing了的一样 就加一分 999的话加三分 问最多可以得到多少分
思路:动态规划 用dp[i]记录前i张牌可以获得的最大分数 用num[x]记录bing了x的上一张牌是第几张 -1表示还没有被bing过
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
题意:给定一串数字序列 从前到后依次选择bing或者不bing 如果这张牌和bing了的一样 就加一分 999的话加三分 问最多可以得到多少分
思路:动态规划 用dp[i]记录前i张牌可以获得的最大分数 用num[x]记录bing了x的上一张牌是第几张 -1表示还没有被bing过
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <stack> #include <map> #include <cmath> #include <vector> #define max_ 100010 #define inf 0x3f3f3f3f #define ll long long using namespace std; int n; int num[max_],dp[max_]; int main(int argc, char const *argv[]) { int i; while(scanf("%d",&n)!=EOF&&n!=0) { memset(dp,0,sizeof(dp)); memset(num,-1,sizeof(num)); for(i=1;i<=n;i++) { int x; scanf("%d",&x); if(num[x]==-1) dp[i]=dp[i-1]; else { if(x==999) dp[i]=max(dp[i-1],dp[num[x]]+3); else dp[i]=max(dp[i-1],dp[num[x]]+1); } num[x]=i; } printf("%d\n",dp ); } return 0; }
相关文章推荐
- uvalive 4764 Bing it 基础dp
- UVALive - 4764 Bing it(dp)
- UVALive 4764 Bing it(dp)
- Bing it UVALive - 4764 (简单dp)
- UVALive 4764 Bing it
- UVALive - 4764 Bing it dp
- Bing it UVALive - 4764 DP
- UVALive 6117 (LA 6117) Pole Arrangement 动态规划
- UVALive4256[Salesmen] 动态规划
- ZOJ1256 UVA602 UVALive5474 What Day Is It?
- UVALive 3942 Remember the word 字典树 动态规划
- 【动态规划】UVALive - 4888 - Railroad
- DP->UVALive 4764
- UVALive 5063 Just Sum It
- UVALive 4764 简单dp水题(也可以暴力求解)
- UVALive 5903 Piece it together
- UVALive3983[Robotruck] 动态规划 滑动窗口优化
- UVALive 5903 Piece it together
- UVALive 4998 浅谈欧拉定理优化数位动态规划求解高阶同余方程
- G - Meeting Room Arrangement UVALive - 6606——动态规划