DP basic 455A codeforces
- // DP practice
- // 455A --- codeforces
- #include <iostream>
- #include <stdio.h>
- #include <cstring>
- using namespace std;
- const int N = 1000010;
- long long int dp[N];
- long long int cnt[N];
- int main()
- {
- int iT;
- int iNum,iLarge;
- int i,j;
- cin >> iT;
- memset(cnt,0,sizeof(cnt));
- iLarge = 0;
- for ( i=0 ; i<iT ; i++ )
- {
- cin >> iNum;
- // count number appear times
- cnt [iNum]++;
- // store the largest number to reduce run time
- if ( iNum>iLarge )
- iLarge = iNum;
- }
- dp[0] = 0;
- dp[1] = cnt[1];
- for ( j=2 ; j<=iLarge ; j++ )
- dp[j] = max(dp[j-1],dp[j-2] + (cnt[j]*j));
- cout << dp[j-1] << endl;
- }
DETAIL:
ex: input 2 -- 1 2
cnt[1] = 1 cnt[2] = 2 2 different number
dp[0] = 0 dp[1] = 1 拿第一種的情況
dp[2] = max ( dp[1], dp[0] + 1*2 ) 拿第二種的情況
ex: input 9 -- 1 2 1 3 2 2 2 2 3
cnt[1] = 2 cnt[2] = 5 cnt[3] = 2 3 different number
dp[0] = 0 dp[1] = 1 拿第一種的情況
dp[2] = max ( dp[1], dp[0]+2*5 ) 拿第二種的情況
dp[3] = max (dp[2], dp[1] + 3*2 ) 拿第三種的情況
留言
張貼留言