codeforces 456 C. Boredom
http://codeforces.com/contest/456/problem/C 题意:给出n(1E5)个数(1E5),每次可以选一个数a[k]并删掉a[k],a[k]-1,a[k]+1得到a[k]分,问最多能得到的分数。 思路:裸dp.f[i]表示选到数i的时候能达到的最大分数。开一个计数数组cnt[x]表示数字x出现的次数。那么显然有f[0]=0,f[1]=cnt[1],f[i(i>=2)] = max(f[i-1],f[i-2]+f[i]*cnt[i]);答案为f[max(a[i])],注意要开long long
/* ***********************************************
Author :111qqz
Created Time :2015年12月25日 星期五 18时17分05秒
File Name :code/cf/problem/455A.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E5+7;
7int n ;
8int a[N];
9LL cnt[N];
10LL dp[N];
1bool cmp( int a,int b)
2{
3 return a>b;
4}
5int main()
6{
7 #ifndef ONLINE_JUDGE
8 freopen("code/in.txt","r",stdin);
9 #endif
10 cin>>n;
11 ms(cnt,0);
12 for ( int i = 0 ; i < n ; i++)
13 {
14 scanf("%d",&a[i]);
15 cnt[a[i]]++;
16 }
1 dp[0] = 0 ; //dp[i]表示到数字i时能得到的最多分数
2 dp[1] = cnt[1];
3 for ( int i = 2 ; i <= 1E5 ; i++)
4 {
5 dp[i] = max(dp[i-1],dp[i-2]+i*cnt[i]);
6 }
7 cout<<dp[100000]<<endl;
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}